add recurring object

This commit is contained in:
DavidBadura 2015-02-08 13:00:15 +00:00
parent 7199ad4dfa
commit 0d5a2b46e6
6 changed files with 238 additions and 4 deletions

View File

@ -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);

81
src/Recurring.php Normal file
View File

@ -0,0 +1,81 @@
<?php
/**
*
*/
namespace DavidBadura\Taskwarrior;
/**
* @author David Badura <d.a.badura@gmail.com>
*/
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;
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace DavidBadura\Taskwarrior\Serializer\Handler;
use Carbon\Carbon;
use DavidBadura\Taskwarrior\Recurring;
use JMS\Serializer\Context;
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\VisitorInterface;
/**
* @author David Badura <d.a.badura@gmail.com>
*/
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);
}
}

View File

@ -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)
{
if (is_string($recur)) {
$this->recur = new Recurring($recur);
} elseif ($recur instanceof Recurring) {
$this->recur = $recur;
} else {
throw new TaskwarriorException();
}
}
/**

View File

@ -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)

74
tests/RecurringTest.php Normal file
View File

@ -0,0 +1,74 @@
<?php
namespace DavidBadura\Taskwarrior\Test;
use DavidBadura\Taskwarrior\Recurring;
/**
* @author David Badura <badura@simplethings.de>
*/
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);
}
}