/**
* {@inheritdoc}
*
* @param FlowQuery $flowQuery
* @param array $arguments
* @return void
*/
public function evaluate(FlowQuery $flowQuery, array $arguments)
{
$subject = $arguments[0];
if (!isset($subject) || empty($subject)) {
$flowQuery->setContext(array());
return;
}
$filteredContext = array();
$context = $flowQuery->getContext();
if (is_string($subject)) {
foreach ($context as $contextElement) {
$contextElementQuery = new FlowQuery(array($contextElement));
$contextElementQuery->pushOperation('children', $arguments);
if ($contextElementQuery->count() > 0) {
$filteredContext[] = $contextElement;
}
}
} else {
if ($subject instanceof FlowQuery) {
$elements = $subject->get();
} elseif ($subject instanceof \Traversable) {
$elements = iterator_to_array($subject);
} elseif (is_object($subject)) {
$elements = array($subject);
} elseif (is_array($subject)) {
$elements = $subject;
} else {
throw new FizzleException('supplied argument for has operation not supported', 1332489625);
}
foreach ($elements as $element) {
if ($element instanceof NodeInterface) {
$parentsQuery = new FlowQuery(array($element));
/** @var NodeInterface $parent */
foreach ($parentsQuery->parents(array())->get() as $parent) {
/** @var NodeInterface $contextElement */
foreach ($context as $contextElement) {
if ($contextElement->getIdentifier() === $parent->getIdentifier()) {
$filteredContext[] = $contextElement;
}
}
}
}
}
$filteredContext = array_unique($filteredContext);
}
$flowQuery->setContext($filteredContext);
}