@@ 8-54 (lines=47) @@ | ||
5 | use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; |
|
6 | use Coduo\ToString\StringConverter; |
|
7 | ||
8 | final class Count implements PatternExpander |
|
9 | { |
|
10 | /** |
|
11 | * @var null|string |
|
12 | */ |
|
13 | private $error; |
|
14 | ||
15 | /** |
|
16 | * @var |
|
17 | */ |
|
18 | private $value; |
|
19 | ||
20 | /** |
|
21 | * @param $value |
|
22 | */ |
|
23 | public function __construct($value) |
|
24 | { |
|
25 | $this->value = $value; |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param $value |
|
30 | * @return boolean |
|
31 | */ |
|
32 | public function match($value) |
|
33 | { |
|
34 | if (!is_array($value)) { |
|
35 | $this->error = sprintf("Count expander require \"array\", got \"%s\".", new StringConverter($value)); |
|
36 | return false; |
|
37 | } |
|
38 | ||
39 | if (count($value) !== $this->value) { |
|
40 | $this->error = sprintf("Expected count of %s is %s.", new StringConverter($value), new StringConverter($this->value)); |
|
41 | return false; |
|
42 | } |
|
43 | ||
44 | return true; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return string|null |
|
49 | */ |
|
50 | public function getError() |
|
51 | { |
|
52 | return $this->error; |
|
53 | } |
|
54 | } |
|
55 |
@@ 8-54 (lines=47) @@ | ||
5 | use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; |
|
6 | use Coduo\ToString\StringConverter; |
|
7 | ||
8 | final class InArray implements PatternExpander |
|
9 | { |
|
10 | /** |
|
11 | * @var null|string |
|
12 | */ |
|
13 | private $error; |
|
14 | ||
15 | /** |
|
16 | * @var |
|
17 | */ |
|
18 | private $value; |
|
19 | ||
20 | /** |
|
21 | * @param $value |
|
22 | */ |
|
23 | public function __construct($value) |
|
24 | { |
|
25 | $this->value = $value; |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param $value |
|
30 | * @return boolean |
|
31 | */ |
|
32 | public function match($value) |
|
33 | { |
|
34 | if (!is_array($value)) { |
|
35 | $this->error = sprintf("InArray expander require \"array\", got \"%s\".", new StringConverter($value)); |
|
36 | return false; |
|
37 | } |
|
38 | ||
39 | if (!in_array($this->value, $value, true)) { |
|
40 | $this->error = sprintf("%s doesn't have \"%s\" element.", new StringConverter($value), new StringConverter($this->value)); |
|
41 | return false; |
|
42 | } |
|
43 | ||
44 | return true; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return string|null |
|
49 | */ |
|
50 | public function getError() |
|
51 | { |
|
52 | return $this->error; |
|
53 | } |
|
54 | } |
|
55 |