/
FilterOperation.php
154 lines (143 loc) · 4.7 KB
/
FilterOperation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Neos\ContentRepository\NodeAccess\FlowQueryOperations;
/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Utility\ObjectAccess;
/**
* This filter implementation contains specific behavior for use on ContentRepository
* nodes. It will not evaluate any elements that are not instances of the
* `Node`.
*
* The implementation changes the behavior of the `instanceof` operator to
* work on node types instead of PHP object types, so that::
*
* [instanceof Neos.NodeTypes:Page]
*
* will in fact use `isOfType()` on the `NodeType` of context elements to
* filter. This filter allow also to filter the current context by a given
* node. Anything else remains unchanged.
*/
class FilterOperation extends \Neos\Eel\FlowQuery\Operations\Object\FilterOperation
{
/**
* {@inheritdoc}
*
* @var integer
*/
protected static $priority = 100;
/**
* {@inheritdoc}
*
* @param array $context $context onto which this operation should be applied (array or array-like object)
* @return boolean true if the operation can be applied onto the $context, false otherwise
*/
public function canEvaluate($context)
{
return (isset($context[0]) && ($context[0] instanceof Node));
}
/**
* {@inheritdoc}
*
* @param FlowQuery $flowQuery
* @param array $arguments
* @return void
*/
public function evaluate(FlowQuery $flowQuery, array $arguments)
{
if (!isset($arguments[0]) || empty($arguments[0])) {
return;
}
if ($arguments[0] instanceof Node) {
$filteredContext = [];
$context = $flowQuery->getContext();
foreach ($context as $element) {
if ($element === $arguments[0]) {
$filteredContext[] = $element;
break;
}
}
$flowQuery->setContext($filteredContext);
} else {
parent::evaluate($flowQuery, $arguments);
}
}
/**
* {@inheritdoc}
*
* @param object $element
* @param string $propertyNameFilter
* @return boolean true if the property name filter matches
*/
protected function matchesPropertyNameFilter($element, $propertyNameFilter)
{
/* @var Node $element */
try {
return ((string)$element->nodeName === $propertyNameFilter);
} catch (\InvalidArgumentException $e) {
// in case the Element has no valid node name, we do not match!
return false;
}
}
/**
* {@inheritdoc}
*
* @param Node $element
* @param string $identifier
* @return boolean
*/
protected function matchesIdentifierFilter($element, $identifier)
{
return (strtolower($element->nodeAggregateId->value) === strtolower($identifier));
}
/**
* {@inheritdoc}
*
* @param Node $element
* @param string $propertyPath
* @return mixed
*/
protected function getPropertyPath($element, $propertyPath)
{
if ($propertyPath === '_identifier') {
// TODO: deprecated (Neos <9 case)
return $element->nodeAggregateId->value;
} elseif ($propertyPath[0] === '_' && $propertyPath !== '_hiddenInIndex') {
return ObjectAccess::getPropertyPath($element, substr($propertyPath, 1));
} else {
return $element->getProperty($propertyPath);
}
}
/**
* {@inheritdoc}
*
* @param mixed $value
* @param string $operator
* @param mixed $operand
* @return boolean
*/
protected function evaluateOperator($value, $operator, $operand)
{
if ($operator === 'instanceof' && $value instanceof Node) {
if ($this->operandIsSimpleType($operand)) {
return $this->handleSimpleTypeOperand($operand, $value);
} elseif ($operand === Node::class) {
return true;
} else {
$isOfType = $value->nodeType->isOfType($operand[0] === '!' ? substr($operand, 1) : $operand);
return $operand[0] === '!' ? $isOfType === false : $isOfType;
}
} elseif ($operator === '!instanceof' && $value instanceof Node) {
return !$this->evaluateOperator($value, 'instanceof', $operand);
}
return parent::evaluateOperator($value, $operator, $operand);
}
}