2017-05-11 12:20:01 +02:00
|
|
|
<?php
|
2018-09-12 12:34:48 +02:00
|
|
|
|
2017-05-11 12:20:01 +02:00
|
|
|
/**
|
|
|
|
|
* @package Grav\Framework\Object
|
|
|
|
|
*
|
2019-01-31 09:04:57 +01:00
|
|
|
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
|
2017-05-11 12:20:01 +02:00
|
|
|
* @license MIT License; see LICENSE file for details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Grav\Framework\Object;
|
|
|
|
|
|
2019-03-14 12:20:29 +01:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2018-07-05 11:27:28 +02:00
|
|
|
use Doctrine\Common\Collections\Criteria;
|
2017-05-11 12:20:01 +02:00
|
|
|
use Grav\Framework\Collection\ArrayCollection;
|
2017-09-13 08:50:34 +02:00
|
|
|
use Grav\Framework\Object\Access\NestedPropertyCollectionTrait;
|
|
|
|
|
use Grav\Framework\Object\Base\ObjectCollectionTrait;
|
2018-07-05 11:27:28 +02:00
|
|
|
use Grav\Framework\Object\Collection\ObjectExpressionVisitor;
|
2017-09-13 08:50:34 +02:00
|
|
|
use Grav\Framework\Object\Interfaces\NestedObjectInterface;
|
|
|
|
|
use Grav\Framework\Object\Interfaces\ObjectCollectionInterface;
|
2017-05-11 12:20:01 +02:00
|
|
|
|
|
|
|
|
/**
|
2018-09-12 12:34:48 +02:00
|
|
|
* Class contains a collection of objects.
|
2017-05-11 12:20:01 +02:00
|
|
|
*/
|
2017-09-13 08:50:34 +02:00
|
|
|
class ObjectCollection extends ArrayCollection implements ObjectCollectionInterface, NestedObjectInterface
|
2017-05-11 12:20:01 +02:00
|
|
|
{
|
2018-09-12 12:34:48 +02:00
|
|
|
use ObjectCollectionTrait;
|
|
|
|
|
use NestedPropertyCollectionTrait {
|
2017-09-13 08:50:34 +02:00
|
|
|
NestedPropertyCollectionTrait::group insteadof ObjectCollectionTrait;
|
|
|
|
|
}
|
2017-09-08 13:44:37 +02:00
|
|
|
|
2017-05-11 12:20:01 +02:00
|
|
|
/**
|
|
|
|
|
* @param array $elements
|
|
|
|
|
* @param string $key
|
2017-08-18 11:28:13 +02:00
|
|
|
* @throws \InvalidArgumentException
|
2017-05-11 12:20:01 +02:00
|
|
|
*/
|
|
|
|
|
public function __construct(array $elements = [], $key = null)
|
|
|
|
|
{
|
2017-09-13 08:50:34 +02:00
|
|
|
parent::__construct($this->setElements($elements));
|
2017-05-11 12:20:01 +02:00
|
|
|
|
2019-03-14 10:31:26 +01:00
|
|
|
$this->setKey($key ?? '');
|
2017-08-18 11:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 09:01:09 +01:00
|
|
|
/**
|
|
|
|
|
* @param array $ordering
|
2019-03-14 12:20:29 +01:00
|
|
|
* @return Collection|static
|
2018-11-15 09:01:09 +01:00
|
|
|
*/
|
|
|
|
|
public function orderBy(array $ordering)
|
|
|
|
|
{
|
|
|
|
|
$criteria = Criteria::create()->orderBy($ordering);
|
|
|
|
|
|
|
|
|
|
return $this->matching($criteria);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $start
|
|
|
|
|
* @param int|null $limit
|
|
|
|
|
* @return static
|
|
|
|
|
*/
|
|
|
|
|
public function limit($start, $limit = null)
|
|
|
|
|
{
|
|
|
|
|
return $this->createFrom($this->slice($start, $limit));
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 11:27:28 +02:00
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
|
|
|
|
public function matching(Criteria $criteria)
|
|
|
|
|
{
|
|
|
|
|
$expr = $criteria->getWhereExpression();
|
|
|
|
|
$filtered = $this->getElements();
|
|
|
|
|
|
|
|
|
|
if ($expr) {
|
|
|
|
|
$visitor = new ObjectExpressionVisitor();
|
|
|
|
|
$filter = $visitor->dispatch($expr);
|
|
|
|
|
$filtered = array_filter($filtered, $filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($orderings = $criteria->getOrderings()) {
|
|
|
|
|
$next = null;
|
2019-03-14 10:31:26 +01:00
|
|
|
/**
|
|
|
|
|
* @var string $field
|
|
|
|
|
* @var string $ordering
|
|
|
|
|
*/
|
2018-07-05 11:27:28 +02:00
|
|
|
foreach (array_reverse($orderings) as $field => $ordering) {
|
2018-09-12 12:34:48 +02:00
|
|
|
$next = ObjectExpressionVisitor::sortByField($field, $ordering === Criteria::DESC ? -1 : 1, $next);
|
2018-07-05 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-14 10:31:26 +01:00
|
|
|
if ($next) {
|
|
|
|
|
uasort($filtered, $next);
|
|
|
|
|
}
|
2018-07-05 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$offset = $criteria->getFirstResult();
|
|
|
|
|
$length = $criteria->getMaxResults();
|
|
|
|
|
|
|
|
|
|
if ($offset || $length) {
|
2018-09-12 12:34:48 +02:00
|
|
|
$filtered = \array_slice($filtered, (int)$offset, $length);
|
2018-07-05 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->createFrom($filtered);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-13 08:50:34 +02:00
|
|
|
protected function getElements()
|
2017-08-18 11:28:13 +02:00
|
|
|
{
|
2017-09-13 08:50:34 +02:00
|
|
|
return $this->toArray();
|
|
|
|
|
}
|
2017-08-18 11:28:13 +02:00
|
|
|
|
2017-09-13 08:50:34 +02:00
|
|
|
protected function setElements(array $elements)
|
|
|
|
|
{
|
|
|
|
|
return $elements;
|
2017-05-11 12:20:01 +02:00
|
|
|
}
|
|
|
|
|
}
|