Skip to content

Added match expander that allows to match nullable @json@ objects #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 15, 2019
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
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ $matcher->getError(); // returns null or error message
* ``@*@`` || ``@wildcard@``
* ``expr(expression)``
* ``@uuid@``
* ``@json@``
* ``@strig@||@integer@`` - string OR integer

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

## Example usage

Expand Down Expand Up @@ -358,6 +360,9 @@ $matcher->match(
"isAdmin": false,
"dateOfBirth" null,
"hasEmailVerified": true
},
"avatar": {
"url": "http://avatar-image.com/avatar.png"
}
},
{
Expand All @@ -369,7 +374,8 @@ $matcher->match(
"isAdmin": true,
"dateOfBirth" null,
"hasEmailVerified": true
}
},
"avatar": null
}
]
}',
Expand All @@ -386,7 +392,8 @@ $matcher->match(
"attributes": {
"isAdmin": @boolean@,
"@*@": "@*@"
}
},
"avatar": "@[email protected]({\"url\":\"@[email protected]()\"})"
}
],
@...@
Expand Down
18 changes: 12 additions & 6 deletions src/Factory/MatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ protected function buildMatchers(Parser $parser) : Matcher\ChainMatcher
$scalarMatchers = $this->buildScalarMatchers($parser);
$arrayMatcher = $this->buildArrayMatcher($scalarMatchers, $parser);

// Matchers are registered in order of matching
// 1) all scalars
// 2) json/xml
// 3) array
// 4) or "||"
// 5) full text

$chainMatcher = new Matcher\ChainMatcher([
$scalarMatchers,
$arrayMatcher,
new Matcher\OrMatcher($scalarMatchers),
new Matcher\JsonMatcher($arrayMatcher),
new Matcher\XmlMatcher($arrayMatcher),
new Matcher\TextMatcher($scalarMatchers, $parser)
$arrayMatcher,
new Matcher\OrMatcher($scalarMatchers),
new Matcher\TextMatcher($scalarMatchers, $parser),
]);

return $chainMatcher;
Expand All @@ -36,16 +43,15 @@ protected function buildMatchers(Parser $parser) : Matcher\ChainMatcher
protected function buildArrayMatcher(Matcher\ChainMatcher $scalarMatchers, Parser $parser) : Matcher\ArrayMatcher
{
$orMatcher = new Matcher\OrMatcher($scalarMatchers);
$arrayMatcher = new Matcher\ArrayMatcher(

return new Matcher\ArrayMatcher(
new Matcher\ChainMatcher([
$orMatcher,
$scalarMatchers,
new Matcher\TextMatcher($scalarMatchers, $parser)
]),
$parser
);

return $arrayMatcher;
}

protected function buildScalarMatchers(Parser $parser) : Matcher\ChainMatcher
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/JsonMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function match($value, $pattern) : bool
return false;
}

$transformedPattern = Json::transformPattern($pattern);
$transformedPattern = Json::isValid($pattern) ? $pattern : Json::transformPattern($pattern);

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

Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Assert/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function isValidPattern($value) : bool
return false;
}

return self::isValid(self::transformPattern($value));
return self::isValid($value) || self::isValid(self::transformPattern($value));
}

public static function transformPattern(string $pattern) : string
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/After.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function is_datetime(string $value) : bool
}
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/Before.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function is_datetime(string $value) : bool
}
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function match($value) :bool

return true;
}
public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/EndsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function match($value) : bool
return $value > $this->boundary;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/InArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/IsDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/IsEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/IsEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/IsIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/IsNotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/IsUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/LowerThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function match($value) : bool
return $value < $this->boundary;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
43 changes: 43 additions & 0 deletions src/Matcher/Pattern/Expander/Match.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\Matcher;

final class Match implements Matcher\Pattern\PatternExpander
{
const NAME = 'match';

/**
* @var Matcher
*/
private $matcher;
private $pattern;

public function __construct($pattern)
{
$this->pattern = $pattern;
}

public static function is(string $name) : bool
{
return self::NAME === $name;
}

public function match($value) : bool
{
if (\is_null($this->matcher)) {
$this->matcher = (new MatcherFactory())->createMatcher();
}

return $this->matcher->match($value, $this->pattern);
}

public function getError() : ?string
{
return $this->matcher->getError();
}
}
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/MatchRegex.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/NotContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/OneOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function match($value) : bool
return false;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function match($values) : bool
return $this->matchJson($values, $matcher);
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/Expander/StartsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function match($value) : bool
return true;
}

public function getError()
public function getError() : ?string
{
return $this->error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/Pattern/PatternExpander.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public static function is(string $name) : bool;

public function match($value) : bool;

public function getError();
public function getError() : ?string;
}
1 change: 1 addition & 0 deletions src/Parser/ExpanderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class ExpanderInitializer
Expander\Optional::NAME => Expander\Optional::class,
Expander\StartsWith::NAME => Expander\StartsWith::class,
Expander\Repeat::NAME => Expander\Repeat::class,
Expander\Match::NAME => Expander\Match::class
];

public function setExpanderDefinition(string $expanderName, string $expanderFQCN)
Expand Down
5 changes: 4 additions & 1 deletion tests/ExpandersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setUp() : void
*/
public function test_expanders($value, $pattern, $expectedResult)
{
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern));
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern), (string) $this->matcher->getError());
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern));
}

Expand Down Expand Up @@ -77,6 +77,9 @@ public static function expanderExamples()
['2001:0db8:0000:42a1:0000:0000:ab1c:0001', '@[email protected]()', true],
['127.255.999.999', '@[email protected]()', false],
['foo:bar:42:42', '@[email protected]()', false],
['{"image":{"url":"http://image.com"}}', '{"image":"@[email protected]({\"url\":\"@[email protected]()\"})"}', true],
['{"image":null}', '{"image":"@null@||@[email protected]({\"url\":\"@[email protected]()\"})"}', true],
['{"image":null}', '{"image":"@[email protected](optional(), match({\"url\":\"@[email protected]()\"}) )"}', true],
];
}
}