/** * Parse the expression * * @see library/POData/QueryProcessor/ExpressionParser::parseFilter() * * @return AbstractExpression * * @throws ODataException */ public function parseFilter() { $expression = parent::parseFilter(); if (!$expression->typeIs(new Boolean())) { throw ODataException::createSyntaxError(Messages::expressionParser2BooleanRequired()); } if ($this->_isPHPExpressionProvider) { $resultExpression = $this->_processNodeForNullability($expression, null); if ($resultExpression != null) { return $resultExpression; } } return $expression; }
public function testFunctionCallExpression() { $expression = 'year(datetime\'1988-11-11\')'; $parser = new ExpressionParser($expression, $this->customersResourceType, false); $expr = $parser->parseFilter(); $this->assertTrue($expr instanceof FunctionCallExpression); $this->assertTrue($expr->getType() instanceof Int32); $expression = "substring('pspl', 1) eq 'pl'"; $parser->resetParser($expression); $expr = $parser->parseFilter(); $this->assertTrue($expr instanceof RelationalExpression); /** @var RelationalExpression $expr */ $this->assertTrue($expr->getLeft() instanceof FunctionCallExpression); $this->assertEquals('strcmp', $expr->getLeft()->getFunctionDescription()->name); $paramExpressions = $expr->getLeft()->getParamExpressions(); $this->assertEquals(2, count($paramExpressions)); $this->assertTrue($paramExpressions[0] instanceof FunctionCallExpression); $this->assertEquals('substring', $paramExpressions[0]->getFunctionDescription()->name); $paramExpressions1 = $paramExpressions[0]->getParamExpressions(); $this->assertEquals(2, count($paramExpressions1)); $this->assertTrue($paramExpressions1[0] instanceof ConstantExpression); $this->assertEquals("'pspl'", $paramExpressions1[0]->getValue()); $expression = 'unknownFun(1, 3)'; $parser->resetParser($expression); try { $expr = $parser->parseFilter(); $this->fail('An expected ODataException for and unknown function was not thrown'); } catch (ODataException $exception) { $this->assertStringStartsWith('Unknown function \'unknownFun\' at position 0', $exception->getMessage()); } $expression = 'endswith(\'mystring\''; $parser->resetParser($expression); try { $expr = $parser->parseFilter(); $this->fail('An expected ODataException for function without closing bracket was not thrown'); } catch (ODataException $exception) { $this->assertStringStartsWith('Close parenthesis expected', $exception->getMessage()); } $expression = 'trim()'; $parser->resetParser($expression); try { $expr = $parser->parseFilter(); $this->fail('An expected ODataException for \'No applicable function found\' was not thrown for trim'); } catch (ODataException $exception) { $this->assertStringStartsWith('No applicable function found for \'trim\' at position 0 with the specified arguments. The functions considered are: Edm.String trim(Edm.String)', $exception->getMessage()); } $expression = 'month(123.4)'; $parser->resetParser($expression); try { $expr = $parser->parseFilter(); $this->fail('An expected ODataException for \'No applicable function found\' was not thrown for month'); } catch (ODataException $exception) { $this->assertStringStartsWith('No applicable function found for \'month\' at position', $exception->getMessage()); } }