* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Protocol\Statusdat; use Icinga\Filter\Query\Node; use Icinga\Filter\Query\Tree; use Icinga\Protocol\Statusdat\Query\Expression; use Icinga\Protocol\Statusdat\Query\Group; class TreeToStatusdatQueryParser { private function nodeToQuery(Node $node) { if ($node->type === Node::TYPE_OPERATOR) { $op = $node->operator; $value = $node->right; if (stripos($node->right, '*') !== false) { $op = 'LIKE'; $value = str_replace('*', '%', $value); } return new Expression($node->left . ' ' . $op . ' ?', $value); } else { $group = new Group(); $group->setType(($node->type === Node::TYPE_OR) ? Group::TYPE_OR : Group::TYPE_AND); $group->addItem($this->nodeToQuery($node->left)); $group->addItem($this->nodeToQuery($node->right)); return $group; } } public function treeToQuery(Tree $tree) { if ($tree->root !== null) { $tree->root = $tree->normalizeTree($tree->root); return $this->nodeToQuery($tree->root); } return null; } }