1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Phiremock. |
4
|
|
|
* |
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Domain\Condition\Matchers; |
20
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Common\Utils\ArraysHelper; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Condition\StringValue; |
23
|
|
|
|
24
|
|
|
class JsonContains extends Matcher |
25
|
|
|
{ |
26
|
|
|
public function __construct(StringValue $string) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($string); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function matches($value): bool |
32
|
|
|
{ |
33
|
|
|
if (\is_string($value)) { |
34
|
|
|
$requestValue = $this->getParsedValue($value); |
35
|
|
|
} else { |
36
|
|
|
$requestValue = $value; |
37
|
|
|
} |
38
|
|
|
$configValue = $this->decodeJson($this->getCheckValue()->get()); |
39
|
|
|
|
40
|
|
|
if (!\is_array($requestValue) || !\is_array($configValue)) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return ArraysHelper::arrayIsContained($requestValue, $configValue); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getName(): string |
48
|
|
|
{ |
49
|
|
|
return 'jsonContains'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function decodeJson(string $value): array |
53
|
|
|
{ |
54
|
|
|
$decodedValue = json_decode($value, true); |
55
|
|
|
if (\JSON_ERROR_NONE !== json_last_error() || $decodedValue === null) { |
56
|
|
|
throw new \InvalidArgumentException('JSON parsing error: ' . json_last_error_msg()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $decodedValue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function getParsedValue(string $value) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
$requestValue = $this->decodeJson($value); |
66
|
|
|
} catch (\Throwable $e) { |
67
|
|
|
$requestValue = $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $requestValue; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|