Completed
Pull Request — master (#23)
by Erin
03:28
created

AbstractMatcher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 8
Bugs 0 Features 7
Metric Value
wmc 9
c 8
b 0
f 7
lcom 2
cbo 1
dl 0
loc 112
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isNegated() 0 4 1
A invert() 0 5 1
A match() 0 8 2
A getTemplate() 0 7 2
A setTemplate() 0 5 1
A setAssertion() 0 5 1
doMatch() 0 1 ?
1
<?php
2
namespace Peridot\Leo\Matcher;
3
4
use Peridot\Leo\Assertion;
5
use Peridot\Leo\Matcher\Template\TemplateInterface;
6
7
/**
8
 * AbstractMatcher serves as the base for all Matchers.
9
 *
10
 * @package Peridot\Leo\Matcher
11
 */
12
abstract class AbstractMatcher implements MatcherInterface
13
{
14
    /**
15
     * @var mixed
16
     */
17
    protected $expected;
18
19
    /**
20
     * @var bool
21
     */
22
    protected $negated = false;
23
24
    /**
25
     * @var TemplateInterface
26
     */
27
    protected $template;
28
29
    /**
30
     * @var Assertion
31
     */
32
    protected $assertion;
33
34
    /**
35
     * @param mixed $expected
36
     */
37
    public function __construct($expected)
38
    {
39
        $this->expected = $expected;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     *
45
     * @return bool
46
     */
47
    public function isNegated()
48
    {
49
        return $this->negated;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @return $this
56
     */
57
    public function invert()
58
    {
59
        $this->negated = !$this->negated;
60
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @param mixed $actual
67
     * @return Match
68
     */
69
    public function match($actual = "")
70
    {
71
        $isMatch = $this->doMatch($actual);
72
        if ($this->isNegated()) {
73
            $isMatch = !$isMatch;
74
        }
75
        return new Match($isMatch, $this->expected, $actual, $this->isNegated());
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @return TemplateInterface
82
     */
83
    public function getTemplate()
84
    {
85
        if (! isset($this->template)) {
86
            return $this->getDefaultTemplate();
87
        }
88
        return $this->template;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     *
94
     * @param TemplateInterface $template
95
     * @return $this
96
     */
97
    public function setTemplate(TemplateInterface $template)
98
    {
99
        $this->template = $template;
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     *
106
     * @param Assertion $assertion
107
     * @return $this
108
     */
109
    public function setAssertion(Assertion $assertion)
110
    {
111
        $this->assertion = $assertion;
112
        return $this;
113
    }
114
115
    /**
116
     * The actual matching algorithm for the matcher. This is called by ->match()
117
     * to create a Match result.
118
     *
119
     * @param mixed $actual
120
     * @return bool
121
     */
122
    abstract protected function doMatch($actual);
123
}
124