1 <?php
2 
3 declare(strict_types=1);
4 
5 namespace JMS\Serializer\Metadata\Driver;
6 
7 use JMS\Serializer\Exception\InvalidMetadataException;
8 use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
9 use JMS\Serializer\Expression\Expression;
10 
11 trait ExpressionMetadataTrait
12 {
13     /**
14      * @var CompilableExpressionEvaluatorInterface
15      */
16     private $expressionEvaluator;
17 
18     /**
19      * @return Expression|string
20      *
21      * @throws InvalidMetadataException
22      */
23     private function parseExpression(string $expression, array $names = [])
24     {
25         if (null === $this->expressionEvaluator) {
26             return $expression;
27         }
28 
29         try {
30             return $this->expressionEvaluator->parse($expression, array_merge(['context', 'property_metadata', 'object'], $names));
31         } catch (\LogicException $e) {
32             throw new InvalidMetadataException(sprintf('Can not parse the expression "%s"', $expression), 0, $e);
33         }
34     }
35 }
36