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