These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Particle. |
||
4 | * |
||
5 | * @link http://github.com/particle-php for the canonical source repository |
||
6 | * @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com) |
||
7 | * @license https://github.com/particle-php/validator/blob/master/LICENSE New BSD License |
||
8 | */ |
||
9 | namespace Particle\Validator\Rule; |
||
10 | |||
11 | use Particle\Validator\Rule; |
||
12 | |||
13 | /** |
||
14 | * This rule will validate a value to be less than a value. |
||
15 | * |
||
16 | * @package Particle\Validator\Rule |
||
17 | */ |
||
18 | View Code Duplication | class LessThan extends Rule |
|
0 ignored issues
–
show
|
|||
19 | { |
||
20 | /** |
||
21 | * A constant for an error message if the value is not less than the max. |
||
22 | */ |
||
23 | const NOT_LESS_THAN = 'LessThan::NOT_LESS_THAN'; |
||
24 | |||
25 | /** |
||
26 | * The message templates which can be returned by this validator. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $messageTemplates = [ |
||
31 | self::NOT_LESS_THAN => '{{ name }} must be less than {{ max }}', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * The upper boundary. |
||
36 | * |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $max; |
||
40 | |||
41 | /** |
||
42 | * Construct the LessThan rule. |
||
43 | * |
||
44 | * @param int $max |
||
45 | */ |
||
46 | 3 | public function __construct($max) |
|
47 | { |
||
48 | 3 | $this->max = $max; |
|
49 | 3 | } |
|
50 | |||
51 | /** |
||
52 | * Checks whether or not $value is less than the max for this rule. |
||
53 | * |
||
54 | * @param mixed $value |
||
55 | * @return bool |
||
56 | */ |
||
57 | 3 | public function validate($value) |
|
58 | { |
||
59 | 3 | return !$this->notLessThan($value, self::NOT_LESS_THAN); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns whether or not the value is less than the max, and logs an error if it isn't. |
||
64 | * |
||
65 | * @param mixed $value |
||
66 | * @param string $error |
||
67 | * @return bool |
||
68 | */ |
||
69 | 3 | protected function notLessThan($value, $error) |
|
70 | { |
||
71 | 3 | if ($value >= $this->max) { |
|
72 | 2 | $this->error($error); |
|
73 | 2 | return true; |
|
74 | } |
||
75 | 1 | return false; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Returns the parameters that may be used in a validation message. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | 2 | protected function getMessageParameters() |
|
84 | { |
||
85 | 2 | return array_merge(parent::getMessageParameters(), [ |
|
86 | 2 | 'max' => $this->max, |
|
87 | 2 | ]); |
|
88 | } |
||
89 | } |
||
90 |
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.