From a25b90caba12ee96d5646ec4a233918ea2452414 Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Thu, 5 Feb 2015 23:09:29 +0000 Subject: [PATCH] add dates & urgency --- src/Modify.php | 45 ---------------------- src/Task.php | 79 +++++++++++++++++++++++++++++---------- src/Taskwarrior.php | 44 ++++++++++++---------- tests/TaskwarriorTest.php | 47 +++++++++++++++++++++++ 4 files changed, 130 insertions(+), 85 deletions(-) delete mode 100644 src/Modify.php diff --git a/src/Modify.php b/src/Modify.php deleted file mode 100644 index 714395f..0000000 --- a/src/Modify.php +++ /dev/null @@ -1,45 +0,0 @@ - - */ -class Modify -{ - /** - * @var string - */ - private $description; - - /** - * @return string - */ - public function getDescription() - { - return $this->description; - } - - /** - * @param string $description - */ - public function setDescription($description) - { - $this->description = $description; - } - - /** - * @param Task $task - * @return Modify - */ - public static function createFromTask(Task $task) - { - $modify = new self(); - $modify->setDescription($task->getDescription()); - - return $modify; - } -} \ No newline at end of file diff --git a/src/Task.php b/src/Task.php index 2a29549..cac9cc7 100644 --- a/src/Task.php +++ b/src/Task.php @@ -9,10 +9,10 @@ use JMS\Serializer\Annotation as JMS; */ class Task { - const STATUS_PENDING = 'pending'; + const STATUS_PENDING = 'pending'; const STATUS_COMPLETED = 'completed'; - const STATUS_DELETED = 'deleted'; - const STATUS_WAITING = 'waiting'; + const STATUS_DELETED = 'deleted'; + const STATUS_WAITING = 'waiting'; /** * @var string @@ -28,6 +28,27 @@ class Task */ private $description; + /** + * @var \DateTime + * + * @JMS\Type(name="DateTime<'Ymd\THis\Z'>") + */ + private $due; + + /** + * @var float + * + * @JMS\Type(name="float") + */ + private $urgency; + + /** + * @var \DateTime + * + * @JMS\Type(name="DateTime<'Ymd\THis\Z'>") + */ + private $entry; + /** * @var string * @@ -40,7 +61,9 @@ class Task */ public function __construct() { - $this->status = self::STATUS_PENDING; + $this->urgency = 0; + $this->entry = new \DateTime('now', new \DateTimeZone('UTC')); + $this->status = self::STATUS_PENDING; } /** @@ -51,14 +74,6 @@ class Task return $this->uuid; } - /** - * @param string $uuid - */ - public function setUuid($uuid) - { - $this->uuid = $uuid; - } - /** * @return string */ @@ -75,6 +90,38 @@ class Task $this->description = $description; } + /** + * @return \DateTime + */ + public function getDue() + { + return $this->due; + } + + /** + * @param \DateTime $due + */ + public function setDue(\DateTime $due = null) + { + $this->due = $due; + } + + /** + * @return \DateTime + */ + public function getEntry() + { + return $this->entry; + } + + /** + * @return float + */ + public function getUrgency() + { + return $this->urgency; + } + /** * @return string */ @@ -83,14 +130,6 @@ class Task return $this->status; } - /** - * @param string $status - */ - public function setStatus($status) - { - $this->status = $status; - } - /** * @return bool */ diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index cc6322d..0f8e291 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -116,7 +116,7 @@ class Taskwarrior } $this->command('delete', $task->getUuid()); - $task->setStatus(Task::STATUS_DELETED); + $this->update($task); } /** @@ -129,7 +129,7 @@ class Taskwarrior } $this->command('done', $task->getUuid()); - $task->setStatus(Task::STATUS_COMPLETED); + $this->update($task); } /** @@ -167,14 +167,8 @@ class Taskwarrior */ private function export($filter = '') { - $tasks = array(); - $json = $this->command('export', $filter); - if (!$json) { - return $tasks; - } - $serializer = SerializerBuilder::create() ->addDefaultHandlers() ->build(); @@ -226,7 +220,7 @@ class Taskwarrior $json = $this->serializeTask($task); $uuid = $this->import($json); - $task->setUuid($uuid); + $this->setValue($task, 'uuid', $uuid); $this->tasks[$uuid] = $task; $this->update($task); @@ -237,9 +231,16 @@ class Taskwarrior */ private function edit(Task $task) { - $modify = Modify::createFromTask($task); - $options = $this->modifyOptions($modify); + $options = []; + + if ($task->getDue()) { + $options[] = 'due:' . $task->getDue()->format('Ymd\THis\Z'); + } + + $options[] = $task->getDescription(); + $this->command('modify', $task->getUuid(), $options); + $this->update($task); } /** @@ -247,7 +248,10 @@ class Taskwarrior */ private function update(Task $task) { - // todo + $clean = $this->export($task->getUuid())[0]; + + $this->setValue($task, 'urgency', $clean->getUrgency()); + $this->setValue($task, 'status', $clean->getStatus()); } /** @@ -284,15 +288,15 @@ class Taskwarrior } /** - * @param Modify $modify - * @return array + * @param Task $task + * @param string $attr + * @param mixed $value */ - private function modifyOptions(Modify $modify) + private function setValue(Task $task, $attr, $value) { - $array = []; - - $array[] = $modify->getDescription(); - - return $array; + $reflectionClass = new \ReflectionClass('DavidBadura\Taskwarrior\Task'); + $prop = $reflectionClass->getProperty($attr); + $prop->setAccessible(true); + $prop->setValue($task, $value); } } \ No newline at end of file diff --git a/tests/TaskwarriorTest.php b/tests/TaskwarriorTest.php index aff7826..f44e69e 100644 --- a/tests/TaskwarriorTest.php +++ b/tests/TaskwarriorTest.php @@ -193,4 +193,51 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase $this->assertEquals('bar1', $result->getDescription()); } + + public function testDue() + { + $date = $this->createDateTime('1989-01-08 11:12:13'); + + $task1 = new Task(); + $task1->setDescription('foo1'); + $task1->setDue($date); + + $this->taskwarrior->save($task1); + $this->taskwarrior->clear(); + + $task2 = $this->taskwarrior->find($task1->getUuid()); + $this->assertEquals($date, $task2->getDue()); + + $newDate = $this->createDateTime('2002-02-20 11:12:13'); + + $task2->setDue($newDate); + + $this->taskwarrior->save($task2); + $this->taskwarrior->clear(); + + $task3 = $this->taskwarrior->find($task1->getUuid()); + $this->assertEquals($newDate, $task3->getDue()); + } + + public function testUrgency() + { + $date = $this->createDateTime('1989-01-08 11:12:13'); + + $task1 = new Task(); + $task1->setDescription('foo1'); + $task1->setDue($date); + + $this->taskwarrior->save($task1); + + $this->assertEquals(12, $task1->getUrgency()); + } + + /** + * @param string $string + * @return \DateTime + */ + private function createDateTime($string = 'now') + { + return new \DateTime($string, new \DateTimeZone('UTC')); + } } \ No newline at end of file