Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/expression-language/ExpressionFunction.php line 84

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\ExpressionLanguage;
  11. /**
  12.  * Represents a function that can be used in an expression.
  13.  *
  14.  * A function is defined by two PHP callables. The callables are used
  15.  * by the language to compile and/or evaluate the function.
  16.  *
  17.  * The "compiler" function is used at compilation time and must return a
  18.  * PHP representation of the function call (it receives the function
  19.  * arguments as arguments).
  20.  *
  21.  * The "evaluator" function is used for expression evaluation and must return
  22.  * the value of the function call based on the values defined for the
  23.  * expression (it receives the values as a first argument and the function
  24.  * arguments as remaining arguments).
  25.  *
  26.  * @author Fabien Potencier <fabien@symfony.com>
  27.  */
  28. class ExpressionFunction
  29. {
  30.     private $name;
  31.     private $compiler;
  32.     private $evaluator;
  33.     /**
  34.      * @param string   $name      The function name
  35.      * @param callable $compiler  A callable able to compile the function
  36.      * @param callable $evaluator A callable able to evaluate the function
  37.      */
  38.     public function __construct(string $name, callable $compiler, callable $evaluator)
  39.     {
  40.         $this->name $name;
  41.         $this->compiler $compiler instanceof \Closure $compiler \Closure::fromCallable($compiler);
  42.         $this->evaluator $evaluator instanceof \Closure $evaluator \Closure::fromCallable($evaluator);
  43.     }
  44.     /**
  45.      * @return string
  46.      */
  47.     public function getName()
  48.     {
  49.         return $this->name;
  50.     }
  51.     /**
  52.      * @return \Closure
  53.      */
  54.     public function getCompiler()
  55.     {
  56.         return $this->compiler;
  57.     }
  58.     /**
  59.      * @return \Closure
  60.      */
  61.     public function getEvaluator()
  62.     {
  63.         return $this->evaluator;
  64.     }
  65.     /**
  66.      * Creates an ExpressionFunction from a PHP function name.
  67.      *
  68.      * @param string|null $expressionFunctionName The expression function name (default: same than the PHP function name)
  69.      *
  70.      * @return self
  71.      *
  72.      * @throws \InvalidArgumentException if given PHP function name does not exist
  73.      * @throws \InvalidArgumentException if given PHP function name is in namespace
  74.      *                                   and expression function name is not defined
  75.      */
  76.     public static function fromPhp(string $phpFunctionNamestring $expressionFunctionName null)
  77.     {
  78.         $phpFunctionName ltrim($phpFunctionName'\\');
  79.         if (!\function_exists($phpFunctionName)) {
  80.             throw new \InvalidArgumentException(sprintf('PHP function "%s" does not exist.'$phpFunctionName));
  81.         }
  82.         $parts explode('\\'$phpFunctionName);
  83.         if (!$expressionFunctionName && \count($parts) > 1) {
  84.             throw new \InvalidArgumentException(sprintf('An expression function name must be defined when PHP function "%s" is namespaced.'$phpFunctionName));
  85.         }
  86.         $compiler = function (...$args) use ($phpFunctionName) {
  87.             return sprintf('\%s(%s)'$phpFunctionNameimplode(', '$args));
  88.         };
  89.         $evaluator = function ($p, ...$args) use ($phpFunctionName) {
  90.             return $phpFunctionName(...$args);
  91.         };
  92.         return new self($expressionFunctionName ?: end($parts), $compiler$evaluator);
  93.     }
  94. }