/** * Parse the expression * * @see library/ODataProducer/QueryProcessor/ODataProducer\QueryProcessor. * ExpressionParser::parseFilter() * * @return AbstractExpression * * @throws ODataException */ public function parseFilter() { $expression = parent::parseFilter(); if (!$expression->typeIs(new Boolean())) { ODataException::createSyntaxError(Messages::expressionParser2BooleanRequired()); } if (!$this->_isCustomExpressionProvider) { $resultExpression = $this->_processNodeForNullability($expression, null); if ($resultExpression != null) { return $resultExpression; } } return $expression; }
public function testFunctionCallExpression() { try { $expression = 'year(datetime\'1988-11-11\')'; $parser = new ExpressionParser($expression, $this->_northWindMetadata->resolveResourceSet('Customers')->getResourceType(), false); $expr = $parser->parseFilter(); $this->AssertEquals($expr instanceof FunctionCallExpression, true); $this->AssertEquals($expr->getType() instanceof Int32, true); $expression = "substring('pspl', 1) eq 'pl'"; $parser->resetParser($expression); $expr = $parser->parseFilter(); $this->AssertEquals($expr instanceof RelationalExpression, true); $this->AssertEquals($expr->getLeft() instanceof FunctionCallExpression, true); $this->AssertEquals($expr->getLeft()->getFunctionDescription()->functionName, 'strcmp'); $paramExpressions = $expr->getLeft()->getParamExpressions(); $this->AssertEquals(count($paramExpressions), 2); $this->AssertEquals($paramExpressions[0] instanceof FunctionCallExpression, true); $this->AssertEquals($paramExpressions[0]->getFunctionDescription()->functionName, 'substring'); $paramExpressions1 = $paramExpressions[0]->getParamExpressions(); $this->AssertEquals(count($paramExpressions1), 2); $this->AssertEquals($paramExpressions1[0] instanceof ConstantExpression, true); $this->AssertEquals($paramExpressions1[0]->getValue(), '\'pspl\''); $exceptionThrown = false; try { $expression = 'unknownFun(1, 3)'; $parser->resetParser($expression); $expr = $parser->parseFilter(); } catch (ODataException $exception) { $this->assertStringStartsWith('Unknown function \'unknownFun\' at position 0', $exception->getMessage()); $exceptionThrown = true; } if (!$exceptionThrown) { $this->fail('An expected ODataException for and unknown function was not thrown'); } $exceptionThrown = false; try { $expression = 'endswith(\'mystring\''; $parser->resetParser($expression); $expr = $parser->parseFilter(); } catch (ODataException $exception) { $this->assertStringStartsWith('Close parenthesis expected', $exception->getMessage()); $exceptionThrown = true; } if (!$exceptionThrown) { $this->fail('An expected ODataException for function without closing bracket was not thrown'); } $exceptionThrown = false; try { $expression = 'trim()'; $parser->resetParser($expression); $expr = $parser->parseFilter(); } 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()); $exceptionThrown = true; } if (!$exceptionThrown) { $this->fail('An expected ODataException for \'No applicable function found\' was not thrown for trim'); } $exceptionThrown = false; try { $expression = 'month(123.4)'; $parser->resetParser($expression); $expr = $parser->parseFilter(); } catch (ODataException $exception) { $this->assertStringStartsWith('No applicable function found for \'month\' at position', $exception->getMessage()); $exceptionThrown = true; } if (!$exceptionThrown) { $this->fail('An expected ODataException for \'No applicable function found\' was not thrown for month'); } } catch (ODataException $exception) { $this->fail('An unexpected ODataException has been raised'); } }