/** * Generate the filter objects corresponding to a UQL string. * * @param $UQLStringInput * * @return Filter */ public function generateFilters($UQLStringInput) { if (empty(trim($UQLStringInput))) { return new Filter(); } // Get the Abstract Syntax Tree of the input from the parser $parser = new Parser(); $AST = $parser->parse($UQLStringInput); // Recursively translate into filters. $filters = $this->buildFilterLevel($AST); if ($filters instanceof FilterCondition) { // Single filter. Wrap into dummy filter collection for consistency. $filterDefinition = new Filter(); $filterDefinition[] = $filters; $filters = $filterDefinition; } return $filters; }
/** * Tests the functionality of the array matcher */ public function testMatchArray() { $testArrays = ["[1, 2, 3, 4, 5]" => [1, 2, 3, 4, 5], "[1, \"abc\", 2, \"def\"]" => [1, "\"abc\"", 2, "\"def\""]]; $parser = new Parser(); foreach ($testArrays as $testArray => $expectedResult) { $tokenStream = Lexer::lex($testArray); $parser->setTokenStream($tokenStream); $parser->setTokenIndex(-1); $array = $parser->matchArray(); $this->assertNotEquals($array, false, 'Array should not be false (meaning it did interpret an array)'); $this->assertTrue($array instanceof ASTArray, 'Arrays should Parse into ASTArrays'); $this->assertCount(count($expectedResult), $array->getElements(), 'Array doesn\'t match the expected number of items'); foreach ($array->getElements() as $index => $element) { $this->assertEquals($expectedResult[$index], $element, "Element '{$element}' on index {$index} doesn't match the expected {$expectedResult[$index]}"); } } }