/**
  * {@inheritdoc}
  *
  * @param FlowQuery $flowQuery the FlowQuery object
  * @param array $arguments the arguments for this operation
  * @return void
  */
 public function evaluate(FlowQuery $flowQuery, array $arguments)
 {
     $output = array();
     $outputNodePaths = array();
     $until = array();
     foreach ($flowQuery->getContext() as $contextNode) {
         $nextNodes = $this->getNextForNode($contextNode);
         if (isset($arguments[0]) && !empty($arguments[0])) {
             $untilQuery = new FlowQuery($nextNodes);
             $untilQuery->pushOperation('filter', array($arguments[0]));
             $until = $untilQuery->get();
         }
         if (isset($until[0]) && !empty($until[0])) {
             $nextNodes = $this->getNodesUntil($nextNodes, $until[0]);
         }
         if (is_array($nextNodes)) {
             foreach ($nextNodes as $nextNode) {
                 if ($nextNode !== null && !isset($outputNodePaths[$nextNode->getPath()])) {
                     $outputNodePaths[$nextNode->getPath()] = true;
                     $output[] = $nextNode;
                 }
             }
         }
     }
     $flowQuery->setContext($output);
     if (isset($arguments[1]) && !empty($arguments[1])) {
         $flowQuery->pushOperation('filter', array($arguments[1]));
     }
 }
 /**
  * {@inheritdoc}
  *
  * @param FlowQuery $flowQuery the FlowQuery object
  * @param array $arguments the arguments for this operation
  * @return void
  */
 public function evaluate(FlowQuery $flowQuery, array $arguments)
 {
     $output = array();
     $outputNodePaths = array();
     foreach ($flowQuery->getContext() as $contextNode) {
         $siteNode = $contextNode->getContext()->getCurrentSiteNode();
         $parentNodes = $this->getParents($contextNode, $siteNode);
         if (isset($arguments[0]) && !empty($arguments[0] && isset($parentNodes[0]))) {
             $untilQuery = new FlowQuery(array($parentNodes[0]));
             $untilQuery->pushOperation('closest', array($arguments[0]));
             $until = $untilQuery->get();
         }
         if (isset($until) && is_array($until) && !empty($until) && isset($until[0])) {
             $parentNodes = $this->getNodesUntil($parentNodes, $until[0]);
         }
         if (is_array($parentNodes)) {
             foreach ($parentNodes as $parentNode) {
                 if ($parentNode !== null && !isset($outputNodePaths[$parentNode->getPath()])) {
                     $outputNodePaths[$parentNode->getPath()] = true;
                     $output[] = $parentNode;
                 }
             }
         }
     }
     $flowQuery->setContext($output);
     if (isset($arguments[1]) && !empty($arguments[1])) {
         $flowQuery->pushOperation('filter', $arguments[1]);
     }
 }
 /**
  * Optimize for typical use cases, filter by node name and filter
  * by NodeType (instanceof). These cases are now optimized and will
  * only load the nodes that match the filters.
  *
  * @param FlowQuery $flowQuery
  * @param array $parsedFilter
  * @return boolean
  */
 protected function earlyOptimizationOfFilters(FlowQuery $flowQuery, array $parsedFilter)
 {
     $optimized = false;
     $output = array();
     $outputNodePaths = array();
     foreach ($parsedFilter['Filters'] as $filter) {
         $instanceOfFilters = array();
         $attributeFilters = array();
         if (isset($filter['AttributeFilters'])) {
             foreach ($filter['AttributeFilters'] as $attributeFilter) {
                 if ($attributeFilter['Operator'] === 'instanceof' && $attributeFilter['Identifier'] === null) {
                     $instanceOfFilters[] = $attributeFilter;
                 } else {
                     $attributeFilters[] = $attributeFilter;
                 }
             }
         }
         // Only apply optimization if there's a property name filter or a instanceof filter or another filter already did optimization
         if (isset($filter['PropertyNameFilter']) || isset($filter['PathFilter']) || count($instanceOfFilters) > 0 || $optimized === true) {
             $optimized = true;
             $filteredOutput = array();
             $filteredOutputNodePaths = array();
             // Optimize property name filter if present
             if (isset($filter['PropertyNameFilter']) || isset($filter['PathFilter'])) {
                 $nodePath = isset($filter['PropertyNameFilter']) ? $filter['PropertyNameFilter'] : $filter['PathFilter'];
                 /** @var NodeInterface $contextNode */
                 foreach ($flowQuery->getContext() as $contextNode) {
                     $childNode = $contextNode->getNode($nodePath);
                     if ($childNode !== null && !isset($filteredOutputNodePaths[$childNode->getPath()])) {
                         $filteredOutput[] = $childNode;
                         $filteredOutputNodePaths[$childNode->getPath()] = true;
                     }
                 }
             } elseif (count($instanceOfFilters) > 0) {
                 // Optimize node type filter if present
                 $allowedNodeTypes = array_map(function ($instanceOfFilter) {
                     return $instanceOfFilter['Operand'];
                 }, $instanceOfFilters);
                 /** @var NodeInterface $contextNode */
                 foreach ($flowQuery->getContext() as $contextNode) {
                     /** @var NodeInterface $childNode */
                     foreach ($contextNode->getChildNodes(implode($allowedNodeTypes, ',')) as $childNode) {
                         if (!isset($filteredOutputNodePaths[$childNode->getPath()])) {
                             $filteredOutput[] = $childNode;
                             $filteredOutputNodePaths[$childNode->getPath()] = true;
                         }
                     }
                 }
             }
             // Apply attribute filters if present
             if (isset($filter['AttributeFilters'])) {
                 $attributeFilters = array_reduce($filter['AttributeFilters'], function ($filters, $attributeFilter) {
                     return $filters . $attributeFilter['text'];
                 });
                 $filteredFlowQuery = new FlowQuery($filteredOutput);
                 $filteredFlowQuery->pushOperation('filter', array($attributeFilters));
                 $filteredOutput = $filteredFlowQuery->get();
             }
             // Add filtered nodes to output
             foreach ($filteredOutput as $filteredNode) {
                 if (!isset($outputNodePaths[$filteredNode->getPath()])) {
                     $output[] = $filteredNode;
                 }
             }
         }
     }
     if ($optimized === true) {
         $flowQuery->setContext($output);
     }
     return $optimized;
 }
 /**
  * {@inheritdoc}
  *
  * @param FlowQuery $flowQuery the FlowQuery object
  * @param array $arguments the arguments for this operation
  * @return void
  */
 public function evaluate(FlowQuery $flowQuery, array $arguments)
 {
     $context = $flowQuery->getContext();
     if (!isset($context[0]) || empty($arguments[0])) {
         return;
     }
     $result = array();
     $selectorAndFilter = $arguments[0];
     $parsedFilter = null;
     $parsedFilter = FizzleParser::parseFilterGroup($selectorAndFilter);
     if (isset($parsedFilter['Filters']) && $this->hasOnlyInstanceOfFilters($parsedFilter['Filters'])) {
         $nodeTypes = array();
         foreach ($parsedFilter['Filters'] as $filter) {
             $nodeTypes[] = $filter['AttributeFilters'][0]['Operand'];
         }
         /** @var NodeInterface $contextNode */
         foreach ($context as $contextNode) {
             $result = array_merge($result, $this->nodeDataRepository->findByParentAndNodeTypeInContext($contextNode->getPath(), implode(',', $nodeTypes), $contextNode->getContext(), true));
         }
     } else {
         foreach ($parsedFilter['Filters'] as $filter) {
             $filterResults = array();
             $generatedNodes = false;
             if (isset($filter['IdentifierFilter'])) {
                 if (!preg_match(UuidValidator::PATTERN_MATCH_UUID, $filter['IdentifierFilter'])) {
                     throw new FlowQueryException('find() requires a valid identifier', 1332492263);
                 }
                 /** @var NodeInterface $contextNode */
                 foreach ($context as $contextNode) {
                     $filterResults = array($contextNode->getContext()->getNodeByIdentifier($filter['IdentifierFilter']));
                 }
                 $generatedNodes = true;
             } elseif (isset($filter['PropertyNameFilter']) || isset($filter['PathFilter'])) {
                 $nodePath = isset($filter['PropertyNameFilter']) ? $filter['PropertyNameFilter'] : $filter['PathFilter'];
                 foreach ($context as $contextNode) {
                     $node = $contextNode->getNode($nodePath);
                     if ($node !== null) {
                         array_push($filterResults, $node);
                     }
                 }
                 $generatedNodes = true;
             }
             if (isset($filter['AttributeFilters']) && $filter['AttributeFilters'][0]['Operator'] === 'instanceof') {
                 foreach ($context as $contextNode) {
                     $filterResults = array_merge($filterResults, $this->nodeDataRepository->findByParentAndNodeTypeInContext($contextNode->getPath(), $filter['AttributeFilters'][0]['Operand'], $contextNode->getContext(), true));
                 }
                 unset($filter['AttributeFilters'][0]);
                 $generatedNodes = true;
             }
             if (isset($filter['AttributeFilters']) && count($filter['AttributeFilters']) > 0) {
                 if (!$generatedNodes) {
                     throw new FlowQueryException('find() needs an identifier, path or instanceof filter for the first filter part', 1436884196);
                 }
                 $filterQuery = new FlowQuery($filterResults);
                 foreach ($filter['AttributeFilters'] as $attributeFilter) {
                     $filterQuery->pushOperation('filter', array($attributeFilter['text']));
                 }
                 $filterResults = $filterQuery->get();
             }
             $result = array_merge($result, $filterResults);
         }
     }
     $flowQuery->setContext(array_unique($result));
 }