Skip to content

Commit 97d08f8

Browse files
authored
Added match expander that allows to match nullable @JSON@ objects (#173)
* Added match expander that allows to match nullable @JSON@ objects * Added failing scenario * Fixed invalid test example * Added another example of optional json element
1 parent f4232a5 commit 97d08f8

28 files changed

+96
-36
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ $matcher->getError(); // returns null or error message
6868
* ``@*@`` || ``@wildcard@``
6969
* ``expr(expression)``
7070
* ``@uuid@``
71+
* ``@json@``
7172
* ``@strig@||@integer@`` - string OR integer
7273

7374
### Available pattern expanders
@@ -85,11 +86,12 @@ $matcher->getError(); // returns null or error message
8586
* ``lowerThan($boundry)``
8687
* ``greaterThan($boundry)``
8788
* ``inArray($value)``
88-
* ``oneOf(...$expanders)`` - example usage ``"@[email protected](contains('foo'), contains('bar'), contains('baz'))"``
89-
* ``matchRegex($regex)`` - example usage ``"@[email protected]('/^lorem.+/')"``
89+
* ``oneOf(...$expanders)`` - example ``"@[email protected](contains('foo'), contains('bar'), contains('baz'))"``
90+
* ``matchRegex($regex)`` - example ``"@[email protected]('/^lorem.+/')"``
9091
* ``optional()`` - work's only with ``ArrayMatcher``, ``JsonMatcher`` and ``XmlMatcher``
91-
* ``count()`` - work's only with ``ArrayMatcher`` - example usage ``"@[email protected](5)"``
92-
* ``repeat($pattern, $isStrict = true)`` - example usage ``'@[email protected]({"name": "foe"})'`` or ``"@[email protected]('@string@')"``
92+
* ``count()`` - work's only with ``ArrayMatcher`` - example ``"@[email protected](5)"``
93+
* ``repeat($pattern, $isStrict = true)`` - example ``'@[email protected]({"name": "foe"})'`` or ``"@[email protected]('@string@')"``
94+
* ``match($pattern)`` - example ``{"image":"@[email protected]({\"url\":\"@[email protected]()\"})"}``
9395

9496
## Example usage
9597

@@ -358,6 +360,9 @@ $matcher->match(
358360
"isAdmin": false,
359361
"dateOfBirth" null,
360362
"hasEmailVerified": true
363+
},
364+
"avatar": {
365+
"url": "http://avatar-image.com/avatar.png"
361366
}
362367
},
363368
{
@@ -369,7 +374,8 @@ $matcher->match(
369374
"isAdmin": true,
370375
"dateOfBirth" null,
371376
"hasEmailVerified": true
372-
}
377+
},
378+
"avatar": null
373379
}
374380
]
375381
}',
@@ -386,7 +392,8 @@ $matcher->match(
386392
"attributes": {
387393
"isAdmin": @boolean@,
388394
"@*@": "@*@"
389-
}
395+
},
396+
"avatar": "@[email protected]({\"url\":\"@[email protected]()\"})"
390397
}
391398
],
392399
@...@

src/Factory/MatcherFactory.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ protected function buildMatchers(Parser $parser) : Matcher\ChainMatcher
2121
$scalarMatchers = $this->buildScalarMatchers($parser);
2222
$arrayMatcher = $this->buildArrayMatcher($scalarMatchers, $parser);
2323

24+
// Matchers are registered in order of matching
25+
// 1) all scalars
26+
// 2) json/xml
27+
// 3) array
28+
// 4) or "||"
29+
// 5) full text
30+
2431
$chainMatcher = new Matcher\ChainMatcher([
2532
$scalarMatchers,
26-
$arrayMatcher,
27-
new Matcher\OrMatcher($scalarMatchers),
2833
new Matcher\JsonMatcher($arrayMatcher),
2934
new Matcher\XmlMatcher($arrayMatcher),
30-
new Matcher\TextMatcher($scalarMatchers, $parser)
35+
$arrayMatcher,
36+
new Matcher\OrMatcher($scalarMatchers),
37+
new Matcher\TextMatcher($scalarMatchers, $parser),
3138
]);
3239

3340
return $chainMatcher;
@@ -36,16 +43,15 @@ protected function buildMatchers(Parser $parser) : Matcher\ChainMatcher
3643
protected function buildArrayMatcher(Matcher\ChainMatcher $scalarMatchers, Parser $parser) : Matcher\ArrayMatcher
3744
{
3845
$orMatcher = new Matcher\OrMatcher($scalarMatchers);
39-
$arrayMatcher = new Matcher\ArrayMatcher(
46+
47+
return new Matcher\ArrayMatcher(
4048
new Matcher\ChainMatcher([
4149
$orMatcher,
4250
$scalarMatchers,
4351
new Matcher\TextMatcher($scalarMatchers, $parser)
4452
]),
4553
$parser
4654
);
47-
48-
return $arrayMatcher;
4955
}
5056

5157
protected function buildScalarMatchers(Parser $parser) : Matcher\ChainMatcher

src/Matcher/JsonMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function match($value, $pattern) : bool
3232
return false;
3333
}
3434

35-
$transformedPattern = Json::transformPattern($pattern);
35+
$transformedPattern = Json::isValid($pattern) ? $pattern : Json::transformPattern($pattern);
3636

3737
$match = $this->arrayMatcher->match(\json_decode($value, true), \json_decode($transformedPattern, true));
3838

src/Matcher/Pattern/Assert/Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function isValidPattern($value) : bool
2929
return false;
3030
}
3131

32-
return self::isValid(self::transformPattern($value));
32+
return self::isValid($value) || self::isValid(self::transformPattern($value));
3333
}
3434

3535
public static function transformPattern(string $pattern) : string

src/Matcher/Pattern/Expander/After.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private function is_datetime(string $value) : bool
6767
}
6868
}
6969

70-
public function getError()
70+
public function getError() : ?string
7171
{
7272
return $this->error;
7373
}

src/Matcher/Pattern/Expander/Before.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private function is_datetime(string $value) : bool
6666
}
6767
}
6868

69-
public function getError()
69+
public function getError() : ?string
7070
{
7171
return $this->error;
7272
}

src/Matcher/Pattern/Expander/Contains.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function match($value) : bool
4747
return true;
4848
}
4949

50-
public function getError()
50+
public function getError() : ?string
5151
{
5252
return $this->error;
5353
}

src/Matcher/Pattern/Expander/Count.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function match($value) :bool
3939

4040
return true;
4141
}
42-
public function getError()
42+
public function getError() : ?string
4343
{
4444
return $this->error;
4545
}

src/Matcher/Pattern/Expander/EndsWith.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function match($value) : bool
5151
return true;
5252
}
5353

54-
public function getError()
54+
public function getError() : ?string
5555
{
5656
return $this->error;
5757
}

src/Matcher/Pattern/Expander/GreaterThan.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function match($value) : bool
4444
return $value > $this->boundary;
4545
}
4646

47-
public function getError()
47+
public function getError() : ?string
4848
{
4949
return $this->error;
5050
}

0 commit comments

Comments
 (0)