Completed
Push — master ( 6bbd38...55e0d1 )
by Erin
10s
created

InclusionMatcher::doMatch()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 6
eloc 13
c 2
b 0
f 1
nc 6
nop 1
dl 0
loc 26
rs 8.439
1
<?php
2
3
namespace Peridot\Leo\Matcher;
4
5
use InvalidArgumentException;
6
use Peridot\Leo\Matcher\Template\ArrayTemplate;
7
use Peridot\Leo\Matcher\Template\TemplateInterface;
8
use Traversable;
9
10
/**
11
 * InclusionMatcher determines if an array or string includes the expected value.
12
 *
13
 * @package Peridot\Leo\Matcher
14
 */
15
class InclusionMatcher extends AbstractMatcher
16
{
17
    /**
18
     * Matches if an array or string contains the expected value.
19
     *
20
     * @param $actual
21
     * @return mixed
22
     * @throws InvalidArgumentException
23
     */
24
    protected function doMatch($actual)
25
    {
26
        if ($actual instanceof Traversable) {
27
            $isMatch = false;
28
29
            foreach ($actual as $value) {
30
                if ($value === $this->expected) {
31
                    $isMatch = true;
32
33
                    break;
34
                }
35
            }
36
37
            return $isMatch;
38
        }
39
40
        if (is_array($actual)) {
41
            return array_search($this->expected, $actual, true) !== false;
42
        }
43
44
        if (is_string($actual)) {
45
            return strpos($actual, $this->expected) !== false;
46
        }
47
48
        throw new InvalidArgumentException("Inclusion matcher requires a string or array");
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @return TemplateInterface
55
     */
56
    public function getDefaultTemplate()
57
    {
58
        return new ArrayTemplate([
59
            'default' => 'Expected {{actual}} to include {{expected}}',
60
            'negated' => 'Expected {{actual}} to not include {{expected}}'
61
        ]);
62
    }
63
}
64