diff --git a/README.md b/README.md index a63ecbb..0869082 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ ```php use DavidBadura\Taskwarrior\TaskManager; use DavidBadura\Taskwarrior\Task; +use DavidBadura\Taskwarrior\Recurring; $tm = TaskManager::create(); @@ -16,7 +17,7 @@ $task->setProject('hobby'); $task->setDue(new \DateTime('tomorrow')); $task->setPriority(Task::PRIORITY_HIGH); $task->addTag('next'); -$task->setRecur('daily'); +$task->setRecur(new Recurring(Recurring::DAILY)); $tm->save($task); diff --git a/src/Recurring.php b/src/Recurring.php new file mode 100644 index 0000000..0c9281d --- /dev/null +++ b/src/Recurring.php @@ -0,0 +1,81 @@ + + */ +class Recurring +{ + const DAILY = 'daily'; + const WEEKDAYS = 'weekdays'; + const WEEKLY = 'weekly'; + const BIWEEKLY = 'biweekly'; + const QUARTERLY = 'quarterly'; + const SEMIANNUAL = 'semiannual'; + const ANNUAL = 'annual'; + const YEARLY = 'yearly'; + const BIANNUAL = 'biannual'; + const BIYEARLY = 'biyearly'; + + /** + * @var string + */ + private $recurring; + + /** + * @param string $recurring + * @throws TaskwarriorException + */ + public function __construct($recurring) + { + if ($this->isValid($recurring)) { + $this->recurring = $recurring; + } else { + throw new TaskwarriorException(); + } + } + + /** + * @return string + */ + public function __toString() + { + return $this->recurring; + } + + /** + * @param string $recur + * @return bool + */ + private function isValid($recur) + { + $refClass = new \ReflectionClass(__CLASS__); + $constants = $refClass->getConstants(); + + if (in_array($recur, $constants)) { + return true; + } + + if (preg_match('/^[0-9]+d$/', $recur)) { + return true; + } + + if (preg_match('/^[0-9]+w$/', $recur)) { + return true; + } + + if (preg_match('/^[0-9]+q$/', $recur)) { + return true; + } + + if (preg_match('/^[0-9]+y$/', $recur)) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/src/Serializer/Handler/RecurringHandler.php b/src/Serializer/Handler/RecurringHandler.php new file mode 100644 index 0000000..82e7f91 --- /dev/null +++ b/src/Serializer/Handler/RecurringHandler.php @@ -0,0 +1,68 @@ + + */ +class RecurringHandler implements SubscribingHandlerInterface +{ + /** + * @return array + */ + public static function getSubscribingMethods() + { + $methods = array(); + + $methods[] = array( + 'type' => 'Recurring', + 'format' => 'json', + 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, + 'method' => 'deserializeCarbon' + ); + + $methods[] = array( + 'type' => 'Recurring', + 'format' => 'json', + 'direction' => GraphNavigator::DIRECTION_SERIALIZATION, + 'method' => 'serializeCarbon' + ); + + return $methods; + } + + /** + * @param VisitorInterface $visitor + * @param Recurring $recurring + * @param array $type + * @param Context $context + * @return string + */ + public function serializeCarbon(VisitorInterface $visitor, Recurring $recurring, array $type, Context $context) + { + return $visitor->visitString((string)$recurring, $type, $context); + } + + /** + * @param VisitorInterface $visitor + * @param string $data + * @param array $type + * @return \DateTime|null + */ + public function deserializeCarbon(VisitorInterface $visitor, $data, array $type) + { + if (null === $data) { + return null; + } + + return new Recurring((string) $data); + } +} diff --git a/src/Task.php b/src/Task.php index 483ea5a..13a1fa6 100644 --- a/src/Task.php +++ b/src/Task.php @@ -86,7 +86,7 @@ class Task /** * @var string * - * @JMS\Type("string") + * @JMS\Type("Recurring") */ private $recur; @@ -269,11 +269,19 @@ class Task } /** - * @param string $recur + * @param string|Recurring $recur + * @throws TaskwarriorException */ public function setRecur($recur) { - $this->recur = $recur; + if (is_string($recur)) { + $this->recur = new Recurring($recur); + } elseif ($recur instanceof Recurring) { + $this->recur = $recur; + } else { + throw new TaskwarriorException(); + + } } /** diff --git a/src/TaskManager.php b/src/TaskManager.php index dddeeff..6c20d9f 100644 --- a/src/TaskManager.php +++ b/src/TaskManager.php @@ -3,6 +3,7 @@ namespace DavidBadura\Taskwarrior; use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler; +use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler; use JMS\Serializer\Handler\HandlerRegistryInterface; use JMS\Serializer\Serializer; use JMS\Serializer\SerializerBuilder; @@ -277,6 +278,7 @@ class TaskManager return SerializerBuilder::create() ->configureHandlers(function (HandlerRegistryInterface $registry) { $registry->registerSubscribingHandler(new CarbonHandler()); + $registry->registerSubscribingHandler(new RecurringHandler()); }) ->addDefaultHandlers() ->setDebug(true) diff --git a/tests/RecurringTest.php b/tests/RecurringTest.php new file mode 100644 index 0000000..c8663ee --- /dev/null +++ b/tests/RecurringTest.php @@ -0,0 +1,74 @@ + + */ +class RecurringTest extends \PHPUnit_Framework_TestCase +{ + /** + * @return array + */ + public function validData() + { + return [ + ['daily'], + ['weekdays'], + ['weekly'], + ['biweekly'], + ['quarterly'], + ['semiannual'], + ['annual'], + ['yearly'], + ['biannual'], + ['biyearly'], + ['2d'], + ['12d'], + ['2w'], + ['12w'], + ['2q'], + ['12q'], + ['2y'], + ['12y'] + ]; + } + + /** + * @dataProvider validData + * @param $recur + */ + public function testValid($recur) + { + $this->assertEquals($recur, (string)new Recurring($recur)); + } + + /** + * @return array + */ + public function invalidData() + { + return [ + ['dailya'], + ['asdasd'], + ['foo'], + ['weekday'], + ['2x'], + ['a2w'], + ['d'] + ]; + } + + /** + * @dataProvider invalidData + * @param $recur + */ + public function testInvalid($recur) + { + $this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException'); + + $obj = new Recurring($recur); + } +} \ No newline at end of file