diff --git a/README.md b/README.md index 7d7c85d..ed4f0b7 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,18 @@ ![WOW](http://i.imgur.com/mvSQh0M.gif) ```php -$tm = \DavidBadura\Taskwarrior\TaskManager::create(); +use DavidBadura\Taskwarrior\TaskManager; +use DavidBadura\Taskwarrior\Task; -$task = new \DavidBadura\Taskwarrior\Task(); +$tm = TaskManager::create(); + +$task = new Task(); $task->setDescription('program this lib'); $task->setProject('hobby'); $task->setDue(new \DateTime('tomorrow')); +$task->setPriority(Task::PRIORITY_HIGH); -$task->addTag('next'); // todo :D +$task->addTag('next'); $tm->save($task); diff --git a/src/Task.php b/src/Task.php index 08d5d71..8989ed0 100644 --- a/src/Task.php +++ b/src/Task.php @@ -14,9 +14,9 @@ class Task const STATUS_DELETED = 'deleted'; const STATUS_WAITING = 'waiting'; - const PRIORITY_LOW = 'L'; + const PRIORITY_LOW = 'L'; const PRIORITY_MEDIUM = 'M'; - const PRIORITY_HIGH = 'H'; + const PRIORITY_HIGH = 'H'; /** * @var string @@ -53,6 +53,13 @@ class Task */ private $due; + /** + * @var array + * + * @JMS\Type(name="array") + */ + private $tags; + /** * @var float * @@ -156,6 +163,42 @@ class Task $this->due = $due; } + /** + * @return array + */ + public function getTags() + { + return (array)$this->tags; + } + + /** + * @param array $tags + */ + public function setTags(array $tags = array()) + { + $this->tags = $tags; + } + + /** + * @param string $tag + */ + public function addTag($tag) + { + if (!in_array($tag, $this->tags)) { + $this->tags[] = $tag; + } + } + + /** + * @param string $tag + */ + public function removeTag($tag) + { + if (false !== $key = array_search($tag, $this->tags)) { + unset($this->tags[$key]); + } + } + /** * @return \DateTime */ diff --git a/src/TaskManager.php b/src/TaskManager.php index 8ded8d5..f8a0281 100644 --- a/src/TaskManager.php +++ b/src/TaskManager.php @@ -192,6 +192,7 @@ class TaskManager 'description' => $task->getDescription(), 'project' => $task->getProject(), 'priority' => $task->getPriority(), + 'tags' => $task->getTags(), 'due' => $task->getDue() ? $task->getDue()->format('Ymd\THis\Z') : null, ], $task->getUuid() diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index 7add7fe..943104e 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -70,6 +70,14 @@ class Taskwarrior $options[] = 'priority:' . $params['priority']; } + if (array_key_exists('tags', $params)) { + if (is_array($params['tags'])) { + $options[] = 'tags:' . implode(',', $params['tags']); + } else { + $options[] = 'tags:' . $params['tags']; + } + } + if (array_key_exists('description', $params)) { $options[] = $params['description']; } diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index 339206e..03bac37 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -373,6 +373,37 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(Task::PRIORITY_HIGH, $task1->getPriority()); } + public function testTag() + { + $task1 = new Task(); + $task1->setDescription('foo1'); + + $this->taskManager->save($task1); + $this->taskManager->clear(); + + $task1 = $this->taskManager->find($task1->getUuid()); + $this->assertEmpty($task1->getTags()); + + $task1->setTags(array('a', 'b', 'c')); + $this->taskManager->save($task1); + $this->taskManager->clear(); + + $task1 = $this->taskManager->find($task1->getUuid()); + $this->assertEquals(array('a', 'b', 'c'), $task1->getTags()); + + $task1->addTag('d'); + $task1->removeTag('a'); + + $this->taskManager->save($task1); + $this->taskManager->clear(); + + $task1 = $this->taskManager->find($task1->getUuid()); + $this->assertEquals(array('b', 'c', 'd'), $task1->getTags()); + $this->assertCount(0, $this->taskManager->filter('+a')); + $this->assertCount(1, $this->taskManager->filter('+b')); + } + + /** * @param string $string * @return \DateTime