/** * Filters query parameter bag * * @param string $filterName * @param ParameterBag $bag * * @return ParameterBag */ public function filter($filterName, ParameterBag $bag) { $expectedParams = $this->filters[$filterName]; // Given parameters foreach ($bag->all() as $name => $value) { if (!isset($expectedParams[$name])) { throw new \InvalidArgumentException(sprintf('Unknow parameter "%s"', $name)); } $options = $expectedParams[$name]; if (0 < count($options['roles']) && !$this->authorizationChecker->isGranted($options['roles'])) { throw new AccessDeniedException(sprintf('User has not the required role to use "%s" parameter', $name)); } $this->validator->validate($name, $options, $value); $bag->set($name, $this->caster->cast($options, $value)); } // Expected parameters foreach ($expectedParams as $name => $options) { Validator::checkMissingParameter($name, $options, $bag); if (!$bag->has($name) && isset($options['default'])) { $bag->set($name, $this->caster->getDefaultValue($options)); } } return $bag; }
/** * Tests "getDefaultValue" with an empty array as default value */ public function test_getDefaultValue_filledArray() { $parameterType = $this->prophet->prophesize('Gl3n\\HttpQueryStringFilterBundle\\ParameterType\\ParameterTypeInterface'); $parameterType->castValue('one')->shouldBeCalled()->willReturn(1); $parameterType->castValue('two')->shouldBeCalled()->willReturn(2); $parameterType->castValue('three')->shouldBeCalled()->willReturn(3); $parameterTypeChain = $this->prophet->prophesize('Gl3n\\HttpQueryStringFilterBundle\\ParameterType\\ParameterTypeChain'); $parameterTypeChain->getParameterType('special')->willReturn($parameterType->reveal()); $caster = new HttpQueryStringFilterBundle\Caster($parameterTypeChain->reveal()); $result = $caster->getDefaultValue(['type' => 'special', 'array' => true, 'default' => '["one", "two", "three"]']); $this->array($result)->isEqualTo([1, 2, 3]); }