Merge pull request #16 from DavidBadura/array-collection
add doctrine array collection
This commit is contained in:
commit
07b050b065
|
@ -18,7 +18,8 @@
|
|||
"symfony/process": "~2.3",
|
||||
"jms/serializer": "0.16.*",
|
||||
"symfony/filesystem": "~2.3",
|
||||
"nesbot/carbon": "~1.14"
|
||||
"nesbot/carbon": "~1.14",
|
||||
"doctrine/collections": "~1.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.0"
|
||||
|
|
|
@ -2,16 +2,23 @@
|
|||
|
||||
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>
|
||||
*/
|
||||
class QueryBuilder
|
||||
{
|
||||
const SORT_URGENCY = 'urgency';
|
||||
const SORT_DESCRIPTION = 'description';
|
||||
const SORT_ENTRY = 'entry';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const ASC = Criteria::ASC;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const DESC = Criteria::DESC;
|
||||
|
||||
/**
|
||||
* @var TaskManager
|
||||
|
@ -24,9 +31,9 @@ class QueryBuilder
|
|||
protected $filter = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var Criteria
|
||||
*/
|
||||
protected $sortBy = self::SORT_URGENCY;
|
||||
protected $criteria;
|
||||
|
||||
/**
|
||||
* @param TaskManager $taskManager
|
||||
|
@ -34,6 +41,7 @@ class QueryBuilder
|
|||
public function __construct(TaskManager $taskManager)
|
||||
{
|
||||
$this->taskManager = $taskManager;
|
||||
$this->criteria = new Criteria();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -148,12 +156,34 @@ class QueryBuilder
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $by
|
||||
* @param array $orderings
|
||||
* @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;
|
||||
}
|
||||
|
@ -167,58 +197,12 @@ class QueryBuilder
|
|||
}
|
||||
|
||||
/**
|
||||
* @return Task[]
|
||||
* @return Task[]|ArrayCollection
|
||||
*/
|
||||
public function getResult()
|
||||
{
|
||||
$result = $this->taskManager->filter($this->getFilter());
|
||||
|
||||
return $this->sort($result, $this->sortBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
return $result->matching($this->criteria);
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ namespace DavidBadura\Taskwarrior;
|
|||
use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
|
||||
use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler;
|
||||
use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use JMS\Serializer\Handler\HandlerRegistryInterface;
|
||||
use JMS\Serializer\JsonSerializationVisitor;
|
||||
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
|
||||
|
@ -90,7 +91,7 @@ class TaskManager
|
|||
|
||||
/**
|
||||
* @param string|array $filter
|
||||
* @return Task[]
|
||||
* @return Task[]|ArrayCollection
|
||||
*/
|
||||
public function filterAll($filter = null)
|
||||
{
|
||||
|
@ -112,12 +113,12 @@ class TaskManager
|
|||
$this->tasks[$task->getUuid()] = $task;
|
||||
}
|
||||
|
||||
return $result;
|
||||
return new ArrayCollection($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $filter
|
||||
* @return Task[]
|
||||
* @return Task[]|ArrayCollection
|
||||
*/
|
||||
public function filter($filter = null)
|
||||
{
|
||||
|
|
|
@ -158,7 +158,10 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
|
|||
$this->taskManager->save($task1);
|
||||
$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()
|
||||
|
|
Loading…
Reference in New Issue