1: <?php
2:
3: /**
4: * This file is part of expect package.
5: *
6: * (c) Noritaka Horio <holy.shared.design@gmail.com>
7: *
8: * This source file is subject to the MIT license that is bundled
9: * with this source code in the file LICENSE.
10: */
11: namespace expect;
12:
13: use expect\matcher\ReportableMatcher;
14:
15: /**
16: * Evaluate the matcher.
17: *
18: * @method boolean toEqual() toEqual(mixed $expected)
19: * @method boolean toBe() toBe(mixed $expected)
20: * @method boolean toBeTrue() toBeTrue()
21: * @method boolean toBeTruthy() toBeTruthy()
22: * @method boolean toBeFalse() toBeFalse()
23: * @method boolean toBeFalsy() toBeFalsy()
24: * @method boolean toBeNull() toBeNull()
25: * @method boolean toBeA() toBeA(string $expected)
26: * @method boolean toBeAn() toBeAn(string $expected)
27: * @method boolean toBeString() toBeString()
28: * @method boolean toBeInteger() toBeInteger()
29: * @method boolean toBeFloat() toBeFloat()
30: * @method boolean toBeBoolean() toBeBoolean()
31: * @method boolean toBeAnInstanceOf() toBeAnInstanceOf(string $expected)
32: * @method boolean toThrow() toThrow(string $expected)
33: * @method boolean toHaveLength() toHaveLength(integer $expected)
34: * @method boolean toBeEmpty() toBeEmpty()
35: * @method boolean toPrint() toPrint(string $expected)
36: * @method boolean toMatch() toMatch(string $expected)
37: * @method boolean toStartWith() toStartWith(string $expected)
38: * @method boolean toEndWith() toEndWith(string $expected)
39: * @method boolean toContain() toContain(string $expected)
40: * @method boolean toHaveKey() toHaveKey(string $expected)
41: * @method boolean toBeGreaterThan() toBeGreaterThan(integer $expected)
42: * @method boolean toBeLessThan() toBeLessThan(integer $expected)
43: * @method boolean toBeAbove() toBeAbove(integer $expected)
44: * @method boolean toBeBelow() toBeBelow(integer $expected)
45: * @method boolean toBeWithin() toBeWithin(integer $from, integer $to)
46: *
47: * @author Noritaka Horio <holy.shared.design@gmail.com>
48: * @copyright Noritaka Horio <holy.shared.design@gmail.com>
49: */
50: class MatcherEvaluator implements Evaluator
51: {
52: /**
53: * @var \expect\matcher\ReportableMatcher
54: */
55: private $matcher;
56:
57: /**
58: * @var bool
59: */
60: private $negated;
61:
62: /**
63: * Create a matcher evaluator.
64: *
65: * @param \expect\matcher\ReportableMatcher $matcher The use matcher
66: */
67: public function __construct(ReportableMatcher $matcher)
68: {
69: $this->negated = false;
70: $this->matcher = $matcher;
71: }
72:
73: /**
74: * Change state to nagative evaluation.
75: *
76: * @return MatcherEvaluator
77: */
78: public function negated()
79: {
80: $this->negated = true;
81:
82: return $this;
83: }
84:
85: /**
86: * Evaluate the value of actual.
87: *
88: * @param mixed $actual value of actual
89: *
90: * @return \expect\Result
91: */
92: public function evaluate($actual)
93: {
94: $matcherResult = $this->matcher->match($actual);
95: $expected = $this->negated ? false : true;
96:
97: $result = $matcherResult === $expected;
98:
99: return new Result($actual, $this->negated, $this->matcher, $result);
100: }
101:
102: /**
103: * {@inheritdoc}
104: */
105: public static function fromMatcher(ReportableMatcher $matcher)
106: {
107: return new self($matcher);
108: }
109: }
110: