|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Valdi package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Valdi\Validator; |
|
13
|
|
|
|
|
14
|
|
|
use Valdi\ValidationException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Validator for parametrized validators. |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractParametrizedValidator implements ValidatorInterface { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Throws an exception if the parameters don't fullfill the expected |
|
23
|
|
|
* parameter count. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $name |
|
26
|
|
|
* the name of the validator |
|
27
|
|
|
* @param integer $parameterAmount |
|
28
|
|
|
* the amount of expected parameters |
|
29
|
|
|
* @param string[] $parameters |
|
30
|
|
|
* the parameters |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function validateParameterCount($name, $parameterAmount, array $parameters) { |
|
33
|
|
View Code Duplication |
if (count($parameters) !== $parameterAmount) { |
|
|
|
|
|
|
34
|
|
|
throw new ValidationException('"' . $name . '" expects ' . $parameterAmount . ' parameter.'); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Throws an exception if the parameters don't fullfill the expected |
|
40
|
|
|
* minimum parameter count. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* the name of the validator |
|
44
|
|
|
* @param integer $parameterAmount |
|
45
|
|
|
* the amount of expected parameters |
|
46
|
|
|
* @param string[] $parameters |
|
47
|
|
|
* the parameters |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function validateMinParameterCount($name, $parameterAmount, array $parameters) { |
|
50
|
|
View Code Duplication |
if (count($parameters) < $parameterAmount) { |
|
|
|
|
|
|
51
|
|
|
throw new ValidationException('"' . $name . '" expects at least ' . $parameterAmount . ' parameter.'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.