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: