-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Changes from 7 commits
4c78197
d6ec56c
ff9ab1a
ef29d68
b780687
3de0aab
c8e0045
12c60ce
a454c90
1df7251
a50d669
1088d4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander; | ||
|
||
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; | ||
|
||
final class Optional implements PatternExpander | ||
{ | ||
/** | ||
* @param mixed $value | ||
* | ||
* @return boolean | ||
*/ | ||
public function match($value) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getError() | ||
{ | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
namespace Coduo\PHPMatcher\Matcher\Pattern; | ||
|
||
use Coduo\PHPMatcher\Parser\ExpanderInitializer; | ||
|
||
final class TypePattern implements Pattern | ||
{ | ||
/** | ||
|
@@ -19,13 +21,20 @@ final class TypePattern implements Pattern | |
*/ | ||
private $error; | ||
|
||
/** | ||
* @var ExpanderInitializer | ||
*/ | ||
private $expanderInitializer; | ||
|
||
/** | ||
* @param string $type | ||
* @param ExpanderInitializer $expanderInitializer | ||
*/ | ||
public function __construct($type) | ||
public function __construct($type, ExpanderInitializer $expanderInitializer) | ||
{ | ||
$this->type = $type; | ||
$this->expanders = array(); | ||
$this->expanderInitializer = $expanderInitializer; | ||
} | ||
|
||
/** | ||
|
@@ -69,10 +78,28 @@ 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 (!$this->expanderInitializer->hasExpanderDefinition($expanderName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait, why do we need to use expanderInitializer? Can't we check if expander deos not exists in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid not. Until I have tried, without success, not to used expanderInitializer. Do you have a better idea? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case I would rather like to expose expanderName from expander instance than inject expander initializer here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 ok, I'll try with that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any easiest way to expand expander name than add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or you can add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we use constants for expander name , there is no need to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is because constant can't be a part of interface. Please add both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case, I propose to I add abstract |
||
continue; | ||
} | ||
|
||
if ($this->expanderInitializer->getExpanderDefinition($expanderName) === get_class($expander)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander; | ||
|
||
use Coduo\PHPMatcher\Matcher\Pattern\Expander\Optional; | ||
|
||
class OptionalTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @dataProvider examplesProvider | ||
*/ | ||
public function test_optional_expander_match($value, $expectedResult) | ||
{ | ||
$expander = new Optional(); | ||
$this->assertEquals($expectedResult, $expander->match($value)); | ||
} | ||
|
||
public static function examplesProvider() | ||
{ | ||
return array( | ||
array(array(), true), | ||
array(array('data'), true), | ||
array('', true), | ||
array(0, true), | ||
array(10.1, true), | ||
array(null, true), | ||
array('Lorem ipsum', true), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern; | ||
|
||
use Coduo\PHPMatcher\Matcher\Pattern\Expander\IsEmail; | ||
use Coduo\PHPMatcher\Matcher\Pattern\Expander\IsEmpty; | ||
use Coduo\PHPMatcher\Matcher\Pattern\Expander\Optional; | ||
use Coduo\PHPMatcher\Matcher\Pattern\Pattern; | ||
use Coduo\PHPMatcher\Matcher\Pattern\TypePattern; | ||
use Coduo\PHPMatcher\Parser\ExpanderInitializer; | ||
|
||
class PatternTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var Pattern | ||
*/ | ||
private $pattern; | ||
|
||
public function setUp() | ||
{ | ||
$this->pattern = new TypePattern('dummy', new ExpanderInitializer()); | ||
$this->pattern->addExpander(new isEmail()); | ||
$this->pattern->addExpander(new isEmpty()); | ||
$this->pattern->addExpander(new Optional()); | ||
} | ||
|
||
/** | ||
* @dataProvider examplesProvider | ||
*/ | ||
public function test_has_expander($expander, $expectedResult) | ||
{ | ||
$this->assertEquals($expectedResult, $this->pattern->hasExpander($expander)); | ||
} | ||
|
||
public static function examplesProvider() | ||
{ | ||
return array( | ||
array("isEmail", true), | ||
array("isEmpty", true), | ||
array("optional", true), | ||
array("isUrl", false), | ||
array("non existing expander", false), | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
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