"Fossies" - the Fresh Open Source Software Archive

Member "neos-development-collection-7.0.1/Neos.ContentRepository/Classes/Eel/FlowQueryOperations/HasOperation.php" (23 Feb 2021, 3767 Bytes) of package /linux/www/neos-development-collection-7.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "HasOperation.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 namespace Neos\ContentRepository\Eel\FlowQueryOperations;
    3 
    4 /*
    5  * This file is part of the Neos.ContentRepository package.
    6  *
    7  * (c) Contributors of the Neos Project - www.neos.io
    8  *
    9  * This package is Open Source Software. For the full copyright and license
   10  * information, please view the LICENSE file which was distributed with this
   11  * source code.
   12  */
   13 
   14 use Neos\ContentRepository\Domain\Projection\Content\TraversableNodeInterface;
   15 use Neos\Eel\FlowQuery\FizzleException;
   16 use Neos\Eel\FlowQuery\FlowQuery;
   17 use Neos\Eel\FlowQuery\Operations\AbstractOperation;
   18 
   19 /**
   20  * "has" operation working on NodeInterface. Reduce the set of matched elements
   21  * to those that have a child node that matches the selector or given subject.
   22  *
   23  * Accepts a selector, an array, an object, a traversable object & a FlowQuery
   24  * object as argument.
   25  */
   26 class HasOperation extends AbstractOperation
   27 {
   28     /**
   29      * {@inheritdoc}
   30      *
   31      * @var string
   32      */
   33     protected static $shortName = 'has';
   34 
   35     /**
   36      * {@inheritdoc}
   37      *
   38      * @var integer
   39      */
   40     protected static $priority = 100;
   41 
   42     /**
   43      * {@inheritdoc}
   44      *
   45      * @param array (or array-like object) $context onto which this operation should be applied
   46      * @return boolean true if the operation can be applied onto the $context, false otherwise
   47      */
   48     public function canEvaluate($context)
   49     {
   50         return count($context) === 0 || (isset($context[0]) && ($context[0] instanceof TraversableNodeInterface));
   51     }
   52 
   53     /**
   54      * {@inheritdoc}
   55      *
   56      * @param FlowQuery $flowQuery
   57      * @param array $arguments
   58      * @return void
   59      * @throws FizzleException
   60      * @throws \Neos\Eel\Exception
   61      */
   62     public function evaluate(FlowQuery $flowQuery, array $arguments)
   63     {
   64         $subject = $arguments[0];
   65         if (!isset($subject) || empty($subject)) {
   66             $flowQuery->setContext([]);
   67             return;
   68         }
   69 
   70         $filteredContext = [];
   71         $context = $flowQuery->getContext();
   72         if (is_string($subject)) {
   73             foreach ($context as $contextElement) {
   74                 $contextElementQuery = new FlowQuery([$contextElement]);
   75                 $contextElementQuery->pushOperation('children', $arguments);
   76                 if ($contextElementQuery->count() > 0) {
   77                     $filteredContext[] = $contextElement;
   78                 }
   79             }
   80         } else {
   81             if ($subject instanceof FlowQuery) {
   82                 $elements = $subject->get();
   83             } elseif ($subject instanceof \Traversable) {
   84                 $elements = iterator_to_array($subject);
   85             } elseif (is_object($subject)) {
   86                 $elements = [$subject];
   87             } elseif (is_array($subject)) {
   88                 $elements = $subject;
   89             } else {
   90                 throw new FizzleException('supplied argument for has operation not supported', 1332489625);
   91             }
   92             foreach ($elements as $element) {
   93                 if ($element instanceof TraversableNodeInterface) {
   94                     $parentsQuery = new FlowQuery([$element]);
   95                     foreach ($parentsQuery->parents([])->get() as $parent) {
   96                         /** @var TraversableNodeInterface $parent */
   97                         foreach ($context as $contextElement) {
   98                             /** @var TraversableNodeInterface $contextElement */
   99                             if ($contextElement === $parent) {
  100                                 $filteredContext[] = $contextElement;
  101                             }
  102                         }
  103                     }
  104                 }
  105             }
  106             $filteredContext = array_unique($filteredContext);
  107         }
  108 
  109         $flowQuery->setContext($filteredContext);
  110     }
  111 }