Skip to content

Commit 46d66d1

Browse files
committed
Added PHPMatcher facade in order to simplify developers experience
1 parent ae724fd commit 46d66d1

File tree

3 files changed

+92
-15
lines changed

3 files changed

+92
-15
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ composer require --dev "coduo/php-matcher"
1919

2020
## Basic usage
2121

22+
### Using facade
23+
24+
```php
25+
<?php
26+
27+
use Coduo\PHPMatcher\PHPMatcher;
28+
29+
if (!PHPMatcher::match("lorem ipsum dolor", "@string@")) {
30+
echo PHPMatcher::getError(); // reason why value does not match pattern.
31+
}
32+
```
33+
34+
35+
### Using Factory
36+
2237
```php
2338
<?php
2439

src/Coduo/PHPMatcher/PHPMatcher.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher;
4+
5+
use Coduo\PHPMatcher\Factory\SimpleFactory;
6+
7+
final class PHPMatcher
8+
{
9+
/**
10+
* @var Matcher|null
11+
*/
12+
private static $matcher;
13+
14+
/**
15+
* @param $value
16+
* @param $pattern
17+
* @return bool
18+
*/
19+
public static function match($value, $pattern)
20+
{
21+
$matcher = self::createMatcher();
22+
23+
return $matcher->match($value, $pattern);
24+
}
25+
26+
/**
27+
* @return null|string
28+
*/
29+
public static function getError()
30+
{
31+
$matcher = self::createMatcher();
32+
33+
return $matcher->getError();
34+
}
35+
36+
private static function createMatcher()
37+
{
38+
if (self::$matcher instanceof Matcher) {
39+
return self::$matcher;
40+
}
41+
42+
$factory = new SimpleFactory();
43+
self::$matcher = $factory->createMatcher();
44+
45+
return self::$matcher;
46+
}
47+
}

tests/Coduo/PHPMatcher/MatcherTest.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Coduo\PHPMatcher\Lexer;
66
use Coduo\PHPMatcher\Matcher;
77
use Coduo\PHPMatcher\Parser;
8+
use Coduo\PHPMatcher\PHPMatcher;
89

910
class MatcherTest extends \PHPUnit_Framework_TestCase
1011
{
@@ -13,8 +14,9 @@ class MatcherTest extends \PHPUnit_Framework_TestCase
1314
*/
1415
protected $matcher;
1516

16-
protected $arrayValue;
17-
17+
/**
18+
* @var Matcher\CaptureMatcher
19+
*/
1820
protected $captureMatcher;
1921

2022
public function setUp()
@@ -67,7 +69,7 @@ public function test_matcher_with_array_value()
6769
'data' => new \stdClass(),
6870
);
6971

70-
$expecation = array(
72+
$expectation = array(
7173
'users' => array(
7274
array(
7375
'id' => '@integer@',
@@ -86,25 +88,17 @@ public function test_matcher_with_array_value()
8688
'data' => '@wildcard@',
8789
);
8890

89-
$this->assertTrue($this->matcher->match($value, $expecation), $this->matcher->getError());
91+
$this->assertTrue($this->matcher->match($value, $expectation), $this->matcher->getError());
92+
$this->assertTrue(PHPMatcher::match($value, $expectation), PHPMatcher::getError());
9093
}
9194

9295
/**
93-
* @dataProvider scalarValues
96+
* @dataProvider scalarValueExamples
9497
*/
9598
public function test_matcher_with_scalar_values($value, $pattern)
9699
{
97100
$this->assertTrue($this->matcher->match($value, $pattern));
98-
}
99-
100-
public function scalarValues()
101-
{
102-
return array(
103-
array('Norbert Orzechowicz', '@string@'),
104-
array(6.66, '@double@'),
105-
array(1, '@integer@'),
106-
array(array('foo'), '@array@')
107-
);
101+
$this->assertTrue(PHPMatcher::match($value, $pattern));
108102
}
109103

110104
public function test_matcher_with_json()
@@ -153,6 +147,7 @@ public function test_matcher_with_json()
153147
}';
154148

155149
$this->assertTrue($this->matcher->match($json, $jsonPattern));
150+
$this->assertTrue(PHPMatcher::match($json, $jsonPattern));
156151
}
157152

158153
public function test_matcher_with_xml()
@@ -189,13 +184,15 @@ public function test_matcher_with_xml()
189184
XML;
190185

191186
$this->assertTrue($this->matcher->match($xml, $xmlPattern));
187+
$this->assertTrue(PHPMatcher::match($xml, $xmlPattern));
192188
}
193189

194190
public function test_text_matcher()
195191
{
196192
$value = "lorem ipsum 1234 random text";
197193
$pattern = "@[email protected]('lo') ipsum @[email protected](10) random text";
198194
$this->assertTrue($this->matcher->match($value, $pattern));
195+
$this->assertTrue(PHPMatcher::match($value, $pattern));
199196
}
200197

201198

@@ -206,6 +203,9 @@ public function test_error_when_json_value_does_not_match_json_pattern()
206203

207204
$this->assertFalse($this->matcher->match($value, $pattern));
208205
$this->assertSame('"5" does not match "4".', $this->matcher->getError());
206+
207+
$this->assertFalse(PHPMatcher::match($value, $pattern));
208+
$this->assertSame('"5" does not match "4".', PHPMatcher::getError());
209209
}
210210

211211
public function test_matcher_with_captures()
@@ -220,13 +220,17 @@ public function test_matcher_with_captures()
220220
public function test_matcher_with_callback()
221221
{
222222
$this->assertTrue($this->matcher->match('test', function($value) { return $value === 'test';}));
223+
$this->assertTrue(PHPMatcher::match('test', function($value) { return $value === 'test';}));
223224
$this->assertFalse($this->matcher->match('test', function($value) { return $value !== 'test';}));
225+
$this->assertFalse(PHPMatcher::match('test', function($value) { return $value !== 'test';}));
224226
}
225227

226228
public function test_matcher_with_wildcard()
227229
{
228230
$this->assertTrue($this->matcher->match('test', '@*@'));
231+
$this->assertTrue(PHPMatcher::match('test', '@*@'));
229232
$this->assertTrue($this->matcher->match('test', '@wildcard@'));
233+
$this->assertTrue(PHPMatcher::match('test', '@wildcard@'));
230234
}
231235

232236
/**
@@ -235,8 +239,19 @@ public function test_matcher_with_wildcard()
235239
public function test_expanders($value, $pattern, $expectedResult)
236240
{
237241
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern));
242+
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern));
238243
}
239244

245+
public function scalarValueExamples()
246+
{
247+
return array(
248+
array('Norbert Orzechowicz', '@string@'),
249+
array(6.66, '@double@'),
250+
array(1, '@integer@'),
251+
array(array('foo'), '@array@')
252+
);
253+
}
254+
240255
public static function expanderExamples()
241256
{
242257
return array(

0 commit comments

Comments
 (0)