1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Config package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Config\Definition\Builder; |
12
|
|
|
|
13
|
|
|
use Borobudur\Config\Exception\InvalidArgumentException; |
14
|
|
|
use Borobudur\Config\MinMaxValueValidatorTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Iqbal Maulana <[email protected]> |
18
|
|
|
* @created 8/10/15 |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractNumericDefinition extends ScalarDefinition |
21
|
|
|
{ |
22
|
|
|
use MinMaxValueValidatorTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int|float|double |
26
|
|
|
*/ |
27
|
|
|
protected $min; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int|float|double |
31
|
|
|
*/ |
32
|
|
|
protected $max; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Assert numeric value. |
36
|
|
|
* |
37
|
|
|
* @param string $name |
38
|
|
|
* @param mixed $value |
39
|
|
|
*/ |
40
|
|
|
public static function assertNumericValue($name, $value) |
41
|
|
|
{ |
42
|
|
|
if (!is_int($value) && !is_float($value) && !is_double($value)) { |
43
|
|
|
throw InvalidArgumentException::invalidNumeric($name, $value); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Define minimum value. |
49
|
|
|
* |
50
|
|
|
* @param int|float|double $min |
51
|
|
|
* |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function min($min) |
55
|
|
|
{ |
56
|
|
|
$this->assertValidValue('max', $min, 'min', 'Cannot define min(%d) greater than max(%d)'); |
57
|
|
|
|
58
|
|
|
if (isset($this->max) && $this->max < $min) { |
59
|
|
|
throw new InvalidArgumentException(sprintf('Cannot define min(%d) greater than max(%d)', $min, $this->max)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->min = $min; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Define maximum value. |
69
|
|
|
* |
70
|
|
|
* @param int|float|double $max |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function max($max) |
75
|
|
|
{ |
76
|
|
|
$this->assertValidValue('min', $max, 'max', 'Cannot define max(%d) smaller than min(%d)'); |
77
|
|
|
|
78
|
|
|
$this->max = $max; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Assert that min or max value is valid. |
85
|
|
|
* |
86
|
|
|
* @param string $type |
87
|
|
|
* @param int|float $value |
88
|
|
|
* @param string $operator |
89
|
|
|
* @param string $message |
90
|
|
|
*/ |
91
|
|
|
private function assertValidValue($type, $value, $operator, $message) |
92
|
|
|
{ |
93
|
|
|
self::assertNumericValue($type, $value); |
94
|
|
|
|
95
|
|
|
if (false === $this->isValid($type, $value, $operator)) { |
96
|
|
|
throw new InvalidArgumentException(sprintf($message, $value, $this->{$type})); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|