Completed
Pull Request — master (#22)
by Erin
02:35
created

CountableMatcher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 9
c 5
b 0
f 3
lcom 1
cbo 1
dl 0
loc 92
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setCountable() 0 5 1
A getCountable() 0 4 1
A getCount() 0 8 2
A getTemplate() 0 8 2
A doMatch() 0 12 3
getDefaultCountableTemplate() 0 1 ?
matchNumeric() 0 1 ?
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