/** * Parse and generate PHP or custom (using custom expression provider) expression * from the the given odata expression. * * @param string $text The text expression to parse * @param ResourceType $resourceType The resource type in which * expression will be applied * @param IExpressionProvider $expressionProvider Implementation of IExpressionProvider * if developer is using IDSQP2, null * in-case of IDSQP which falls to default * expression provider that is PHP expression * provider * * @return InternalFilterInfo * * @throws ODataException If any error occurs while parsing the odata * expression or building the php/custom expression. */ public static function parseExpression2($text, ResourceType $resourceType, $expressionProvider) { $isCustomExpressionProvider = !is_null($expressionProvider); $expressionParser2 = new ExpressionParser2($text, $resourceType, $isCustomExpressionProvider); $expressionTree = null; try { $expressionTree = $expressionParser2->parseFilter(); } catch (ODataException $odataException) { throw $odataException; } $expressionProcessor = null; $expressionAsString = null; $filterFunction = null; if (!$isCustomExpressionProvider) { $expressionProvider = new PHPExpressionProvider('$lt'); } $expressionProvider->setResourceType($resourceType); $expressionProcessor = new ExpressionProcessor($expressionTree, $expressionProvider); try { $expressionAsString = $expressionProcessor->processExpression(); } catch (\InvalidArgumentException $invalidArgumentException) { ODataException::createInternalServerError($invalidArgumentException->getMessage()); } $navigationPropertiesUsed = empty($expressionParser2->_navigationPropertiesUsedInTheExpression) ? null : $expressionParser2->_navigationPropertiesUsedInTheExpression; unset($expressionProcessor); unset($expressionParser2); if ($isCustomExpressionProvider) { $filterFunction = new AnonymousFunction(array(), ' ODataException::createInternalServerError("Library will not perform filtering in case of custom IExpressionProvider"); '); } else { $filterFunction = new AnonymousFunction(array('$lt'), 'if(' . $expressionAsString . ') { return true; } else { return false;}'); } return new InternalFilterInfo(new FilterInfo($navigationPropertiesUsed), $filterFunction, $expressionAsString, $isCustomExpressionProvider); }
/** * Parse the astoria filter expression, generate the same expression as PHP expression, * retrieve only the entries which satisifes this expression. * * @param string $astoriaFilter * @param ResourceType $resourceType * @param array<objects> $entries * * @return array<objects> */ private function executeExpression($astoriaFilter, $resourceType, $entries) { try { //Parse the Astoria filter query option to expression tree $parser = new ExpressionParser2($astoriaFilter, $resourceType, null); $expressionTree = $parser->parseFilter(); //emit the PHP expression corrosponds to Astoria filter query $expressionProcessor = new ExpressionProcessor($expressionTree, new PHPExpressionProvider('$lt')); $phpExpression = $expressionProcessor->processExpression(); //create an anonymous function with the generated PHP expression in if condition $fun = create_function('$lt', 'if(' . $phpExpression . ') { return true; } else { return false;}'); $result = array(); foreach ($entries as $lt) { //Filter out only the entries which satisifies the condition if ($fun($lt)) { $result[] = $lt; } } return $result; } catch (ODataException $exception) { $this->fail('An unexpected ODataException has been raised.' . $exception->getMessage()); } }