add date features

This commit is contained in:
DavidBadura 2015-02-06 23:22:17 +00:00
parent d2596a35f0
commit eee7c668cc
4 changed files with 284 additions and 61 deletions

View File

@ -53,6 +53,20 @@ class Task
*/ */
private $due; private $due;
/**
* @var \DateTime
*
* @JMS\Type(name="DateTime<'Ymd\THis\Z'>")
*/
private $wait;
/**
* @var \DateTime
*
* @JMS\Type(name="DateTime<'Ymd\THis\Z'>")
*/
private $until;
/** /**
* @var array * @var array
* *
@ -74,6 +88,21 @@ class Task
*/ */
private $entry; private $entry;
/**
* @var \DateTime
*
* @JMS\Type(name="DateTime<'Ymd\THis\Z'>")
*/
private $modified;
/**
* @var \DateTime
*
* @JMS\Type(name="DateTime<'Ymd\THis\Z'>")
*/
private $end;
/** /**
* @var string * @var string
* *
@ -156,11 +185,43 @@ class Task
} }
/** /**
* @param \DateTime $due * @param \DateTime|string $due
*/ */
public function setDue(\DateTime $due = null) public function setDue($due = null)
{ {
$this->due = $due; $this->due = $this->parseDateTime($due);
}
/**
* @return \DateTime
*/
public function getWait()
{
return $this->wait;
}
/**
* @param \DateTime|string $wait
*/
public function setWait($wait = null)
{
$this->wait = $this->parseDateTime($wait);
}
/**
* @return \DateTime
*/
public function getUntil()
{
return $this->until;
}
/**
* @param \DateTime|string $until
*/
public function setUntil($until = null)
{
$this->until = $this->parseDateTime($until);
} }
/** /**
@ -215,6 +276,22 @@ class Task
return $this->entry; return $this->entry;
} }
/**
* @return \DateTime
*/
public function getModified()
{
return $this->modified;
}
/**
* @return \DateTime
*/
public function getEnd()
{
return $this->end;
}
/** /**
* @return float * @return float
*/ */
@ -263,6 +340,32 @@ class Task
return $this->status == self::STATUS_DELETED; return $this->status == self::STATUS_DELETED;
} }
/**
* @param string|\DateTime|null $date
* @return \DateTime|null
* @throws TaskwarriorException
*/
private function parseDateTime($date)
{
if ($date instanceof \DateTime) {
$date = clone $date;
$date->setTimezone(new \DateTimeZone('UTC'));
return $date;
}
if (is_string($date)) {
return new \DateTime($date, new \DateTimeZone('UTC'));
}
if ($date === null) {
return null;
}
throw new TaskwarriorException();
}
/** /**
* *
*/ */

View File

@ -47,6 +47,8 @@ class TaskManager
} else { } else {
$this->edit($task); $this->edit($task);
} }
$this->refresh($task);
} }
/** /**
@ -89,6 +91,7 @@ class TaskManager
if (isset($this->tasks[$task->getUuid()])) { if (isset($this->tasks[$task->getUuid()])) {
$result[$key] = $this->tasks[$task->getUuid()]; $result[$key] = $this->tasks[$task->getUuid()];
$this->merge($result[$key], $task);
continue; continue;
} }
@ -120,7 +123,7 @@ class TaskManager
} }
$this->taskwarrior->delete($task->getUuid()); $this->taskwarrior->delete($task->getUuid());
$this->update($task); $this->refresh($task);
} }
/** /**
@ -133,15 +136,16 @@ class TaskManager
} }
$this->taskwarrior->done($task->getUuid()); $this->taskwarrior->done($task->getUuid());
$this->update($task); $this->refresh($task);
} }
/** /**
* @return array * @param Task $task
*/ */
public function projects() public function refresh(Task $task)
{ {
return $this->taskwarrior->projects(); $clean = $this->export($task->getUuid())[0];
$this->merge($task, $clean);
} }
/** /**
@ -178,8 +182,6 @@ class TaskManager
$this->setValue($task, 'uuid', $uuid); $this->setValue($task, 'uuid', $uuid);
$this->tasks[$uuid] = $task; $this->tasks[$uuid] = $task;
$this->update($task);
} }
/** /**
@ -194,22 +196,22 @@ class TaskManager
'priority' => $task->getPriority(), 'priority' => $task->getPriority(),
'tags' => $task->getTags(), 'tags' => $task->getTags(),
'due' => $task->getDue() ? $task->getDue()->format('Ymd\THis\Z') : null, 'due' => $task->getDue() ? $task->getDue()->format('Ymd\THis\Z') : null,
'wait' => $task->getWait() ? $task->getWait()->format('Ymd\THis\Z') : null,
], ],
$task->getUuid() $task->getUuid()
); );
$this->update($task);
} }
/** /**
* @param Task $task * @param Task $old
* @param Task $new
*/ */
private function update(Task $task) private function merge(Task $old, Task $new)
{ {
$clean = $this->export($task->getUuid())[0]; $this->setValue($old, 'urgency', $new->getUrgency());
$this->setValue($old, 'status', $new->getStatus());
$this->setValue($task, 'urgency', $clean->getUrgency()); $this->setValue($old, 'modified', $new->getModified());
$this->setValue($task, 'status', $clean->getStatus()); $this->setValue($old, 'end', $new->getEnd());
} }
/** /**

View File

@ -50,48 +50,44 @@ class Taskwarrior
$this->command('done', $uuid); $this->command('done', $uuid);
} }
/**
* @param array $params
* @throws TaskwarriorException
*/
public function add(array $params)
{
$this->command('modify', null, $this->getOptions($params));
}
/** /**
* @param array $params * @param array $params
* @param string|array $filter * @param string|array $filter
*/ */
public function modify(array $params, $filter = null) public function modify(array $params, $filter = null)
{ {
$options = []; $this->command('modify', $filter, $this->getOptions($params));
if (array_key_exists('due', $params)) {
$options[] = 'due:' . $params['due'];
}
if (array_key_exists('project', $params)) {
$options[] = 'project:' . $params['project'];
}
if (array_key_exists('priority', $params)) {
$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'];
}
$this->command('modify', $filter, $options);
} }
/** /**
* @param null $filter
* @return array * @return array
* @throws TaskwarriorException * @throws TaskwarriorException
*/ */
public function projects() public function projects($filter = null)
{ {
$result = $this->command('_project'); $result = $this->command('_project', $filter);
return array_filter(explode("\n", $result), 'strlen');
}
/**
* @param null $filter
* @return array
* @throws TaskwarriorException
*/
public function tags($filter = null)
{
$result = $this->command('_tags', $filter);
return array_filter(explode("\n", $result), 'strlen'); return array_filter(explode("\n", $result), 'strlen');
} }
@ -112,11 +108,11 @@ class Taskwarrior
$fs->remove($file); $fs->remove($file);
if (!preg_match('/([0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})/', $output, $matches)) { if ($uuid = self::parseUuid($output)) {
throw new TaskwarriorException(); return $uuid;
} }
return $matches[1]; throw new TaskwarriorException();
} }
/** /**
@ -140,7 +136,7 @@ class Taskwarrior
$builder = $this->createProcessBuilder(); $builder = $this->createProcessBuilder();
if (!is_array($filter)) { if (!is_array($filter)) {
$filter = [$filter]; $filter = explode(' ', $filter);
} }
foreach ($filter as $param) { foreach ($filter as $param) {
@ -180,6 +176,49 @@ class Taskwarrior
return $this->command('_version'); return $this->command('_version');
} }
/**
* @param $params
* @return array
*/
private function getOptions($params)
{
$options = [];
if (array_key_exists('due', $params)) {
$options[] = 'due:' . $params['due'];
}
if (array_key_exists('wait', $params)) {
$options[] = 'wait:' . $params['wait'];
}
if (array_key_exists('until', $params)) {
$options[] = 'until:' . $params['until'];
}
if (array_key_exists('project', $params)) {
$options[] = 'project:' . $params['project'];
}
if (array_key_exists('priority', $params)) {
$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'];
}
return $options;
}
/** /**
* @return ProcessBuilder * @return ProcessBuilder
*/ */
@ -196,4 +235,17 @@ class Taskwarrior
return $builder; return $builder;
} }
/**
* @param string $string
* @return string|null
*/
public static function parseUuid($string)
{
if (preg_match('/([0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})/', $string, $matches)) {
return $matches[1];
}
return null;
}
} }

View File

@ -176,6 +176,32 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertCount(1, $this->taskManager->filter('project:home prio:H +now')); $this->assertCount(1, $this->taskManager->filter('project:home prio:H +now'));
} }
public function testDates()
{
$task1 = new Task();
$task1->setDescription('foo1');
$this->taskManager->save($task1);
$this->assertInstanceOf('DateTime', $task1->getEntry());
$this->assertNull($task1->getModified());
$this->assertNull($task1->getEnd());
$task1->setDescription('bar2');
$this->taskManager->save($task1);
$this->assertInstanceOf('DateTime', $task1->getEntry());
$this->assertInstanceOf('DateTime', $task1->getModified());
$this->assertNull($task1->getEnd());
$this->taskManager->done($task1);
$this->assertInstanceOf('DateTime', $task1->getEntry());
$this->assertInstanceOf('DateTime', $task1->getModified());
$this->assertInstanceOf('DateTime', $task1->getEnd());
}
public function testPending() public function testPending()
{ {
$task1 = new Task(); $task1 = new Task();
@ -377,7 +403,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->taskManager->save($task1); $this->taskManager->save($task1);
$this->taskManager->save($task2); $this->taskManager->save($task2);
$this->assertEquals(array('home', 'office'), $this->taskManager->projects()); $this->assertEquals(array('home', 'office'), $this->taskwarrior->projects());
} }
public function testPriority() public function testPriority()
@ -434,6 +460,46 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertCount(1, $this->taskManager->filter('+b')); $this->assertCount(1, $this->taskManager->filter('+b'));
} }
public function testWait()
{
$task1 = new Task();
$task1->setDescription('foo1');
$this->taskManager->save($task1);
$this->taskManager->clear();
$task1 = $this->taskManager->find($task1->getUuid());
$this->assertNull($task1->getWait());
$task1->setWait($this->createDateTime('+2 sec'));
$this->taskManager->save($task1);
$this->assertTrue($task1->isWaiting());
$this->assertCount(0, $this->taskManager->filter());
$this->assertCount(1, $this->taskManager->filterAll('status:waiting'));
sleep(3);
$this->assertCount(1, $this->taskManager->filter());
$this->assertFalse($task1->isWaiting());
}
public function testUntil()
{
$this->markTestIncomplete('not working yet');
$task1 = new Task();
$task1->setDescription('foo1');
$task1->setUntil('+2 sec');
$this->taskManager->save($task1);
$this->assertCount(1, $this->taskManager->filter());
sleep(3);
$this->assertCount(0, $this->taskManager->filter());
}
/** /**
* @param string $string * @param string $string
@ -441,6 +507,6 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
*/ */
private function createDateTime($string = 'now') private function createDateTime($string = 'now')
{ {
return new \DateTime($string, new \DateTimeZone('UTC')); return new \DateTime($string);
} }
} }