| @@ 13-64 (lines=52) @@ | ||
| 10 | use eZ\Publish\API\Repository\Exceptions\NotImplementedException; |
|
| 11 | use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
|
| 12 | ||
| 13 | class CriteriaConverter |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * Criterion handlers. |
|
| 17 | * |
|
| 18 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Query\CriterionHandler[] |
|
| 19 | */ |
|
| 20 | protected $handlers; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * Construct from an optional array of Criterion handlers. |
|
| 24 | * |
|
| 25 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Query\CriterionHandler[] $handlers |
|
| 26 | */ |
|
| 27 | public function __construct(iterable $handlers = []) |
|
| 28 | { |
|
| 29 | $this->handlers = $handlers; |
|
| 30 | } |
|
| 31 | ||
| 32 | /** |
|
| 33 | * Adds handler. |
|
| 34 | * |
|
| 35 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Query\CriterionHandler $handler |
|
| 36 | */ |
|
| 37 | public function addHandler(CriterionHandler $handler) |
|
| 38 | { |
|
| 39 | $this->handlers[] = $handler; |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * Generic converter of criteria into query fragments. |
|
| 44 | * |
|
| 45 | * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException if Criterion is not applicable to its target |
|
| 46 | * |
|
| 47 | * @param \Doctrine\DBAL\Query\QueryBuilder $query |
|
| 48 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
|
| 49 | * |
|
| 50 | * @return \Doctrine\DBAL\Query\Expression\CompositeExpression|string Expression to be used in where clause of query |
|
| 51 | */ |
|
| 52 | public function convertCriteria(QueryBuilder $query, Criterion $criterion) |
|
| 53 | { |
|
| 54 | foreach ($this->handlers as $handler) { |
|
| 55 | if ($handler->accept($criterion)) { |
|
| 56 | return $handler->handle($this, $query, $criterion); |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||
| 60 | throw new NotImplementedException( |
|
| 61 | 'No visitor available for: ' . get_class($criterion) |
|
| 62 | ); |
|
| 63 | } |
|
| 64 | } |
|
| 65 | ||
| @@ 13-63 (lines=51) @@ | ||
| 10 | use eZ\Publish\API\Repository\Values\URL\Query\Criterion; |
|
| 11 | use eZ\Publish\Core\Persistence\Database\SelectQuery; |
|
| 12 | ||
| 13 | class CriteriaConverter |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * Criterion handlers. |
|
| 17 | * |
|
| 18 | * @var \eZ\Publish\Core\Persistence\Legacy\URL\Query\CriterionHandler[] |
|
| 19 | */ |
|
| 20 | protected $handlers; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * Construct from an optional array of Criterion handlers. |
|
| 24 | * |
|
| 25 | * @param \eZ\Publish\Core\Persistence\Legacy\URL\Query\CriterionHandler[] $handlers |
|
| 26 | */ |
|
| 27 | public function __construct(array $handlers = []) |
|
| 28 | { |
|
| 29 | $this->handlers = $handlers; |
|
| 30 | } |
|
| 31 | ||
| 32 | /** |
|
| 33 | * Adds handler. |
|
| 34 | * |
|
| 35 | * @param \eZ\Publish\Core\Persistence\Legacy\URL\Query\CriterionHandler $handler |
|
| 36 | */ |
|
| 37 | public function addHandler(CriterionHandler $handler) |
|
| 38 | { |
|
| 39 | $this->handlers[] = $handler; |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * Generic converter of criteria into query fragments. |
|
| 44 | * |
|
| 45 | * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException if Criterion is not applicable to its target |
|
| 46 | * |
|
| 47 | * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query |
|
| 48 | * @param \eZ\Publish\API\Repository\Values\URL\Query\Criterion $criterion |
|
| 49 | * @return \eZ\Publish\Core\Persistence\Database\Expression|string |
|
| 50 | */ |
|
| 51 | public function convertCriteria(SelectQuery $query, Criterion $criterion) |
|
| 52 | { |
|
| 53 | foreach ($this->handlers as $handler) { |
|
| 54 | if ($handler->accept($criterion)) { |
|
| 55 | return $handler->handle($this, $query, $criterion); |
|
| 56 | } |
|
| 57 | } |
|
| 58 | ||
| 59 | throw new NotImplementedException( |
|
| 60 | 'No visitor available for: ' . get_class($criterion) |
|
| 61 | ); |
|
| 62 | } |
|
| 63 | } |
|
| 64 | ||