Skip to content

Matcher modifiers #132

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

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 17 additions & 0 deletions src/Exception/UnknownModifierClassException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class UnknownModifierClassException extends \Exception
{
public function __construct(string $name, string $class)
{
parent::__construct(\sprintf(
'Unknown class %s for modifier %s',
$class,
$name
));
}
}
9 changes: 9 additions & 0 deletions src/Exception/UnknownModifierException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Exception;

class UnknownModifierException extends Exception
{
}
8 changes: 5 additions & 3 deletions src/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ protected function buildMatchers() : Matcher\ChainMatcher
$scalarMatchers = $this->buildScalarMatchers();
$orMatcher = $this->buildOrMatcher();

$parser = $this->buildParser();

$chainMatcher = new Matcher\ChainMatcher([
$scalarMatchers,
$orMatcher,
new Matcher\JsonMatcher($orMatcher),
new Matcher\JsonMatcher($orMatcher, $parser),
new Matcher\XmlMatcher($orMatcher),
new Matcher\TextMatcher($scalarMatchers, $this->buildParser())
new Matcher\TextMatcher($scalarMatchers, $parser)
]);

return $chainMatcher;
Expand Down Expand Up @@ -76,6 +78,6 @@ protected function buildScalarMatchers() : Matcher\ChainMatcher

protected function buildParser() : Parser
{
return new Parser(new Lexer(), new Parser\ExpanderInitializer());
return new Parser(new Lexer(), new Parser\ExpanderInitializer(), new Parser\ModifiersRegistry());
}
}
11 changes: 11 additions & 0 deletions src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class Lexer extends AbstractLexer
const T_COMMA = 10;
const T_COLON = 11;
const T_TYPE_PATTERN = 12;
const T_MODIFIERS_NAMES = 13;

/**
* Lexical catchable patterns.
Expand All @@ -33,6 +34,7 @@ protected function getCatchablePatterns() : array
"'(?:[^']|'')*'", // string between ' character
'"(?:[^"]|"")*"', // string between " character,
'@[a-zA-Z0-9\\*]+@', // type pattern
Parser::MODIFIERS_REGEX, // modifiers names
];
}

Expand Down Expand Up @@ -102,6 +104,10 @@ protected function getType(&$value) : int
return self::T_EXPANDER_NAME;
}

if ($this->isModifierNamesToken($value)) {
return self::T_MODIFIERS_NAMES;
}

return $type;
}

Expand Down Expand Up @@ -134,4 +140,9 @@ protected function isTypePatternToken(string $value) : bool
{
return \substr($value, 0, 1) === '@' && \substr($value, \strlen($value) - 1, 1) === '@' && \strlen($value) > 1;
}

protected function isModifierNamesToken(string $value) : bool
{
return \substr($value, 0, 1) === '|' && \substr($value, \strlen($value) - 1, 1) === '|' && \strlen($value) > 2;
}
}
44 changes: 39 additions & 5 deletions src/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@
namespace Coduo\PHPMatcher\Matcher;

use Coduo\PHPMatcher\Exception\Exception;
use Coduo\PHPMatcher\Matcher\Modifier\CaseInsensitive;
use Coduo\PHPMatcher\Matcher\Modifier\IgnoreExtraKeys;
use Coduo\PHPMatcher\Matcher\Modifier\MatcherModifier;
use Coduo\PHPMatcher\Parser;
use Coduo\ToString\StringConverter;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\PropertyPath;

final class ArrayMatcher extends Matcher
final class ArrayMatcher extends ModifiableMatcher
{
const PATTERN = 'array';
const UNBOUNDED_PATTERN = '@...@';

const SUPPORTED_MODIFIERS = [
IgnoreExtraKeys::NAME,
CaseInsensitive::NAME
];

private $propertyMatcher;

private $accessor;

private $parser;

/**
* @var bool
*/
private $ignoreExtraKeys;

public function __construct(ValueMatcher $propertyMatcher, Parser $parser)
{
$this->propertyMatcher = $propertyMatcher;
$this->parser = $parser;

$this->ignoreExtraKeys = false;
}

public function match($value, $pattern) : bool
Expand Down Expand Up @@ -55,13 +70,32 @@ public function canMatch($pattern) : bool
return \is_array($pattern) || $this->isArrayPattern($pattern);
}

public function supportedModifiers(): array
{
return self::SUPPORTED_MODIFIERS;
}

public function getMatchers(): array
{
return [$this->propertyMatcher];
}

public function applyModifier(MatcherModifier $modifier)
{
switch ($modifier->getName()) {
case IgnoreExtraKeys::NAME:
$this->ignoreExtraKeys = true;
break;
}
}

private function isArrayPattern($pattern) : bool
{
if (!\is_string($pattern)) {
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
return $this->parser->hasValidSyntax($pattern) && $this->parser->parseTypePattern($pattern)->is(self::PATTERN);
}

private function iterateMatch(array $values, array $patterns, string $parentPath = '') : bool
Expand Down Expand Up @@ -126,7 +160,7 @@ function ($item) use ($skipPattern) {
);

$notExistingKeys = $this->findNotExistingKeys($pattern, $values);
if (\count($notExistingKeys) > 0) {
if (\count($notExistingKeys) > 0 && !$this->ignoreExtraKeys) {
$keyNames = \array_keys($notExistingKeys);
$path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
$this->setMissingElementInError('value', $path);
Expand All @@ -148,7 +182,7 @@ private function findNotExistingKeys(array $pattern, array $values) : array
}

try {
$typePattern = $this->parser->parse($pattern);
$typePattern = $this->parser->parseTypePattern($pattern);
} catch (Exception $e) {
return true;
} catch (\Throwable $t) {
Expand Down Expand Up @@ -237,7 +271,7 @@ private function shouldSkippValueMatchingFor($lastPattern) : bool

private function allExpandersMatch($value, $pattern) : bool
{
$typePattern = $this->parser->parse($pattern);
$typePattern = $this->parser->parseTypePattern($pattern);
if (!$typePattern->matchExpanders($value)) {
$this->error = $typePattern->getError();
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/BooleanMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function canMatch($pattern) : bool
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
return $this->parser->hasValidSyntax($pattern) && $this->parser->parseTypePattern($pattern)->is(self::PATTERN);
}
}
19 changes: 18 additions & 1 deletion src/Matcher/ChainMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Coduo\PHPMatcher\Matcher;

use Coduo\PHPMatcher\Matcher\Modifier\MatcherModifier;
use Coduo\PHPMatcher\Parser\ModifiersRegistry;
use Coduo\ToString\StringConverter;

final class ChainMatcher extends Matcher
final class ChainMatcher extends ModifiableMatcher
{
/**
* @var ValueMatcher[]
Expand Down Expand Up @@ -53,4 +55,19 @@ public function canMatch($pattern) : bool
{
return true;
}

public function supportedModifiers(): array
{
return \array_keys(ModifiersRegistry::BUILT_IN_MODIFIERS);
}

public function getMatchers(): array
{
return $this->matchers;
}

public function applyModifier(MatcherModifier $modifier)
{

}
}
4 changes: 2 additions & 2 deletions src/Matcher/DoubleMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function match($value, $pattern) : bool
return false;
}

$typePattern = $this->parser->parse($pattern);
$typePattern = $this->parser->parseTypePattern($pattern);
if (!$typePattern->matchExpanders($value)) {
$this->error = $typePattern->getError();
return false;
Expand All @@ -40,6 +40,6 @@ public function canMatch($pattern) : bool
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
return $this->parser->hasValidSyntax($pattern) && $this->parser->parseTypePattern($pattern)->is(self::PATTERN);
}
}
4 changes: 2 additions & 2 deletions src/Matcher/IntegerMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function match($value, $pattern) : bool
return false;
}

$typePattern = $this->parser->parse($pattern);
$typePattern = $this->parser->parseTypePattern($pattern);
if (!$typePattern->matchExpanders($value)) {
$this->error = $typePattern->getError();
return false;
Expand All @@ -40,6 +40,6 @@ public function canMatch($pattern) : bool
return false;
}

return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is(self::PATTERN);
return $this->parser->hasValidSyntax($pattern) && $this->parser->parseTypePattern($pattern)->is(self::PATTERN);
}
}
46 changes: 44 additions & 2 deletions src/Matcher/JsonMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@

namespace Coduo\PHPMatcher\Matcher;

use Coduo\PHPMatcher\Matcher\Modifier\CaseInsensitive;
use Coduo\PHPMatcher\Matcher\Modifier\IgnoreExtraKeys;
use Coduo\PHPMatcher\Matcher\Modifier\MatcherModifier;
use Coduo\PHPMatcher\Matcher\Pattern\Assert\Json;
use Coduo\PHPMatcher\Parser;

final class JsonMatcher extends Matcher
final class JsonMatcher extends ModifiableMatcher
{
const SUPPORTED_MODIFIERS = [
IgnoreExtraKeys::NAME,
CaseInsensitive::NAME
];

/**
* @var ValueMatcher
*/
private $matcher;

public function __construct(ValueMatcher $matcher)
/**
* @var Parser
*/
private $parser;

public function __construct(ValueMatcher $matcher, Parser $parser)
{
$this->matcher = $matcher;
$this->parser = $parser;
}

public function match($value, $pattern) : bool
Expand All @@ -21,6 +39,12 @@ public function match($value, $pattern) : bool
return true;
}

foreach ($this->parser->parseModifiers($pattern) as $modifier) {
$this->modify($modifier);
}

$pattern = $this->parser->trimModifiers($pattern);

if (!Json::isValid($value)) {
$this->error = \sprintf('Invalid given JSON of value. %s', $this->getErrorMessage());
return false;
Expand All @@ -43,9 +67,27 @@ public function match($value, $pattern) : bool

public function canMatch($pattern) : bool
{
if (\is_string($pattern)) {
$pattern = $this->parser->trimModifiers($pattern);
}

return Json::isValidPattern($pattern);
}

public function supportedModifiers(): array
{
return \array_keys(self::SUPPORTED_MODIFIERS);
}

public function getMatchers(): array
{
return [$this->matcher];
}

public function applyModifier(MatcherModifier $modifier)
{
}

private function getErrorMessage()
{
switch (\json_last_error()) {
Expand Down
Loading