vendor/friendsofsymfony/rest-bundle/Request/ParamFetcher.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\RestBundle\Request;
  11. use FOS\RestBundle\Controller\Annotations\ParamInterface;
  12. use FOS\RestBundle\Exception\InvalidParameterException;
  13. use FOS\RestBundle\Util\ResolverTrait;
  14. use FOS\RestBundle\Validator\Constraints\ResolvableConstraintInterface;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  19. use Symfony\Component\Validator\Constraint;
  20. use Symfony\Component\Validator\ConstraintViolationList;
  21. use Symfony\Component\Validator\Validator\ValidatorInterface;
  22. use Symfony\Component\Validator\Exception\ValidatorException;
  23. use Symfony\Component\Validator\ConstraintViolation;
  24. /**
  25.  * Helper to validate parameters of the active request.
  26.  *
  27.  * @author Alexander <iam.asm89@gmail.com>
  28.  * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  29.  * @author Jordi Boggiano <j.boggiano@seld.be>
  30.  * @author Boris GuĂ©ry <guery.b@gmail.com>
  31.  */
  32. class ParamFetcher implements ParamFetcherInterface
  33. {
  34.     use ResolverTrait;
  35.     private $container;
  36.     private $parameterBag;
  37.     private $requestStack;
  38.     private $validator;
  39.     /**
  40.      * Initializes fetcher.
  41.      *
  42.      * @param ContainerInterface   $container
  43.      * @param ParamReaderInterface $paramReader
  44.      * @param RequestStack         $requestStack
  45.      * @param ValidatorInterface   $validator
  46.      */
  47.     public function __construct(ContainerInterface $containerParamReaderInterface $paramReaderRequestStack $requestStackValidatorInterface $validator null)
  48.     {
  49.         $this->container $container;
  50.         $this->requestStack $requestStack;
  51.         $this->validator $validator;
  52.         $this->parameterBag = new ParameterBag($paramReader);
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function setController($controller)
  58.     {
  59.         $this->parameterBag->setController($this->getRequest(), $controller);
  60.     }
  61.     /**
  62.      * Add additional params to the ParamFetcher during runtime.
  63.      *
  64.      * Note that adding a param that has the same name as an existing param will override that param.
  65.      *
  66.      * @param ParamInterface $param
  67.      */
  68.     public function addParam(ParamInterface $param)
  69.     {
  70.         $this->parameterBag->addParam($this->getRequest(), $param);
  71.     }
  72.     /**
  73.      * @return ParamInterface[]
  74.      */
  75.     public function getParams()
  76.     {
  77.         return $this->parameterBag->getParams($this->getRequest());
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function get($name$strict null)
  83.     {
  84.         $params $this->getParams();
  85.         if (!array_key_exists($name$params)) {
  86.             throw new \InvalidArgumentException(sprintf("No @ParamInterface configuration for parameter '%s'."$name));
  87.         }
  88.         /** @var ParamInterface $param */
  89.         $param $params[$name];
  90.         $default $param->getDefault();
  91.         $default $this->resolveValue($this->container$default);
  92.         $strict = (null !== $strict $strict $param->isStrict());
  93.         $paramValue $param->getValue($this->getRequest(), $default);
  94.         return $this->cleanParamWithRequirements($param$paramValue$strict$default);
  95.     }
  96.     /**
  97.      * @param ParamInterface $param
  98.      * @param mixed          $paramValue
  99.      * @param bool           $strict
  100.      * @param mixed          $default
  101.      *
  102.      * @throws BadRequestHttpException
  103.      * @throws \RuntimeException
  104.      *
  105.      * @return mixed
  106.      *
  107.      * @internal
  108.      */
  109.     protected function cleanParamWithRequirements(ParamInterface $param$paramValue$strict$default)
  110.     {
  111.         $this->checkNotIncompatibleParams($param);
  112.         if (null !== $default && $default === $paramValue) {
  113.             return $paramValue;
  114.         }
  115.         $constraints $param->getConstraints();
  116.         $this->resolveConstraints($constraints);
  117.         if (empty($constraints)) {
  118.             return $paramValue;
  119.         }
  120.         if (null === $this->validator) {
  121.             throw new \RuntimeException(
  122.                 'The ParamFetcher requirements feature requires the symfony/validator component.'
  123.             );
  124.         }
  125.         try {
  126.             $errors $this->validator->validate($paramValue$constraints);
  127.         } catch (ValidatorException $e) {
  128.             $violation = new ConstraintViolation(
  129.                 $e->getMessage(),
  130.                 $e->getMessage(),
  131.                 array(),
  132.                 $paramValue,
  133.                 '',
  134.                 null,
  135.                 null,
  136.                 $e->getCode()
  137.             );
  138.             $errors = new ConstraintViolationList(array($violation));
  139.         }
  140.         if (count($errors)) {
  141.             if ($strict) {
  142.                 throw InvalidParameterException::withViolations($param$errors);
  143.             }
  144.             return null === $default '' $default;
  145.         }
  146.         return $paramValue;
  147.     }
  148.     /**
  149.      * {@inheritdoc}
  150.      */
  151.     public function all($strict null)
  152.     {
  153.         $configuredParams $this->getParams();
  154.         $params = [];
  155.         foreach ($configuredParams as $name => $param) {
  156.             $params[$name] = $this->get($name$strict);
  157.         }
  158.         return $params;
  159.     }
  160.     /**
  161.      * Check if current param is not in conflict with other parameters
  162.      * according to the "incompatibles" field.
  163.      *
  164.      * @param ParamInterface $param the configuration for the param fetcher
  165.      *
  166.      * @throws InvalidArgumentException
  167.      * @throws BadRequestHttpException
  168.      *
  169.      * @internal
  170.      */
  171.     protected function checkNotIncompatibleParams(ParamInterface $param)
  172.     {
  173.         if (null === $param->getValue($this->getRequest(), null)) {
  174.             return;
  175.         }
  176.         $params $this->getParams();
  177.         foreach ($param->getIncompatibilities() as $incompatibleParamName) {
  178.             if (!array_key_exists($incompatibleParamName$params)) {
  179.                 throw new \InvalidArgumentException(sprintf("No @ParamInterface configuration for parameter '%s'."$incompatibleParamName));
  180.             }
  181.             $incompatibleParam $params[$incompatibleParamName];
  182.             if (null !== $incompatibleParam->getValue($this->getRequest(), null)) {
  183.                 $exceptionMessage sprintf(
  184.                     "'%s' param is incompatible with %s param.",
  185.                     $param->getName(),
  186.                     $incompatibleParam->getName()
  187.                 );
  188.                 throw new BadRequestHttpException($exceptionMessage);
  189.             }
  190.         }
  191.     }
  192.     /**
  193.      * @param Constraint[] $constraints
  194.      */
  195.     private function resolveConstraints(array $constraints)
  196.     {
  197.         foreach ($constraints as $constraint) {
  198.             if ($constraint instanceof ResolvableConstraintInterface) {
  199.                 $constraint->resolve($this->container);
  200.             }
  201.         }
  202.     }
  203.     /**
  204.      * @throws \RuntimeException
  205.      *
  206.      * @return Request
  207.      */
  208.     private function getRequest()
  209.     {
  210.         $request $this->requestStack->getCurrentRequest();
  211.         if (null === $request) {
  212.             throw new \RuntimeException('There is no current request.');
  213.         }
  214.         return $request;
  215.     }
  216. }