From 79681d6185b53dcf2efbfac30de537347df26afd Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Sun, 24 Jan 2016 11:25:00 +0100 Subject: [PATCH 1/5] Added PHPMatcher facade in order to simplify developers experience --- README.md | 15 ++++++++ src/Coduo/PHPMatcher/PHPMatcher.php | 47 ++++++++++++++++++++++++++ tests/Coduo/PHPMatcher/MatcherTest.php | 42 +++++++++++++++-------- 3 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 src/Coduo/PHPMatcher/PHPMatcher.php diff --git a/README.md b/README.md index 314b0e01..83622ceb 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,21 @@ composer require --dev "coduo/php-matcher" ## Basic usage +### Using facade + +```php +match($value, $pattern); + } + + /** + * @return null|string + */ + public static function getError() + { + $matcher = self::createMatcher(); + + return $matcher->getError(); + } + + private static function createMatcher() + { + if (self::$matcher instanceof Matcher) { + return self::$matcher; + } + + $factory = new SimpleFactory(); + self::$matcher = $factory->createMatcher(); + + return self::$matcher; + } +} \ No newline at end of file diff --git a/tests/Coduo/PHPMatcher/MatcherTest.php b/tests/Coduo/PHPMatcher/MatcherTest.php index 7e70ff46..a517610d 100644 --- a/tests/Coduo/PHPMatcher/MatcherTest.php +++ b/tests/Coduo/PHPMatcher/MatcherTest.php @@ -5,6 +5,7 @@ use Coduo\PHPMatcher\Lexer; use Coduo\PHPMatcher\Matcher; use Coduo\PHPMatcher\Parser; +use Coduo\PHPMatcher\PHPMatcher; class MatcherTest extends \PHPUnit_Framework_TestCase { @@ -13,8 +14,6 @@ class MatcherTest extends \PHPUnit_Framework_TestCase */ protected $matcher; - protected $arrayValue; - public function setUp() { $parser = new Parser(new Lexer(), new Parser\ExpanderInitializer()); @@ -63,7 +62,7 @@ public function test_matcher_with_array_value() 'data' => new \stdClass(), ); - $expecation = array( + $expectation = array( 'users' => array( array( 'id' => '@integer@', @@ -82,25 +81,17 @@ public function test_matcher_with_array_value() 'data' => '@wildcard@', ); - $this->assertTrue($this->matcher->match($value, $expecation), $this->matcher->getError()); + $this->assertTrue($this->matcher->match($value, $expectation), $this->matcher->getError()); + $this->assertTrue(PHPMatcher::match($value, $expectation), PHPMatcher::getError()); } /** - * @dataProvider scalarValues + * @dataProvider scalarValueExamples */ public function test_matcher_with_scalar_values($value, $pattern) { $this->assertTrue($this->matcher->match($value, $pattern)); - } - - public function scalarValues() - { - return array( - array('Norbert Orzechowicz', '@string@'), - array(6.66, '@double@'), - array(1, '@integer@'), - array(array('foo'), '@array@') - ); + $this->assertTrue(PHPMatcher::match($value, $pattern)); } public function test_matcher_with_json() @@ -149,6 +140,7 @@ public function test_matcher_with_json() }'; $this->assertTrue($this->matcher->match($json, $jsonPattern)); + $this->assertTrue(PHPMatcher::match($json, $jsonPattern)); } public function test_matcher_with_xml() @@ -185,6 +177,7 @@ public function test_matcher_with_xml() XML; $this->assertTrue($this->matcher->match($xml, $xmlPattern)); + $this->assertTrue(PHPMatcher::match($xml, $xmlPattern)); } public function test_text_matcher() @@ -192,6 +185,7 @@ public function test_text_matcher() $value = "lorem ipsum 1234 random text"; $pattern = "@string@.startsWith('lo') ipsum @number@.greaterThan(10) random text"; $this->assertTrue($this->matcher->match($value, $pattern)); + $this->assertTrue(PHPMatcher::match($value, $pattern)); } @@ -202,18 +196,25 @@ public function test_error_when_json_value_does_not_match_json_pattern() $this->assertFalse($this->matcher->match($value, $pattern)); $this->assertSame('"5" does not match "4".', $this->matcher->getError()); + + $this->assertFalse(PHPMatcher::match($value, $pattern)); + $this->assertSame('"5" does not match "4".', PHPMatcher::getError()); } public function test_matcher_with_callback() { $this->assertTrue($this->matcher->match('test', function($value) { return $value === 'test';})); + $this->assertTrue(PHPMatcher::match('test', function($value) { return $value === 'test';})); $this->assertFalse($this->matcher->match('test', function($value) { return $value !== 'test';})); + $this->assertFalse(PHPMatcher::match('test', function($value) { return $value !== 'test';})); } public function test_matcher_with_wildcard() { $this->assertTrue($this->matcher->match('test', '@*@')); + $this->assertTrue(PHPMatcher::match('test', '@*@')); $this->assertTrue($this->matcher->match('test', '@wildcard@')); + $this->assertTrue(PHPMatcher::match('test', '@wildcard@')); } /** @@ -222,8 +223,19 @@ public function test_matcher_with_wildcard() public function test_expanders($value, $pattern, $expectedResult) { $this->assertSame($expectedResult, $this->matcher->match($value, $pattern)); + $this->assertSame($expectedResult, PHPMatcher::match($value, $pattern)); } + public function scalarValueExamples() + { + return array( + array('Norbert Orzechowicz', '@string@'), + array(6.66, '@double@'), + array(1, '@integer@'), + array(array('foo'), '@array@') + ); + } + public static function expanderExamples() { return array( From b8d3b2cd4899644e501089dc24d7da7949b01f1d Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Sun, 24 Jan 2016 20:32:58 +0100 Subject: [PATCH 2/5] Removed facade state --- README.md | 5 +++-- src/Coduo/PHPMatcher/PHPMatcher.php | 35 ++++++----------------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 83622ceb..a019b9e1 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,10 @@ composer require --dev "coduo/php-matcher" use Coduo\PHPMatcher\PHPMatcher; -if (!PHPMatcher::match("lorem ipsum dolor", "@string@")) { - echo PHPMatcher::getError(); // reason why value does not match pattern. +if (!PHPMatcher::match("lorem ipsum dolor", "@string@", $error)) { + echo $error; // in case of error message is set on $error message via reference } + ``` diff --git a/src/Coduo/PHPMatcher/PHPMatcher.php b/src/Coduo/PHPMatcher/PHPMatcher.php index a374dcd2..7f0db295 100644 --- a/src/Coduo/PHPMatcher/PHPMatcher.php +++ b/src/Coduo/PHPMatcher/PHPMatcher.php @@ -6,42 +6,21 @@ final class PHPMatcher { - /** - * @var Matcher|null - */ - private static $matcher; - /** * @param $value * @param $pattern + * @param null $error * @return bool */ - public static function match($value, $pattern) + public static function match($value, $pattern, &$error = null) { - $matcher = self::createMatcher(); + $matcher = (new SimpleFactory())->createMatcher(); - return $matcher->match($value, $pattern); - } - - /** - * @return null|string - */ - public static function getError() - { - $matcher = self::createMatcher(); - - return $matcher->getError(); - } - - private static function createMatcher() - { - if (self::$matcher instanceof Matcher) { - return self::$matcher; + if (!$matcher->match($value, $pattern)) { + $error = $matcher->getError(); + return false; } - $factory = new SimpleFactory(); - self::$matcher = $factory->createMatcher(); - - return self::$matcher; + return true; } } \ No newline at end of file From 33c54a08db339b167bfbc7ec9ba5f0dd53e674e0 Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Sun, 24 Jan 2016 20:34:02 +0100 Subject: [PATCH 3/5] Fixed tests --- tests/Coduo/PHPMatcher/MatcherTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Coduo/PHPMatcher/MatcherTest.php b/tests/Coduo/PHPMatcher/MatcherTest.php index a517610d..fb200e1e 100644 --- a/tests/Coduo/PHPMatcher/MatcherTest.php +++ b/tests/Coduo/PHPMatcher/MatcherTest.php @@ -82,7 +82,7 @@ public function test_matcher_with_array_value() ); $this->assertTrue($this->matcher->match($value, $expectation), $this->matcher->getError()); - $this->assertTrue(PHPMatcher::match($value, $expectation), PHPMatcher::getError()); + $this->assertTrue(PHPMatcher::match($value, $expectation, $error), $error); } /** @@ -197,8 +197,8 @@ public function test_error_when_json_value_does_not_match_json_pattern() $this->assertFalse($this->matcher->match($value, $pattern)); $this->assertSame('"5" does not match "4".', $this->matcher->getError()); - $this->assertFalse(PHPMatcher::match($value, $pattern)); - $this->assertSame('"5" does not match "4".', PHPMatcher::getError()); + $this->assertFalse(PHPMatcher::match($value, $pattern, $error)); + $this->assertSame('"5" does not match "4".', $error); } public function test_matcher_with_callback() From 88cd24d343173ed56ade1dded38619fa2c57dd0d Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Sun, 24 Jan 2016 21:52:04 +0100 Subject: [PATCH 4/5] Fixed PHPMatcher facade on php 5.3 --- src/Coduo/PHPMatcher/PHPMatcher.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Coduo/PHPMatcher/PHPMatcher.php b/src/Coduo/PHPMatcher/PHPMatcher.php index 7f0db295..c33f88c8 100644 --- a/src/Coduo/PHPMatcher/PHPMatcher.php +++ b/src/Coduo/PHPMatcher/PHPMatcher.php @@ -14,7 +14,8 @@ final class PHPMatcher */ public static function match($value, $pattern, &$error = null) { - $matcher = (new SimpleFactory())->createMatcher(); + $factory = new SimpleFactory(); + $matcher = $factory->createMatcher(); if (!$matcher->match($value, $pattern)) { $error = $matcher->getError(); From 85461e668c84b651877438b5a7a8e04e96a99b7d Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Sun, 24 Jan 2016 22:10:06 +0100 Subject: [PATCH 5/5] Fixed typo in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a019b9e1..7f25c97e 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ composer require --dev "coduo/php-matcher" use Coduo\PHPMatcher\PHPMatcher; if (!PHPMatcher::match("lorem ipsum dolor", "@string@", $error)) { - echo $error; // in case of error message is set on $error message via reference + echo $error; // in case of error message is set on $error variable via reference } ```