Completed
Push — master ( ce18a8...486e29 )
by Andres
04:16
created

NumberValidator::negative()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
namespace Comfort\Validator;
3
4
use Comfort\Comfort;
5
use Comfort\Validator\Helper\AnyOfTrait;
6
7
class NumberValidator extends AbstractValidator
8
{
9
    use AnyOfTrait;
10
11
    public function __construct(Comfort $comfort)
12
    {
13
        parent::__construct($comfort);
14
15
        $this->errorHandlers += [
16
            'number.min' => [
17
                'message' => '%s must be above %s'
18
            ],
19
            'number.max' => [
20
                'message' => '%s must be below %s'
21
            ],
22
            'number.precision' => [
23
                'message' => '%s must be precision %s'
24
            ],
25
            'number.is_int' => [
26
                'message' => '%s must be an int'
27
            ],
28
            'number.is_float' => [
29
                'message' => '%s must be a float'
30
            ],
31
            'number.is_number' => [
32
                'message' => '%s must be a number'
33
            ],
34
            'number.negative' => [
35
                'message' => '%s must be negative'
36
            ],
37
            'number.positive' => [
38
                'message' => '%s must be positive'
39
            ]
40
        ];
41
42
        $this->isNumber();
43
    }
44
45
    /**
46
     * Validate number is no less than $min
47
     *
48
     * @param $min
49
     * @return $this
50
     */
51
    public function min($min)
52
    {
53
        return $this->add(function($value, $nameKey) use ($min) {
54
            if ($value < $min) {
55
                return $this->createError('number.min', $value, $nameKey);
56
            }
57
        });
58
    }
59
60
    /**
61
     * Validate number is no more than $max
62
     *
63
     * @param $max
64
     * @return $this
65
     */
66
    public function max($max)
67
    {
68
        return $this->add(function($value, $nameKey) use ($max) {
69
            if ($value > $max) {
70
                return $this->createError('number.max', $value, $nameKey);
71
            }
72
        });
73
    }
74
75
    /**
76
     * Validate precision of number is $precision
77
     *
78
     * @todo think if less kludgy way to do this...
79
     * @param $precision
80
     * @return $this
81
     */
82
    public function precision($precision)
83
    {
84
        return $this->add(function($value, $nameKey) use ($precision) {
85
            if (strlen(substr($value, strpos($value, '.') + 1)) != $precision) {
86
                return $this->createError('number.precision', $value, $nameKey);
87
            }
88
        });
89
    }
90
91
    /**
92
     * Validate a number is positive
93
     *
94
     * @return $this
95
     */
96
    public function positive()
97
    {
98
        return $this->add(function($value, $nameKey) {
99
            if ($value < 0) {
100
                return $this->createError('number.positive', $value, $nameKey);
101
            }
102
        });
103
    }
104
105
    /**
106
     * Validate a number is negative
107
     */
108
    public function negative()
109
    {
110
        return $this->add(function($value, $nameKey) {
111
            if ($value > 0) {
112
                return $this->createError('number.negative', $value, $nameKey);
113
            }
114
        });
115
    }
116
117
    /**
118
     * Validate value os an integer
119
     *
120
     * @return $this
121
     */
122
    public function isInt()
123
    {
124
        return $this->add(function($value, $nameKey) {
125
            if (!is_int($value)) {
126
                return $this->createError('number.is_int', $value, $nameKey);
127
            }
128
        });
129
    }
130
131
    /**
132
     * Validate value is a float
133
     *
134
     * @return $this
135
     */
136
    public function isFloat()
137
    {
138
        return $this->add(function($value, $nameKey) {
139
            if (!is_float($value)) {
140
                return $this->createError('number.is_float', $value, $nameKey);
141
            }
142
        });
143
    }
144
145
    /**
146
     * Validate value is numeric, be it float or int.
147
     *
148
     * @return $this
149
     */
150
    public function isNumber()
151
    {
152
        return $this->add(function($value, $nameKey) {
153
            if (!is_numeric($value)) {
154
                return $this->createError('number.is_numeric', $value, $nameKey);
155
            }
156
        });
157
    }
158
}