|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Neos\ContentRepository\NodeAccess\FlowQueryOperations; |
| 6 | + |
| 7 | +/* |
| 8 | + * This file is part of the Neos.ContentRepository package. |
| 9 | + * |
| 10 | + * (c) Contributors of the Neos Project - www.neos.io |
| 11 | + * |
| 12 | + * This package is Open Source Software. For the full copyright and license |
| 13 | + * information, please view the LICENSE file which was distributed with this |
| 14 | + * source code. |
| 15 | + */ |
| 16 | + |
| 17 | +use Neos\Flow\Annotations as Flow; |
| 18 | +use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindDescendantNodesFilter; |
| 19 | +use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\NodeType\NodeTypeCriteria; |
| 20 | +use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\Pagination\Pagination; |
| 21 | +use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\PropertyValueCriteriaParser; |
| 22 | +use Neos\ContentRepository\Core\Projection\ContentGraph\Node; |
| 23 | +use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry; |
| 24 | +use Neos\Eel\FlowQuery\FlowQuery; |
| 25 | +use Neos\Eel\FlowQuery\FlowQueryException; |
| 26 | +use Neos\Eel\FlowQuery\Operations\AbstractOperation; |
| 27 | + |
| 28 | +/** |
| 29 | + * "findByCriteria" operation working on ContentRepository nodes. This operation allows for retrieval of descendant nodes |
| 30 | + * |
| 31 | + * Argument 1 (string|null): nodeTypeFilter, A list of NodeType Names seperated by ",", disallowed NodeTypes are prefixed with "!" |
| 32 | + * |
| 33 | + * Argument 2 (string|null): propertyValueCriteria A property criteria in the form |
| 34 | + * |
| 35 | + * property criteria are specified as "<property> <operator> <value>". Multiple criteria can be combined using "AND", "OR", "NOT" and "()" |
| 36 | + * |
| 37 | + * |
| 38 | + * property criteria support the following comparison operators: |
| 39 | + * |
| 40 | + * =~ : Strict equality of case-insensitive value and operand |
| 41 | + * = : Strict equality of value and operand |
| 42 | + * !=~ : Strict inequality of case-insensitive value and operand |
| 43 | + * != : Strict inequality of value and operand |
| 44 | + * < : Value is less than operand |
| 45 | + * <= : Value is less than or equal to operand |
| 46 | + * > : Value is greater than operand |
| 47 | + * >= : Value is greater than or equal to operand |
| 48 | + * $=~ : Value ends with operand (string-based) or case-insensitive value's last element is equal to operand (array-based) |
| 49 | + * $= : Value ends with operand (string-based) or value's last element is equal to operand (array-based) |
| 50 | + * ^=~ : Value starts with operand (string-based) or case-insensitive value's first element is equal to operand (array-based) |
| 51 | + * ^= : Value starts with operand (string-based) or value's first element is equal to operand (array-based) |
| 52 | + * *=~ : Value contains operand (string-based) or case-insensitive value contains an element that is equal to operand (array based) |
| 53 | + * *= : Value contains operand (string-based) or value contains an element that is equal to operand (array based) |
| 54 | + * |
| 55 | + * criteria can be combined using "AND" and "OR": |
| 56 | + * |
| 57 | + * "prop1 ^= 'foo' AND (prop2 = 'bar' OR prop3 = 'baz')" |
| 58 | + * |
| 59 | + * furthermore "NOT" can be used to negate a whole sub query |
| 60 | + * |
| 61 | + * "prop1 ^= 'foo' AND NOT (prop2 = 'bar' OR prop3 = 'baz')" |
| 62 | + * |
| 63 | + * Argument 3 ({limit?:int, offset?:int}}): Pagination of the date |
| 64 | + * |
| 65 | + * |
| 66 | + * Example (node type): |
| 67 | + * |
| 68 | + * q(node).findByCriteria('Neos.NodeTypes:Text') |
| 69 | + * |
| 70 | + * Example (multiple node types): |
| 71 | + * |
| 72 | + * q(node).findByCriteria('Neos.NodeTypes:Text,Neos.NodeTypes:Image') |
| 73 | + * |
| 74 | + * Example (node type with property filter): |
| 75 | + * |
| 76 | + * q(node).findByCriteria('Neos.NodeTypes:Text', 'text*="Neos"') |
| 77 | + * |
| 78 | + * Example (node type with property filter and pagination): |
| 79 | + * |
| 80 | + * q(node).findByCriteria('Neos.NodeTypes:Document', 'title*="Flow"', {limit:10, offset:2}) |
| 81 | + */ |
| 82 | +class FindByCriteriaOperation extends AbstractOperation |
| 83 | +{ |
| 84 | + use CreateNodeHashTrait; |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritdoc} |
| 88 | + * |
| 89 | + * @var string |
| 90 | + */ |
| 91 | + protected static $shortName = 'findByCriteria'; |
| 92 | + |
| 93 | + /** |
| 94 | + * {@inheritdoc} |
| 95 | + * |
| 96 | + * @var integer |
| 97 | + */ |
| 98 | + protected static $priority = 100; |
| 99 | + |
| 100 | + /** |
| 101 | + * @Flow\Inject |
| 102 | + * @var ContentRepositoryRegistry |
| 103 | + */ |
| 104 | + protected $contentRepositoryRegistry; |
| 105 | + |
| 106 | + /** |
| 107 | + * {@inheritdoc} |
| 108 | + * |
| 109 | + * @param array<int,mixed> $context (or array-like object) onto which this operation should be applied |
| 110 | + * @return boolean true if the operation can be applied onto the $context, false otherwise |
| 111 | + */ |
| 112 | + public function canEvaluate($context) |
| 113 | + { |
| 114 | + foreach ($context as $contextNode) { |
| 115 | + if (!$contextNode instanceof Node) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return true; |
| 121 | + } |
| 122 | + /** |
| 123 | + * This operation operates rather on the given Context object than on the given node |
| 124 | + * and thus may work with the legacy node interface until subgraphs are available |
| 125 | + * {@inheritdoc} |
| 126 | + * |
| 127 | + * @param FlowQuery<int,mixed> $flowQuery the FlowQuery object |
| 128 | + * @param array<int,mixed> $arguments the arguments for this operation |
| 129 | + * @throws FlowQueryException |
| 130 | + * @throws \Neos\Eel\Exception |
| 131 | + * @throws \Neos\Eel\FlowQuery\FizzleException |
| 132 | + */ |
| 133 | + public function evaluate(FlowQuery $flowQuery, array $arguments): void |
| 134 | + { |
| 135 | + /** @var array<int,Node> $contextNodes */ |
| 136 | + $contextNodes = $flowQuery->getContext(); |
| 137 | + if (count($contextNodes) === 0) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + $firstContextNode = reset($contextNodes); |
| 142 | + assert($firstContextNode instanceof Node); |
| 143 | + |
| 144 | + $nodeTypeFilter = $arguments[0] ?? null; |
| 145 | + $propertyValueFilter = $arguments[1] ?? null; |
| 146 | + $pagination = $arguments[2] ?? null; |
| 147 | + |
| 148 | + assert($nodeTypeFilter === null || is_string($nodeTypeFilter)); |
| 149 | + assert($propertyValueFilter === null || is_string($propertyValueFilter)); |
| 150 | + assert($pagination === null || is_array($pagination)); |
| 151 | + |
| 152 | + /** @var Node[] $result */ |
| 153 | + $result = []; |
| 154 | + $findDescendentNodesFilter = FindDescendantNodesFilter::create( |
| 155 | + nodeTypes: $nodeTypeFilter ? NodeTypeCriteria::fromFilterString($nodeTypeFilter) : null, |
| 156 | + propertyValue: $propertyValueFilter ? PropertyValueCriteriaParser::parse($propertyValueFilter) : null, |
| 157 | + pagination: $pagination ? Pagination::fromArray($pagination) : null |
| 158 | + ); |
| 159 | + |
| 160 | + /** @var Node $contextNode */ |
| 161 | + foreach ($flowQuery->getContext() as $contextNode) { |
| 162 | + $subgraph = $this->contentRepositoryRegistry->subgraphForNode($contextNode); |
| 163 | + foreach ($subgraph->findDescendantNodes($contextNode->aggregateId, $findDescendentNodesFilter) as $descendant) { |
| 164 | + $result[] = $descendant; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + $flowQuery->setContext($result); |
| 169 | + } |
| 170 | +} |
0 commit comments