Code Duplication    Length = 67-69 lines in 2 locations

src/Weew/Validator/Constraints/MinMaxConstraint.php 1 location

@@ 11-77 (lines=67) @@
8
/**
9
 * Check if the value is inside the given range.
10
 */
11
class MinMaxConstraint implements IConstraint {
12
    /**
13
     * @var int
14
     */
15
    protected $min;
16
17
    /**
18
     * @var int
19
     */
20
    protected $max;
21
22
    /**
23
     * @var string
24
     */
25
    protected $message;
26
27
    /**
28
     * MinMaxConstraint constructor.
29
     *
30
     * @param int $min
31
     * @param int $max
32
     * @param string $message
33
     */
34
    public function __construct($min, $max, $message = null) {
35
        $this->min = $min;
36
        $this->max = $max;
37
        $this->message = $message;
38
    }
39
40
    /**
41
     * @param $value
42
     * @param IValidationData $data
43
     *
44
     * @return bool
45
     */
46
    public function check($value, IValidationData $data = null) {
47
        if (is_numeric($value)) {
48
            return $value >= $this->min && $value <= $this->max;
49
        }
50
51
        return false;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getMessage() {
58
        if ($this->message !== null) {
59
            return $this->message;
60
        }
61
62
        return s(
63
            'Must have a value between "%s" and "%s".',
64
            $this->min, $this->max
65
        );
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getOptions() {
72
        return [
73
            'min' => $this->min,
74
            'max' => $this->max,
75
        ];
76
    }
77
}
78

src/Weew/Validator/Constraints/MinMaxLengthConstraint.php 1 location

@@ 11-79 (lines=69) @@
8
/**
9
 * Check if the value's length is in the given range.
10
 */
11
class MinMaxLengthConstraint implements IConstraint {
12
    /**
13
     * @var int
14
     */
15
    protected $min;
16
17
    /**
18
     * @var int
19
     */
20
    protected $max;
21
22
    /**
23
     * @var string
24
     */
25
    protected $message;
26
27
    /**
28
     * MinMaxLengthConstraint constructor.
29
     *
30
     * @param $min
31
     * @param $max
32
     * @param string $message
33
     */
34
    public function __construct($min, $max, $message = null) {
35
        $this->min = $min;
36
        $this->max = $max;
37
        $this->message = $message;
38
    }
39
40
    /**
41
     * @param $value
42
     * @param IValidationData $data
43
     *
44
     * @return bool
45
     */
46
    public function check($value, IValidationData $data = null) {
47
        if (is_string($value)) {
48
            $length = strlen($value);
49
50
            return $length >= $this->min && $length <= $this->max;
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getMessage() {
60
        if ($this->message !== null) {
61
            return $this->message;
62
        }
63
64
        return s(
65
            'Must be between "%s" and "%s" characters long.',
66
            $this->min, $this->max
67
        );
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getOptions() {
74
        return [
75
            'min' => $this->min,
76
            'max' => $this->max,
77
        ];
78
    }
79
}
80