|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) eZ Systems AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +namespace App\QueryType; |
| 8 | + |
| 9 | +use eZ\Publish\API\Repository\LocationService; |
| 10 | +use eZ\Publish\API\Repository\Values\Content\Content; |
| 11 | +use eZ\Publish\API\Repository\Values\Content\Location; |
| 12 | +use eZ\Publish\API\Repository\Values\Content\Query; |
| 13 | +use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
| 14 | +use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
| 15 | +use eZ\Publish\Core\QueryType\QueryType; |
| 16 | + |
| 17 | +class AncestorsOfContentQueryType implements QueryType |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \eZ\Publish\API\Repository\LocationService |
| 21 | + */ |
| 22 | + private $locationService; |
| 23 | + |
| 24 | + public function __construct(LocationService $locationService) |
| 25 | + { |
| 26 | + $this->locationService = $locationService; |
| 27 | + } |
| 28 | + |
| 29 | + public function getQuery(array $parameters = []) |
| 30 | + { |
| 31 | + if ($parameters['content'] instanceof Content) { |
| 32 | + $pathStrings = array_map( |
| 33 | + function (Location $location) { |
| 34 | + return $location->pathString; |
| 35 | + }, |
| 36 | + $this->locationService->loadLocations($parameters['content']->contentInfo) |
| 37 | + ); |
| 38 | + } else { |
| 39 | + throw new InvalidArgumentException('content', 'should be of type API\Content'); |
| 40 | + } |
| 41 | + |
| 42 | + $filter = new Criterion\LogicalAnd([ |
| 43 | + new Criterion\Ancestor($pathStrings), |
| 44 | + new Criterion\ContentTypeIdentifier($parameters['type']), |
| 45 | + ]); |
| 46 | + |
| 47 | + return new Query([ |
| 48 | + 'filter' => $filter, |
| 49 | + ]); |
| 50 | + } |
| 51 | + |
| 52 | + public function getSupportedParameters() |
| 53 | + { |
| 54 | + return ['content', 'type']; |
| 55 | + } |
| 56 | + |
| 57 | + public static function getName() |
| 58 | + { |
| 59 | + return 'AncestorsOfContent'; |
| 60 | + } |
| 61 | +} |
0 commit comments