Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Argument / Validator.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.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
11 namespace Behat\Testwork\Argument;
12
13 use Behat\Testwork\Argument\Exception\UnknownParameterValueException;
14 use ReflectionFunctionAbstract;
15 use ReflectionMethod;
16 use ReflectionParameter;
17
18 /**
19  * Validates function arguments.
20  *
21  * @author Konstantin Kudryashov <ever.zet@gmail.com>
22  */
23 final class Validator
24 {
25     /**
26      * Validates that all arguments are in place, throws exception otherwise.
27      *
28      * @param ReflectionFunctionAbstract $function
29      * @param mixed[]                    $arguments
30      *
31      * @throws UnknownParameterValueException
32      */
33     public function validateArguments(ReflectionFunctionAbstract $function, array $arguments)
34     {
35         foreach ($function->getParameters() as $num => $parameter) {
36             $this->validateArgument($parameter, $num, $arguments);
37         }
38     }
39
40     /**
41      * Validates given argument.
42      *
43      * @param ReflectionParameter $parameter
44      * @param integer             $parameterIndex
45      * @param array               $givenArguments
46      */
47     private function validateArgument(ReflectionParameter $parameter, $parameterIndex, array $givenArguments)
48     {
49         if ($parameter->isDefaultValueAvailable()) {
50             return;
51         }
52
53         if (array_key_exists($parameterIndex, $givenArguments)) {
54             return;
55         }
56
57         if (array_key_exists($parameter->getName(), $givenArguments)) {
58             return;
59         }
60
61         throw new UnknownParameterValueException(sprintf(
62             'Can not find a matching value for an argument `$%s` of the method `%s`.',
63             $parameter->getName(),
64             $this->getFunctionPath($parameter->getDeclaringFunction())
65         ));
66     }
67
68     /**
69      * Returns function path for a provided reflection.
70      *
71      * @param ReflectionFunctionAbstract $function
72      *
73      * @return string
74      */
75     private function getFunctionPath(ReflectionFunctionAbstract $function)
76     {
77         if ($function instanceof ReflectionMethod) {
78             return sprintf(
79                 '%s::%s()',
80                 $function->getDeclaringClass()->getName(),
81                 $function->getName()
82             );
83         }
84
85         return sprintf('%s()', $function->getName());
86     }
87 }