1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Chromabits\Nucleus\Validation\Constraints;
13:
14: use Chromabits\Nucleus\Exceptions\LackOfCoffeeException;
15: use Chromabits\Nucleus\Meditation\Arguments;
16: use Chromabits\Nucleus\Meditation\Constraints\AbstractConstraint;
17: use Chromabits\Nucleus\Meditation\Constraints\NumericConstraint;
18: use Chromabits\Nucleus\Meditation\Exceptions\InvalidArgumentException;
19:
20: 21: 22: 23: 24: 25:
26: class BetweenConstraint extends AbstractConstraint
27: {
28: 29: 30:
31: protected $min;
32:
33: 34: 35:
36: protected $max;
37:
38: 39: 40:
41: public function getMin()
42: {
43: return $this->min;
44: }
45:
46: 47: 48:
49: public function getMax()
50: {
51: return $this->max;
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61: 62:
63: public function __construct($min, $max)
64: {
65: parent::__construct();
66:
67: Arguments::define(new NumericConstraint(), new NumericConstraint())
68: ->check($min, $max);
69:
70: if ($min > $max) {
71: throw new LackOfCoffeeException(
72: 'Min should be smaller than or equal to max.'
73: );
74: }
75:
76: $this->min = $min;
77: $this->max = $max;
78: }
79:
80: 81: 82: 83: 84: 85: 86: 87:
88: public function check($value, array $context = [])
89: {
90: return ($this->min <= $value && $this->max >= $value);
91: }
92:
93: 94: 95: 96: 97:
98: public function toString()
99: {
100: return vsprintf('%f <= x <= %f', [$this->min, $this->max]);
101: }
102:
103: 104: 105: 106: 107:
108: public function getDescription()
109: {
110: return vsprintf(
111: 'The value is expected to be between %f and %f'
112: . ' (%f <= x <= %f).',
113: [$this->min, $this->max, $this->min, $this->max]
114: );
115: }
116: }
117: