Skip to content

Allow to mark JSON properties & XML nodes as optional #102

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 12 commits into from
Aug 2, 2017
Merged
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ $matcher->getError(); // returns null or error message
* ``inArray($value)``
* ``oneOf(...$expanders)`` - example usage ``"@string@.oneOf(contains('foo'), contains('bar'), contains('baz'))"``
* ``matchRegex($regex)`` - example usage ``"@string@.matchRegex('/^lorem.+/')"``
* ``optional()`` - work's only with ``ArrayMatcher``, ``JsonMatcher`` and ``XmlMatcher``

##Example usage

Expand Down Expand Up @@ -237,7 +238,8 @@ $matcher->match(
'id' => 1,
'firstName' => 'Norbert',
'lastName' => 'Orzechowicz',
'roles' => array('ROLE_USER')
'roles' => array('ROLE_USER'),
'position' => 'Developer',
),
array(
'id' => 2,
Expand All @@ -261,7 +263,8 @@ $matcher->match(
'id' => '@integer@.greaterThan(0)',
'firstName' => '@string@',
'lastName' => 'Orzechowicz',
'roles' => '@array@'
'roles' => '@array@',
'position' => '@string@.optional()'
),
array(
'id' => '@integer@',
Expand Down Expand Up @@ -303,7 +306,8 @@ $matcher->match(
"firstName": @string@,
"lastName": @string@,
"created": "@string@.isDateTime()",
"roles": @array@
"roles": @array@,
"posiiton": "@string@.optional()"
}
]
}'
Expand Down Expand Up @@ -347,6 +351,7 @@ XML
<m:GetStockPrice>
<m:StockName>@string@</m:StockName>
<m:StockValue>@string@</m:StockValue>
<m:StockQty>@integer@.optional()</m:StockQty>
</m:GetStockPrice>
</soap:Body>

Expand Down
32 changes: 30 additions & 2 deletions src/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Coduo\PHPMatcher\Matcher;

use Coduo\PHPMatcher\Exception\Exception;
use Coduo\PHPMatcher\Parser;
use Coduo\ToString\StringConverter;
use Symfony\Component\PropertyAccess\PropertyAccess;
Expand Down Expand Up @@ -153,19 +154,46 @@ function ($item) use ($skipPattern) {
}
);

$notExistingKeys = array_diff_key($pattern, $values);

$notExistingKeys = $this->findNotExistingKeys($pattern, $values);
if (count($notExistingKeys) > 0) {
$keyNames = array_keys($notExistingKeys);
$path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
$this->setMissingElementInError('value', $path);

return false;
}
}

return true;
}

/**
* Finds not existing keys
* Excludes keys with pattern which includes Optional Expander
*
* @param $pattern
* @param $values
* @return array
*/
private function findNotExistingKeys($pattern, $values)
{
$notExistingKeys = array_diff_key($pattern, $values);

return array_filter($notExistingKeys, function ($pattern) use ($values) {
if (is_array($pattern)) {
return !$this->match($values, $pattern);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@norzechowicz I'm not pretty sure that we must check if values match to pattern because it already hasn't array_diff_key($pattern, $values) and also because OptionalExpander works only with top level match

}

try {
$typePattern = $this->parser->parse($pattern);
} catch (Exception $e) {
return true;
}

return !$typePattern->hasExpander('optional');
});
}

/**
* @param $value
* @param $pattern
Expand Down
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'contains';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'count';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/EndsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,12 @@ protected function matchValue($value)
? mb_substr(mb_strtolower($value), -mb_strlen(mb_strtolower($this->stringEnding))) === mb_strtolower($this->stringEnding)
: mb_substr($value, -mb_strlen($this->stringEnding)) === $this->stringEnding;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'endsWith';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'greaterThan';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/InArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'inArray';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please make constant from those names? For example:

InArray::NAME

This should allow to use constants here https://github.com/coduo/php-matcher/blob/master/src/Parser/ExpanderInitializer.php#L18

You can also replace "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\StartsWith" with StartsWith::class

}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/IsDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ protected function matchValue($value)
return false;
}
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'isDateTime';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/IsEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ protected function matchValue($value)
return false;
}
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'isEmail';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/IsEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'isEmpty';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/IsNotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'isNotEmpty';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/IsUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ protected function matchValue($value)
return false;
}
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'isUrl';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/LowerThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'lowerThan';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/MatchRegex.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'matchRegex';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/OneOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'oneOf';
}
}
32 changes: 32 additions & 0 deletions src/Matcher/Pattern/Expander/Optional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;

final class Optional implements PatternExpander
{
/**
* {@inheritdoc}
*/
public function match($value)
{
return true;
}

/**
* {@inheritdoc}
*/
public function getError()
{
return null;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'optional';
}
}
8 changes: 8 additions & 0 deletions src/Matcher/Pattern/Expander/StartsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function getError()
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'startsWith';
}

/**
* @param $value
* @return bool
Expand Down
9 changes: 9 additions & 0 deletions src/Matcher/Pattern/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ public function matchExpanders($value);
* @return null|string
*/
public function getError();

/**
* Checks whether a Pattern has added Expander.
*
* @param string $expanderName The name of the expander
*
* @return bool true if the specified pattern has expander, false otherwise
*/
public function hasExpander(string $expanderName);
}
7 changes: 7 additions & 0 deletions src/Matcher/Pattern/PatternExpander.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public function match($value);
* @return string|null
*/
public function getError();

/**
* Returns the name by which the expander is identified.
*
* @return string The name of the expander
*/
public function getName();
}
16 changes: 15 additions & 1 deletion src/Matcher/Pattern/TypePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,24 @@ public function matchExpanders($value)
}

/**
* @return null|string
* {@inheritdoc}
*/
public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function hasExpander(string $expanderName)
{
foreach ($this->expanders as $expander) {
if ($expander->getName() === $expanderName) {
return true;
}
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/Parser/ExpanderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ExpanderInitializer
"count" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Count",
"contains" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Contains",
"matchRegex" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\MatchRegex",

"optional" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Optional",
"oneOf" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\OneOf"
);

Expand Down
Loading