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 greater than a value. |
||
15 | * |
||
16 | * @package Particle\Validator\Rule |
||
17 | */ |
||
18 | View Code Duplication | class GreaterThan extends Rule |
|
0 ignored issues
–
show
|
|||
19 | { |
||
20 | /** |
||
21 | * A constant for an error message if the value is not greater than the min. |
||
22 | */ |
||
23 | const NOT_GREATER_THAN = 'GreaterThan::NOT_GREATER_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_GREATER_THAN => '{{ name }} must be greater than {{ min }}', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * The lower boundary. |
||
36 | * |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $min; |
||
40 | |||
41 | /** |
||
42 | * Construct the GreaterThan rule. |
||
43 | * |
||
44 | * @param int $min |
||
45 | */ |
||
46 | 3 | public function __construct($min) |
|
47 | { |
||
48 | 3 | $this->min = $min; |
|
49 | 3 | } |
|
50 | |||
51 | /** |
||
52 | * Checks whether or not $value is greater than the min for this rule. |
||
53 | * |
||
54 | * @param mixed $value |
||
55 | * @return bool |
||
56 | */ |
||
57 | 3 | public function validate($value) |
|
58 | { |
||
59 | 3 | return !$this->notGreaterThan($value, self::NOT_GREATER_THAN); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns whether or not the value is greater than the min, 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 notGreaterThan($value, $error) |
|
70 | { |
||
71 | 3 | if ($value <= $this->min) { |
|
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 | 'min' => $this->min, |
|
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.