|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Peridot\Leo\Matcher; |
|
4
|
|
|
|
|
5
|
|
|
use Peridot\Leo\Matcher\Template\ArrayTemplate; |
|
6
|
|
|
use Peridot\Leo\Matcher\Template\TemplateInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* RangeMatcher matches a number or the length of a countable |
|
10
|
|
|
* between a lower and upper bound - both of which are inclusive. |
|
11
|
|
|
* |
|
12
|
|
|
* @package Peridot\Leo\Matcher |
|
13
|
|
|
*/ |
|
14
|
|
|
class RangeMatcher extends CountableMatcher |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var mixed |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $lowerBound; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var mixed |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $upperBound; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param mixed $lower |
|
28
|
|
|
* @param mixed $upper |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($lower, $upper) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setLowerBound($lower); |
|
33
|
|
|
$this->setUpperBound($upper); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
* |
|
39
|
|
|
* @return TemplateInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getDefaultCountableTemplate() |
|
42
|
|
|
{ |
|
43
|
|
|
return new ArrayTemplate([ |
|
44
|
|
|
'default' => "Expected {{actual}} to be within {$this->lowerBound}..{$this->upperBound}", |
|
45
|
|
|
'negated' => "Expected {{actual}} to not be within {$this->lowerBound}..{$this->upperBound}", |
|
46
|
|
|
]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
* |
|
52
|
|
|
* @return TemplateInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getDefaultTemplate() |
|
55
|
|
|
{ |
|
56
|
|
|
return new ArrayTemplate([ |
|
57
|
|
|
'default' => "Expected {{actual}} to be within {$this->lowerBound}..{$this->upperBound}", |
|
58
|
|
|
'negated' => "Expected {{actual}} to not be within {$this->lowerBound}..{$this->upperBound}", |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set the lower bound of the range matcher. |
|
64
|
|
|
* |
|
65
|
|
|
* @param mixed $lowerBound |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setLowerBound($lowerBound) |
|
69
|
|
|
{ |
|
70
|
|
|
if (!is_numeric($lowerBound)) { |
|
71
|
|
|
throw new \InvalidArgumentException('Lower bound must be a numeric value'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->lowerBound = $lowerBound; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set the upper bound of the range matcher. |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $upperBound |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setUpperBound($upperBound) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!is_numeric($upperBound)) { |
|
88
|
|
|
throw new \InvalidArgumentException('Upper bound must be a numeric value'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->upperBound = $upperBound; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return the lower bound of the range matcher. |
|
98
|
|
|
* |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getLowerBound() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->lowerBound; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Return the upper bound of the range matcher. |
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getUpperBound() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->upperBound; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Determine if the number is between an upper and lower bound (inclusive). |
|
118
|
|
|
* |
|
119
|
|
|
* @param $number |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function matchNumeric($number) |
|
123
|
|
|
{ |
|
124
|
|
|
return $number <= $this->upperBound && $number >= $this->lowerBound; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|