Completed
Push — master ( 46ca35...9cf9e5 )
by Maxim
03:19
created

MaxConstraint::getMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Weew\Validator\Constraints;
4
5
use Weew\Validator\IConstraint;
6
use Weew\Validator\IValidationData;
7
8
/**
9
 * Check if the value is not greater then given.
10
 */
11 View Code Duplication
class MaxConstraint implements IConstraint {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
12
    /**
13
     * @var int
14
     */
15
    protected $max;
16
17
    /**
18
     * @var string
19
     */
20
    protected $message;
21
22
    /**
23
     * MaxConstraint constructor.
24
     *
25
     * @param int $max
26
     * @param string $message
27
     */
28
    public function __construct($max, $message = null) {
29
        $this->max = $max;
30
        $this->message = $message;
31
    }
32
33
    /**
34
     * @param $value
35
     * @param IValidationData $data
36
     *
37
     * @return bool
38
     */
39
    public function check($value, IValidationData $data = null) {
40
        if (is_numeric($value)) {
41
            return $value <= $this->max;
42
        }
43
44
        return false;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getMessage() {
51
        if ($this->message !== null) {
52
            return $this->message;
53
        }
54
55
        return s(
56
            'Must not be greater than "%s".', $this->max
57
        );
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function getOptions() {
64
        return [
65
            'max' => $this->max,
66
        ];
67
    }
68
}
69