CountableMatcher::getTemplate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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