1: <?php
2:
3: namespace Peridot\Leo\Matcher;
4:
5: use ArrayAccess;
6: use InvalidArgumentException;
7: use Peridot\Leo\Matcher\Template\ArrayTemplate;
8: use Peridot\Leo\Matcher\Template\TemplateInterface;
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: //we will support ArrayAccess for now, even though array_search throws a warning about it
27: if (is_array($actual) or $actual instanceof ArrayAccess) {
28: return array_search($this->expected, $actual, true) !== false;
29: }
30:
31: if (is_string($actual)) {
32: return strpos($actual, $this->expected) !== false;
33: }
34:
35: throw new InvalidArgumentException("Inclusion matcher requires a string or array");
36: }
37:
38: /**
39: * {@inheritdoc}
40: *
41: * @return TemplateInterface
42: */
43: public function getDefaultTemplate()
44: {
45: return new ArrayTemplate([
46: 'default' => 'Expected {{actual}} to include {{expected}}',
47: 'negated' => 'Expected {{actual}} to not include {{expected}}'
48: ]);
49: }
50: }
51: