/** * Add a filter to this query * * This is the implementation for the Filterable, use where instead * * @param $filter */ public function addFilter($filter) { if (is_string($filter)) { $this->addFilter(call_user_func_array(array($this, 'parseFilterExpression'), func_get_args())); } elseif ($filter instanceof Node) { $this->filter->insert($filter); } }
/** * Create a query tree containing this filter * * Query parts that couldn't be parsed can be retrieved with Filter::getIgnoredQueryParts * * @param String $query The query string to parse into a query tree * @return Tree The resulting query tree (empty for invalid queries) */ public function createQueryTreeForFilter($query) { $this->ignoredQueryParts = array(); $right = $query; $domain = null; $tree = new Tree(); do { list($left, $conjunction, $right) = $this->splitQueryAtNextConjunction($right); $domain = $this->getFirstDomainForQuery($left); if ($domain === null) { $this->ignoredQueryParts[] = $left; continue; } $node = $domain->convertToTreeNode($left); if (!$node) { $this->ignoredQueryParts[] = $left; continue; } $tree->insert($node); if ($conjunction === 'AND') { $tree->insert(Node::createAndNode()); } elseif ($conjunction === 'OR') { $tree->insert(Node::createOrNode()); } } while ($right !== null); return $tree; }
/** * Copy the given node or branch into the given tree * * @param Node $node The node to copy * @param Tree $tree The tree to insert the copied node and it's subnodes to */ private function copyBranch(Node $node, Tree &$tree) { if ($node->type === Node::TYPE_OPERATOR) { $copy = Node::createOperatorNode($node->operator, $node->left, $node->right); $copy->context = $node->context; $tree->insert($copy); } else { if ($node->left) { $this->copyBranch($node->left, $tree); } $tree->insert($node->type === Node::TYPE_OR ? Node::createOrNode() : Node::createAndNode()); if ($node->right) { $this->copyBranch($node->right, $tree); } } }