From 624435017b7bf92ea0ddcf4cfbee1e26b0d1da44 Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Fri, 15 May 2015 11:38:38 +0000 Subject: [PATCH] better durations support (months) --- README.md | 2 +- src/Recurring.php | 39 +++++++++++-- tests/RecurringTest.php | 125 +++++++++++++++++++++++++++++++++++----- 3 files changed, 148 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5b2f765..3d95594 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ composer require 'davidbadura/taskwarrior' ## Requirements -Task Warrior changes its behavior by patch level updates and it is very difficult to support all versions. +Taskwarrior changes its behavior by patch level updates and it is very difficult to support all versions. That's why I've decided just to support only one version: **2.4.3** ## Usage diff --git a/src/Recurring.php b/src/Recurring.php index d52825f..052e2b6 100644 --- a/src/Recurring.php +++ b/src/Recurring.php @@ -16,6 +16,8 @@ class Recurring const WEEKDAYS = 'weekdays'; const WEEKLY = 'weekly'; const BIWEEKLY = 'biweekly'; + const MONTHLY = 'monthly'; + const BIMONTHLY = 'bimonthly'; const QUARTERLY = 'quarterly'; const SEMIANNUAL = 'semiannual'; const ANNUAL = 'annual'; @@ -70,19 +72,48 @@ class Recurring return true; } - if (preg_match('/^[0-9]+d$/', $recur)) { + // seconds + if (preg_match('/^[0-9]*\s?se?c?o?n?d?s?$/', $recur)) { return true; } - if (preg_match('/^[0-9]+w$/', $recur)) { + // minutes + if (preg_match('/^[0-9]*\s?mi?n?u?t?e?s?$/', $recur)) { return true; } - if (preg_match('/^[0-9]+q$/', $recur)) { + // hours + if (preg_match('/^[0-9]*\s?ho?u?r?s?$/', $recur)) { return true; } - if (preg_match('/^[0-9]+y$/', $recur)) { + // days + if (preg_match('/^[0-9]*\s?da?y?s?$/', $recur)) { + return true; + } + + // weeks + if (preg_match('/^[0-9]*\s?we?e?k?s?$/', $recur)) { + return true; + } + + // months + if (preg_match('/^[0-9]*\s?mo?n?t?h?s?$/', $recur)) { + return true; + } + + // quarters + if (preg_match('/^[0-9]*\s?qu?a?r?t?e?r?(s|ly)?/', $recur)) { + return true; + } + + // years + if (preg_match('/^[0-9]*\s?ye?a?r?s?$/', $recur)) { + return true; + } + + // fortnight | sennight + if (preg_match('/^[0-9]*\s?(fortnight|sennight)$/', $recur)) { return true; } diff --git a/tests/RecurringTest.php b/tests/RecurringTest.php index a692396..626ed42 100644 --- a/tests/RecurringTest.php +++ b/tests/RecurringTest.php @@ -10,29 +10,129 @@ use DavidBadura\Taskwarrior\Recurring; class RecurringTest extends \PHPUnit_Framework_TestCase { /** + * @see http://taskwarrior.org/docs/durations.html * @return array */ public function validData() { return [ + ['5 seconds'], + ['5 second'], + ['5 secs'], + ['5 sec'], + ['5 s'], + ['5seconds'], + ['5second'], + ['5secs'], + ['5sec'], + ['5s'], + ['second'], + ['sec'], + ['5 minutes'], + ['5 minute'], + ['5 mins'], + ['5 min'], + ['5minutes'], + ['5minute'], + ['5mins'], + ['5min'], + ['minute'], + ['min'], + ['3 hours'], + ['3 hour'], + ['3 hrs'], + ['3 hr'], + ['3 h'], + ['3hours'], + ['3hour'], + ['3hrs'], + ['3hr'], + ['3h'], + ['hour'], + ['hr'], + ['2 days'], + ['2 day'], + ['2 d'], + ['2days'], + ['2day'], + ['2d'], ['daily'], - ['weekdays'], + ['day'], + ['3 weeks'], + ['3 week'], + ['3 wks'], + ['3 wk'], + ['3 w'], + ['3weeks'], + ['3week'], + ['3wks'], + ['3wk'], + ['3w'], ['weekly'], + ['week'], + ['wk'], + ['weekdays'], + ['2 fortnight'], + ['2 sennight'], + ['2fortnight'], + ['2sennight'], ['biweekly'], + ['fortnight'], + ['sennight'], + ['5 months'], + ['5 month'], + ['5 mnths'], + ['5 mths'], + ['5 mth'], + ['5 mo'], + ['5 m'], + ['5months'], + ['5month'], + ['5mnths'], + ['5mths'], + ['5mth'], + ['5mo'], + ['5m'], + ['monthly'], + ['month'], + ['mth'], + ['mo'], + ['bimonthly'], + ['1 quarterly'], + ['1 quarters'], + ['1 quarter'], + ['1 qrtrs'], + ['1 qrtr'], + ['1 qtr'], + ['1 q'], + ['1quarterly'], + ['1quarters'], + ['1quarter'], + ['1qrtrs'], + ['1qrtr'], + ['1qtr'], + ['1q'], ['quarterly'], + ['quarter'], + ['qrtr'], + ['qtr'], ['semiannual'], + ['1 years'], + ['1 year'], + ['1 yrs'], + ['1 yr'], + ['1 y'], + ['1years'], + ['1year'], + ['1yrs'], + ['1yr'], + ['1y'], ['annual'], ['yearly'], + ['year'], + ['yr'], ['biannual'], - ['biyearly'], - ['2d'], - ['12d'], - ['2w'], - ['12w'], - ['2q'], - ['12q'], - ['2y'], - ['12y'] + ['biyearly'] ]; } @@ -56,8 +156,7 @@ class RecurringTest extends \PHPUnit_Framework_TestCase ['foo'], ['weekday'], ['2x'], - ['a2w'], - ['d'] + ['a2w'] ]; } @@ -69,6 +168,6 @@ class RecurringTest extends \PHPUnit_Framework_TestCase { $this->setExpectedException('DavidBadura\Taskwarrior\Exception\RecurringParseException'); - $obj = new Recurring($recur); + new Recurring($recur); } } \ No newline at end of file