1
|
|
|
<?php |
2
|
|
|
namespace Peridot\Leo\Matcher; |
3
|
|
|
|
4
|
|
|
use Peridot\Leo\Matcher\Template\TemplateInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* CountableMatcher a matcher is a matcher that matches numeric values, |
8
|
|
|
* or reduces a countable value - like array, string, or Countable - to a single |
9
|
|
|
* numeric value. |
10
|
|
|
* |
11
|
|
|
* @package Peridot\Leo\Matcher |
12
|
|
|
*/ |
13
|
|
|
abstract class CountableMatcher extends AbstractMatcher |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var mixed |
17
|
|
|
*/ |
18
|
|
|
protected $countable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Set the countable value used by the CountableMatcher. |
22
|
|
|
* |
23
|
|
|
* @param mixed $countable |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function setCountable($countable) |
27
|
|
|
{ |
28
|
|
|
$this->countable = $countable; |
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the countable used by the CountableMatcher. |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function getCountable() |
38
|
|
|
{ |
39
|
|
|
return $this->countable; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the count of the countable value. |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function getCount() |
47
|
|
|
{ |
48
|
|
|
if (is_string($this->countable)) { |
49
|
|
|
return strlen($this->countable); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return count($this->countable); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
* |
58
|
|
|
* Returns a default countable interface if the countable is set. |
59
|
|
|
* |
60
|
|
|
* @return TemplateInterface |
61
|
|
|
*/ |
62
|
|
|
public function getTemplate() |
63
|
|
|
{ |
64
|
|
|
if (isset($this->countable)) { |
65
|
|
|
return $this->getDefaultCountableTemplate(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return parent::getTemplate(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
* |
74
|
|
|
* @param $actual |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
protected function doMatch($actual = null) |
78
|
|
|
{ |
79
|
|
|
if (isset($this->countable)) { |
80
|
|
|
$actual = $this->getCount(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!is_numeric($actual)) { |
84
|
|
|
throw new \InvalidArgumentException(get_class($this) . " requires a numeric value"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->matchNumeric($actual); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return a default template for when a countable has been set. |
92
|
|
|
* |
93
|
|
|
* @return TemplateInterface |
94
|
|
|
*/ |
95
|
|
|
abstract public function getDefaultCountableTemplate(); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Determine if a number matches a specified condition |
99
|
|
|
* |
100
|
|
|
* @param $number |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
abstract protected function matchNumeric($number); |
104
|
|
|
} |
105
|
|
|
|