From ece82c24a95c716622a93be1ff16810522c82cf9 Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Sun, 8 Feb 2015 15:44:41 +0000 Subject: [PATCH 1/2] add annotations --- src/Annotation.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++ src/Task.php | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/Annotation.php diff --git a/src/Annotation.php b/src/Annotation.php new file mode 100644 index 0000000..c656dd7 --- /dev/null +++ b/src/Annotation.php @@ -0,0 +1,50 @@ + + */ +class Annotation +{ + /** + * @var string + * + * @JMS\Type("string") + */ + private $description; + + /** + * @var Carbon + * + * @JMS\Type("Carbon") + */ + private $entry; + + /** + * @return string + */ + public function getDescription() + { + return $this->description; + } + + /** + * @param string $description + */ + public function setDescription($description) + { + $this->description = $description; + } + + /** + * @return Carbon + */ + public function getEntry() + { + return $this->entry; + } +} \ No newline at end of file diff --git a/src/Task.php b/src/Task.php index 220e012..3c33928 100644 --- a/src/Task.php +++ b/src/Task.php @@ -106,6 +106,13 @@ class Task */ private $until; + /** + * @var Annotation[] + * + * @JMS\Type("array") + */ + private $annotations; + /** * @var Carbon * @@ -352,6 +359,46 @@ class Task $this->until = $this->parseDateTime($until); } + /** + * @return Annotation[] + */ + public function getAnnotations() + { + return $this->annotations; + } + + /** + * @param Annotation[] $annotations + */ + public function setAnnotations(array $annotations = []) + { + $this->annotations = []; + + foreach ($annotations as $annotation) { + $this->addAnnotation($annotation); + } + } + + /** + * @param Annotation $annotation + */ + public function addAnnotation(Annotation $annotation) + { + if (!in_array($annotation, $this->annotations)) { + $this->annotations[] = $annotation; + } + } + + /** + * @param Annotation $annotation + */ + public function removeAnnotation(Annotation $annotation) + { + if ($key = array_search($annotation, $this->annotations)) { + unset($this->annotations[$key]); + } + } + /** * @return Carbon */ From d506df75aee13226ec2342cb70ef38a9d9601fc0 Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Sat, 12 Mar 2016 10:04:31 +0000 Subject: [PATCH 2/2] finish annotation supprt --- src/Annotation.php | 9 ++++++ src/Task.php | 7 +++-- tests/TaskManagerTest.php | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/Annotation.php b/src/Annotation.php index c656dd7..23398d7 100644 --- a/src/Annotation.php +++ b/src/Annotation.php @@ -24,6 +24,15 @@ class Annotation */ private $entry; + /** + * @param string $description + */ + public function __construct($description = null) + { + $this->entry = new Carbon(); // todo https://bug.tasktools.org/browse/TW-1780 + $this->description = $description; + } + /** * @return string */ diff --git a/src/Task.php b/src/Task.php index 3c33928..d6c236f 100644 --- a/src/Task.php +++ b/src/Task.php @@ -111,7 +111,7 @@ class Task * * @JMS\Type("array") */ - private $annotations; + private $annotations = []; /** * @var Carbon @@ -364,7 +364,7 @@ class Task */ public function getAnnotations() { - return $this->annotations; + return (array)$this->annotations; } /** @@ -394,8 +394,9 @@ class Task */ public function removeAnnotation(Annotation $annotation) { - if ($key = array_search($annotation, $this->annotations)) { + if (false !== $key = array_search($annotation, $this->annotations)) { unset($this->annotations[$key]); + $this->annotations = array_values($this->annotations); } } diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index 7609dc7..ea2d379 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -2,6 +2,7 @@ namespace DavidBadura\Taskwarrior\Test; +use DavidBadura\Taskwarrior\Annotation; use DavidBadura\Taskwarrior\Recurring; use DavidBadura\Taskwarrior\Task; use DavidBadura\Taskwarrior\TaskManager; @@ -928,6 +929,64 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(0, $this->taskManager->count('+baz')); } + public function testEmptyAnnotation() + { + $task = new Task(); + $task->setDescription('foo'); + + $this->taskManager->save($task); + + $temp1 = $this->taskManager->find($task->getUuid()); + + $this->assertCount(0, $temp1->getAnnotations()); + } + + public function testAnnotation() + { + $task = new Task(); + $task->setDescription('foo'); + $task->addAnnotation(new Annotation('testbar')); + + $this->taskManager->save($task); + $task = $this->taskManager->find($task->getUuid()); + + $annotations = $task->getAnnotations(); + $this->assertCount(1, $annotations); + $this->assertEquals('testbar', $annotations[0]->getDescription()); + + $task->addAnnotation(new Annotation('blabla')); + + $this->taskManager->save($task); + $task = $this->taskManager->find($task->getUuid()); + + $annotations = $task->getAnnotations(); + $this->assertCount(2, $annotations); + $this->assertEquals('testbar', $annotations[0]->getDescription()); + $this->assertEquals('blabla', $annotations[1]->getDescription()); + + $task->removeAnnotation($annotations[0]); + + $this->taskManager->save($task); + $task = $this->taskManager->find($task->getUuid()); + + $annotations = $task->getAnnotations(); + $this->assertCount(1, $annotations); + $this->assertEquals('blabla', $annotations[0]->getDescription()); + + $task->setAnnotations([ + new Annotation('foo'), + new Annotation('bar') + ]); + + $this->taskManager->save($task); + $task = $this->taskManager->find($task->getUuid()); + + $annotations = $task->getAnnotations(); + $this->assertCount(2, $annotations); + $this->assertEquals('foo', $annotations[0]->getDescription()); + $this->assertEquals('bar', $annotations[1]->getDescription()); + } + /** * @param string $string * @return \DateTime