/** * Create a Statusdat expression from a Tree node * * @param Node $node The node to convert to an expression * @param Filterable $source The filterable to use for field mapping * * @return IQueryPart Either a statusdat expression or an expression group */ private function nodeToQuery(Node $node, Filterable $source) { if ($node->type === Node::TYPE_OPERATOR) { $op = $node->operator; $node->left = $source->getMappedField($node->left); $op = 'IN'; $values = $node->right; if ($node->operator === NODE::OPERATOR_EQUALS_NOT) { $op = 'NOT_' . $op; } foreach ($values as &$value) { $value = str_replace('*', '%', $value); } $values = array($values); return new Expression($node->left . ' ' . $op . ' ? ', $values); } else { $group = new Group(); $group->setType($node->type === Node::TYPE_OR ? Group::TYPE_OR : Group::TYPE_AND); $group->addItem($this->nodeToQuery($node->left, $source)); $group->addItem($this->nodeToQuery($node->right, $source)); return $group; } }
/** * Remove all tree nodes that are not applicable ot the given Filterable * * @param Node $node The root node to use * @param Filterable $filter The Filterable to test nodes against * @return Node The normalized tree node */ public function removeInvalidFilter($node, Filterable $filter) { if ($node === null) { return $node; } if ($node->type === Node::TYPE_OPERATOR) { if (!$filter->isValidFilterTarget($node->left)) { return null; } else { return $node; } } $node->left = $this->removeInvalidFilter($node->left, $filter); $node->right = $this->removeInvalidFilter($node->right, $filter); if ($node->left || $node->right) { if (!$node->left) { $node->left = $node->right; $node->right = null; } return $node; } return null; }