-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Add
PropertyValueCriteriaMatcher
The property value criteria matcher accepts a ``PropertyValueCriteriaInterface`` and provides static methods to check wether a ``Node`` or a ``PropertyCollection`` matches those criteria.
- Loading branch information
Showing
3 changed files
with
423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ore/Classes/Projection/ContentGraph/Filter/PropertyValue/PropertyValueCriteriaMatcher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue; | ||
|
||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\AndCriteria; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\NegateCriteria; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\OrCriteria; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueContains; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueCriteriaInterface; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueEndsWith; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueEquals; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueGreaterThan; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueGreaterThanOrEqual; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueLessThan; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueLessThanOrEqual; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\PropertyValue\Criteria\PropertyValueStartsWith; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Node; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\PropertyCollection; | ||
|
||
/** | ||
* Performs property checks against a given set of constraints | ||
* | ||
* @internal | ||
*/ | ||
final class PropertyValueCriteriaMatcher | ||
{ | ||
public static function matchesPropertyCollection(PropertyCollection $propertyCollection, PropertyValueCriteriaInterface $propertyValueCriteria): bool | ||
{ | ||
switch (true) { | ||
case $propertyValueCriteria instanceof AndCriteria: | ||
return self::matchesPropertyCollection($propertyCollection, $propertyValueCriteria->criteria1) && self::matchesPropertyCollection($propertyCollection, $propertyValueCriteria->criteria2); | ||
case $propertyValueCriteria instanceof OrCriteria: | ||
return self::matchesPropertyCollection($propertyCollection, $propertyValueCriteria->criteria1) || self::matchesPropertyCollection($propertyCollection, $propertyValueCriteria->criteria2); | ||
case $propertyValueCriteria instanceof NegateCriteria: | ||
return !self::matchesPropertyCollection($propertyCollection, $propertyValueCriteria->criteria); | ||
case $propertyValueCriteria instanceof PropertyValueContains: | ||
$propertyValue = $propertyCollection->serialized()->getProperty($propertyValueCriteria->propertyName->value)->value; | ||
return is_string($propertyValue) | ||
? str_contains((string)$propertyValue, $propertyValueCriteria->value) | ||
: false; | ||
case $propertyValueCriteria instanceof PropertyValueEndsWith: | ||
$propertyValue = $propertyCollection->serialized()->getProperty($propertyValueCriteria->propertyName->value)->value; | ||
return is_string($propertyValue) ? | ||
str_ends_with((string)$propertyValue, $propertyValueCriteria->value) | ||
: false; | ||
case $propertyValueCriteria instanceof PropertyValueStartsWith: | ||
$propertyValue = $propertyCollection->serialized()->getProperty($propertyValueCriteria->propertyName->value)->value; | ||
return (is_string($propertyValue) || $propertyValue instanceof \Stringable) | ||
? str_starts_with((string)$propertyValue, $propertyValueCriteria->value) | ||
: false; | ||
case $propertyValueCriteria instanceof PropertyValueEquals: | ||
return $propertyCollection->serialized()->propertyExists($propertyValueCriteria->propertyName->value) | ||
&& $propertyCollection->serialized()->getProperty($propertyValueCriteria->propertyName->value)->value == $propertyValueCriteria->value; | ||
case $propertyValueCriteria instanceof PropertyValueGreaterThan: | ||
return $propertyCollection->serialized()->propertyExists($propertyValueCriteria->propertyName->value) | ||
&& $propertyCollection->serialized()->getProperty($propertyValueCriteria->propertyName->value)->value > $propertyValueCriteria->value; | ||
case $propertyValueCriteria instanceof PropertyValueGreaterThanOrEqual: | ||
return $propertyCollection->serialized()->propertyExists($propertyValueCriteria->propertyName->value) | ||
&& $propertyCollection->serialized()->getProperty($propertyValueCriteria->propertyName->value)->value >= $propertyValueCriteria->value; | ||
case $propertyValueCriteria instanceof PropertyValueLessThan: | ||
return $propertyCollection->serialized()->propertyExists($propertyValueCriteria->propertyName->value) | ||
&& $propertyCollection->serialized()->getProperty($propertyValueCriteria->propertyName->value)->value < $propertyValueCriteria->value; | ||
case $propertyValueCriteria instanceof PropertyValueLessThanOrEqual: | ||
return $propertyCollection->serialized()->propertyExists($propertyValueCriteria->propertyName->value) | ||
&& $propertyCollection->serialized()->getProperty($propertyValueCriteria->propertyName->value)->value <= $propertyValueCriteria->value; | ||
default: | ||
throw new \InvalidArgumentException(sprintf('Invalid/unsupported property value criteria "%s"', get_debug_type($propertyValueCriteria)), 1679561073); | ||
} | ||
} | ||
|
||
public static function matchesNode(Node $node, PropertyValueCriteriaInterface $propertyValueCriteria): bool | ||
{ | ||
return static::matchesPropertyCollection($node->properties, $propertyValueCriteria); | ||
} | ||
} |
Oops, something went wrong.