We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
16 | class Length extends AbstractRule |
||
17 | { |
||
18 | public $minValue; |
||
19 | public $maxValue; |
||
20 | public $inclusive; |
||
21 | |||
22 | 45 | public function __construct($min = null, $max = null, $inclusive = true) |
|
23 | { |
||
24 | 45 | $this->minValue = $min; |
|
25 | 45 | $this->maxValue = $max; |
|
26 | 45 | $this->inclusive = $inclusive; |
|
27 | 45 | $paramValidator = new AnyOf(new NumericVal(), new NullType()); |
|
28 | 45 | if (!$paramValidator->validate($min)) { |
|
29 | 1 | throw new ComponentException( |
|
30 | 1 | sprintf('%s is not a valid numeric length', $min) |
|
31 | ); |
||
32 | } |
||
33 | |||
34 | 44 | if (!$paramValidator->validate($max)) { |
|
35 | 1 | throw new ComponentException( |
|
36 | 1 | sprintf('%s is not a valid numeric length', $max) |
|
37 | ); |
||
38 | } |
||
39 | |||
40 | 43 | if (!is_null($min) && !is_null($max) && $min > $max) { |
|
41 | 1 | throw new ComponentException( |
|
42 | 1 | sprintf('%s cannot be less than %s for validation', $min, $max) |
|
43 | ); |
||
44 | } |
||
45 | 42 | } |
|
46 | |||
47 | 40 | public function validate($input) |
|
53 | |||
54 | 40 | protected function extractLength($input) |
|
55 | { |
||
56 | 40 | if (is_string($input)) { |
|
57 | 31 | return mb_strlen($input, mb_detect_encoding($input)); |
|
58 | } |
||
59 | |||
60 | 11 | if (is_array($input) || $input instanceof \Countable) { |
|
61 | 4 | return count($input); |
|
62 | } |
||
63 | |||
64 | 7 | if (is_object($input)) { |
|
65 | 3 | return count(get_object_vars($input)); |
|
66 | } |
||
67 | |||
68 | 4 | if (is_int($input)) { |
|
69 | 2 | return mb_strlen((string) $input); |
|
70 | } |
||
71 | |||
72 | 2 | return false; |
|
73 | } |
||
74 | |||
75 | 40 | protected function validateMin($length) |
|
87 | |||
88 | 30 | protected function validateMax($length) |
|
100 | } |
||
101 |