add dates & urgency

This commit is contained in:
DavidBadura 2015-02-05 23:09:29 +00:00
parent dad551a72a
commit a25b90caba
4 changed files with 130 additions and 85 deletions

View File

@ -1,45 +0,0 @@
<?php
/**
* (c) SimpleThings GmbH
*/
namespace DavidBadura\Taskwarrior;
/**
* @author David Badura <badura@simplethings.de>
*/
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;
}
}

View File

@ -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
*/

View File

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

View File

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