Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,64 @@ $matcher->match(
);
```

### Json matching with unbounded arrays and objects

```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;

$factory = new SimpleFactory();
$matcher = $factory->createMatcher();

$matcher->match(
'{
"users":[
{
"firstName": "Norbert",
"lastName": "Orzechowicz",
"created": "2014-01-01",
"roles":["ROLE_USER", "ROLE_DEVELOPER"]},
"attributes": {
"isAdmin": false,
"dateOfBirth" null,
"hasEmailVerified": true
}
},
{
"firstName": "Michał",
"lastName": "Dąbrowski",
"created": "2014-01-01",
"roles":["ROLE_USER", "ROLE_DEVELOPER", "ROLE_ADMIN"]},
"attributes": {
"isAdmin": true,
"dateOfBirth" null,
"hasEmailVerified": true
}
}
]
}',
'{
"users":[
{
"firstName": @string@,
"lastName": @string@,
"created": "@string@.isDateTime()",
"roles": [
"ROLE_USER",
@...@
],
"attributes": {
"isAdmin": @boolean@,
"@*@": "@*@"
}
}
],
@...@
}'
);
```

### Xml matching

```php
Expand Down
2 changes: 1 addition & 1 deletion src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function getCatchablePatterns() : array
'\\-?[0-9]*\\.?[0-9]*', // numbers
"'(?:[^']|'')*'", // string between ' character
'"(?:[^"]|"")*"', // string between " character,
'@[a-zA-Z0-9\\*]+@', // type pattern
'@[a-zA-Z0-9\\*.]+@', // type pattern
];
}

Expand Down
17 changes: 12 additions & 5 deletions src/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class ArrayMatcher extends Matcher
{
const PATTERN = 'array';
const UNBOUNDED_PATTERN = '@...@';
const UNIVERSAL_KEY = '@*@';

private $propertyMatcher;

Expand Down Expand Up @@ -70,18 +71,20 @@ private function iterateMatch(array $values, array $patterns, string $parentPath
foreach ($values as $key => $value) {
$path = $this->formatAccessPath($key);

if ($this->shouldSkippValueMatchingFor($pattern)) {
if ($this->shouldSkipValueMatchingFor($pattern)) {
continue;
}

if ($this->valueExist($path, $patterns)) {
$pattern = $this->getValueByPath($patterns, $path);
} elseif (isset($patterns[self::UNIVERSAL_KEY])) {
$pattern = $patterns[self::UNIVERSAL_KEY];
} else {
$this->setMissingElementInError('pattern', $this->formatFullPath($parentPath, $path));
return false;
}

if ($this->shouldSkippValueMatchingFor($pattern)) {
if ($this->shouldSkipValueMatchingFor($pattern)) {
continue;
}

Expand Down Expand Up @@ -138,9 +141,13 @@ function ($item) use ($skipPattern) {
return true;
}

private function findNotExistingKeys(array $pattern, array $values) : array
private function findNotExistingKeys(array $patterns, array $values) : array
{
$notExistingKeys = \array_diff_key($pattern, $values);
if (isset($patterns[self::UNIVERSAL_KEY])) {
return [];
}

$notExistingKeys = \array_diff_key($patterns, $values);

return \array_filter($notExistingKeys, function ($pattern) use ($values) {
if (\is_array($pattern)) {
Expand Down Expand Up @@ -230,7 +237,7 @@ private function formatFullPath(string $parentPath, string $path) : string
return \sprintf('%s%s', $parentPath, $path);
}

private function shouldSkippValueMatchingFor($lastPattern) : bool
private function shouldSkipValueMatchingFor($lastPattern) : bool
{
return $lastPattern === self::UNBOUNDED_PATTERN;
}
Expand Down
1 change: 1 addition & 0 deletions tests/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public static function validMatcherTypePatterns()
['@integer@'],
['@number@'],
['@*@'],
['@...@'],
['@wildcard@']
];
}
Expand Down
45 changes: 45 additions & 0 deletions tests/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,39 @@ public static function positiveMatchData()
6.66
];

$simpleArrPatternWithUniversalKey = [
'users' => [
[
'firstName' => '@string@',
Matcher\ArrayMatcher::UNIVERSAL_KEY => '@*@'
],
Matcher\ArrayMatcher::UNBOUNDED_PATTERN
],
true,
false,
1,
6.66
];

$simpleArrPatternWithUniversalKeyAndStringValue = [
'users' => [
[
'firstName' => '@string@',
Matcher\ArrayMatcher::UNIVERSAL_KEY => '@string@'
],
Matcher\ArrayMatcher::UNBOUNDED_PATTERN
],
true,
false,
1,
6.66
];

return [
[$simpleArr, $simpleArr],
[$simpleArr, $simpleArrPattern],
[$simpleArr, $simpleArrPatternWithUniversalKey],
[$simpleArr, $simpleArrPatternWithUniversalKeyAndStringValue],
[[], []],
[['foo' => null], ['foo' => null]],
[['foo' => null], ['foo' => '@null@']],
Expand Down Expand Up @@ -224,8 +254,23 @@ public static function negativeMatchData()
6.66
];

$simpleArrPatternWithUniversalKeyAndIntegerValue = [
'users' => [
[
'firstName' => '@string@',
Matcher\ArrayMatcher::UNIVERSAL_KEY => '@integer@'
],
Matcher\ArrayMatcher::UNBOUNDED_PATTERN
],
true,
false,
1,
6.66
];

return [
[$simpleArr, $simpleDiff],
[$simpleArr, $simpleArrPatternWithUniversalKeyAndIntegerValue],
[['status' => 'ok', 'data' => [['foo']]], ['status' => 'ok', 'data' => []]],
[[1], []],
[['key' => 'val'], ['key' => 'val2']],
Expand Down
12 changes: 12 additions & 0 deletions tests/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,21 @@ public static function positiveMatches()
'{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":["ROLE_USER", "ROLE_DEVELOPER"]}]}',
'{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":"@wildcard@"}]}'
],
[
'{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":["ROLE_USER", "ROLE_DEVELOPER"]}]}',
'{"users":[{"firstName":"Norbert","@*@":"@*@"}]}'
],
[
'{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":["ROLE_USER", "ROLE_DEVELOPER"]},{}]}',
'{"users":[{"firstName":"Norbert","@*@":"@*@"},@...@]}'
],
[
'[{"name": "Norbert"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
'[{"name": "Norbert"},@...@]'
],
[
'[{"name": "Norbert","lastName":"Orzechowicz"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
'[{"name": "Norbert","@*@":"@*@"},@...@]'
]
];
}
Expand Down