add doctrine array collection

This commit is contained in:
DavidBadura 2015-04-22 10:44:11 +00:00
parent 43318c7c0e
commit 562f00171a
4 changed files with 51 additions and 62 deletions

View File

@ -18,7 +18,8 @@
"symfony/process": "~2.3", "symfony/process": "~2.3",
"jms/serializer": "0.16.*", "jms/serializer": "0.16.*",
"symfony/filesystem": "~2.3", "symfony/filesystem": "~2.3",
"nesbot/carbon": "~1.14" "nesbot/carbon": "~1.14",
"doctrine/collections": "~1.3"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~4.0" "phpunit/phpunit": "~4.0"

View File

@ -2,16 +2,23 @@
namespace DavidBadura\Taskwarrior; namespace DavidBadura\Taskwarrior;
use DavidBadura\Taskwarrior\Exception\TaskwarriorException; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
/** /**
* @author David Badura <d.a.badura@gmail.com> * @author David Badura <d.a.badura@gmail.com>
*/ */
class QueryBuilder class QueryBuilder
{ {
const SORT_URGENCY = 'urgency'; /**
const SORT_DESCRIPTION = 'description'; * @var string
const SORT_ENTRY = 'entry'; */
const ASC = Criteria::ASC;
/**
* @var string
*/
const DESC = Criteria::DESC;
/** /**
* @var TaskManager * @var TaskManager
@ -24,9 +31,9 @@ class QueryBuilder
protected $filter = []; protected $filter = [];
/** /**
* @var string * @var Criteria
*/ */
protected $sortBy = self::SORT_URGENCY; protected $criteria;
/** /**
* @param TaskManager $taskManager * @param TaskManager $taskManager
@ -34,6 +41,7 @@ class QueryBuilder
public function __construct(TaskManager $taskManager) public function __construct(TaskManager $taskManager)
{ {
$this->taskManager = $taskManager; $this->taskManager = $taskManager;
$this->criteria = new Criteria();
} }
/** /**
@ -148,12 +156,34 @@ class QueryBuilder
} }
/** /**
* @param string $by * @param array $orderings
* @return $this * @return $this
*/ */
public function sortBy($by = self::SORT_URGENCY) public function orderBy(array $orderings)
{ {
$this->sortBy = $by; $this->criteria->orderBy($orderings);
return $this;
}
/**
* @param int $firstResult
* @return $this
*/
public function setFirstResult($firstResult)
{
$this->criteria->setFirstResult($firstResult);
return $this;
}
/**
* @param int $maxResults
* @return $this
*/
public function setMaxResults($maxResults)
{
$this->criteria->setMaxResults($maxResults);
return $this; return $this;
} }
@ -167,58 +197,12 @@ class QueryBuilder
} }
/** /**
* @return Task[] * @return Task[]|ArrayCollection
*/ */
public function getResult() public function getResult()
{ {
$result = $this->taskManager->filter($this->getFilter()); $result = $this->taskManager->filter($this->getFilter());
return $this->sort($result, $this->sortBy); return $result->matching($this->criteria);
}
/**
* @param Task[] $tasks
* @param string $by
* @return Task[]
* @throws TaskwarriorException
*/
protected function sort(array $tasks, $by)
{
switch ($by) {
case self::SORT_ENTRY:
$callback = function (Task $a, Task $b) {
return $a->getEntry() >= $b->getEntry() ? 1 : -1;
};
break;
case self::SORT_DESCRIPTION:
$callback = function (Task $a, Task $b) {
return strcmp($b->getDescription(), $a->getDescription());
};
break;
case self::SORT_URGENCY:
$callback = function (Task $a, Task $b) {
if (0 != $diff = $b->getUrgency() - $a->getUrgency()) {
return $diff;
}
return $a->getEntry() >= $b->getEntry() ? 1 : -1;
};
break;
default:
throw new TaskwarriorException('sorting by "%s" is not supported', $by);
}
usort($tasks, $callback);
return $tasks;
} }
} }

View File

@ -5,6 +5,7 @@ namespace DavidBadura\Taskwarrior;
use DavidBadura\Taskwarrior\Exception\TaskwarriorException; use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler; use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler;
use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler; use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Handler\HandlerRegistryInterface; use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\JsonSerializationVisitor; use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\Naming\CamelCaseNamingStrategy; use JMS\Serializer\Naming\CamelCaseNamingStrategy;
@ -90,7 +91,7 @@ class TaskManager
/** /**
* @param string|array $filter * @param string|array $filter
* @return Task[] * @return Task[]|ArrayCollection
*/ */
public function filterAll($filter = null) public function filterAll($filter = null)
{ {
@ -112,12 +113,12 @@ class TaskManager
$this->tasks[$task->getUuid()] = $task; $this->tasks[$task->getUuid()] = $task;
} }
return $result; return new ArrayCollection($result);
} }
/** /**
* @param string|array $filter * @param string|array $filter
* @return Task[] * @return Task[]|ArrayCollection
*/ */
public function filter($filter = null) public function filter($filter = null)
{ {

View File

@ -158,7 +158,10 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->taskManager->save($task1); $this->taskManager->save($task1);
$this->taskManager->save($task2); $this->taskManager->save($task2);
$this->assertCount(2, $this->taskManager->filter()); $result = $this->taskManager->filter();
$this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $result);
$this->assertCount(2, $result);
} }
public function testMultiFilter() public function testMultiFilter()