Skip to content

Commit

Permalink
Array universal key (#143)
Browse files Browse the repository at this point in the history
* Array universal key

* Remove unwanted Exception

* add test and remove dump

* revert invalid array key check

* remove not needed code samples

* Add more tests

* Update Readme with Universal Array Key example
  • Loading branch information
JarJak authored and norberttech committed Feb 8, 2019
1 parent 44ba53e commit 300c41a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 6 deletions.
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": "@[email protected]()",
"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

0 comments on commit 300c41a

Please sign in to comment.