vendor/sylius/resource-bundle/src/Bundle/Controller/ParametersParser.php line 48

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Controller;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Webmozart\Assert\Assert;
  16. final class ParametersParser implements ParametersParserInterface
  17. {
  18.     /** @var ContainerInterface */
  19.     private $container;
  20.     /** @var ExpressionLanguage */
  21.     private $expression;
  22.     public function __construct(ContainerInterface $containerExpressionLanguage $expression)
  23.     {
  24.         $this->container $container;
  25.         $this->expression $expression;
  26.     }
  27.     public function parseRequestValues(array $parametersRequest $request): array
  28.     {
  29.         return array_map(
  30.             /**
  31.              * @param mixed $parameter
  32.              *
  33.              * @return mixed
  34.              */
  35.             function ($parameter) use ($request) {
  36.                 if (is_array($parameter)) {
  37.                     return $this->parseRequestValues($parameter$request);
  38.                 }
  39.                 return $this->parseRequestValue($parameter$request);
  40.             },
  41.             $parameters
  42.         );
  43.     }
  44.     /**
  45.      * @param mixed $parameter
  46.      *
  47.      * @return mixed
  48.      */
  49.     private function parseRequestValue($parameterRequest $request)
  50.     {
  51.         if (!is_string($parameter)) {
  52.             return $parameter;
  53.         }
  54.         if (=== strpos($parameter'$')) {
  55.             return $request->get(substr($parameter1));
  56.         }
  57.         if (=== strpos($parameter'expr:')) {
  58.             return $this->parseRequestValueExpression(substr($parameter5), $request);
  59.         }
  60.         if (=== strpos($parameter'!!')) {
  61.             return $this->parseRequestValueTypecast($parameter$request);
  62.         }
  63.         return $parameter;
  64.     }
  65.     /** @return mixed */
  66.     private function parseRequestValueExpression(string $expressionRequest $request)
  67.     {
  68.         $expression = (string) preg_replace_callback(
  69.             '/(\$\w+)/',
  70.             /**
  71.              * @return mixed
  72.              */
  73.             function (array $matches) use ($request) {
  74.                 $variable $request->get(substr($matches[1], 1));
  75.                 if (is_array($variable) || is_object($variable)) {
  76.                     throw new \InvalidArgumentException(sprintf(
  77.                         'Cannot use %s ($%s) as parameter in expression.',
  78.                         gettype($variable),
  79.                         $matches[1]
  80.                     ));
  81.                 }
  82.                 return is_string($variable) ? sprintf('"%s"'addslashes($variable)) : $variable;
  83.             },
  84.             $expression
  85.         );
  86.         return $this->expression->evaluate($expression, ['container' => $this->container]);
  87.     }
  88.     /** @return mixed */
  89.     private function parseRequestValueTypecast(string $parameterRequest $request)
  90.     {
  91.         [$typecast$castedValue] = explode(' '$parameter2);
  92.         /** @var callable $castFunctionName */
  93.         $castFunctionName substr($typecast2) . 'val';
  94.         Assert::oneOf($castFunctionName, ['intval''floatval''boolval'], 'Variable can be casted only to int, float or bool.');
  95.         return $castFunctionName($this->parseRequestValue($castedValue$request));
  96.     }
  97. }