Skip to content

Commit bfb6e0d

Browse files
author
Norbert Orzechowicz
committed
Merge pull request #64 from norzechowicz/php-matcher-facade
Added PHPMatcher facade in order to simplify developers experience
2 parents a11daa5 + 85461e6 commit bfb6e0d

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ 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@", $error)) {
30+
echo $error; // in case of error message is set on $error variable via reference
31+
}
32+
33+
```
34+
35+
36+
### Using Factory
37+
2238
```php
2339
<?php
2440

src/Coduo/PHPMatcher/PHPMatcher.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher;
4+
5+
use Coduo\PHPMatcher\Factory\SimpleFactory;
6+
7+
final class PHPMatcher
8+
{
9+
/**
10+
* @param $value
11+
* @param $pattern
12+
* @param null $error
13+
* @return bool
14+
*/
15+
public static function match($value, $pattern, &$error = null)
16+
{
17+
$factory = new SimpleFactory();
18+
$matcher = $factory->createMatcher();
19+
20+
if (!$matcher->match($value, $pattern)) {
21+
$error = $matcher->getError();
22+
return false;
23+
}
24+
25+
return true;
26+
}
27+
}

tests/Coduo/PHPMatcher/MatcherTest.php

Lines changed: 27 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,6 @@ class MatcherTest extends \PHPUnit_Framework_TestCase
1314
*/
1415
protected $matcher;
1516

16-
protected $arrayValue;
17-
1817
public function setUp()
1918
{
2019
$parser = new Parser(new Lexer(), new Parser\ExpanderInitializer());
@@ -63,7 +62,7 @@ public function test_matcher_with_array_value()
6362
'data' => new \stdClass(),
6463
);
6564

66-
$expecation = array(
65+
$expectation = array(
6766
'users' => array(
6867
array(
6968
'id' => '@integer@',
@@ -82,25 +81,17 @@ public function test_matcher_with_array_value()
8281
'data' => '@wildcard@',
8382
);
8483

85-
$this->assertTrue($this->matcher->match($value, $expecation), $this->matcher->getError());
84+
$this->assertTrue($this->matcher->match($value, $expectation), $this->matcher->getError());
85+
$this->assertTrue(PHPMatcher::match($value, $expectation, $error), $error);
8686
}
8787

8888
/**
89-
* @dataProvider scalarValues
89+
* @dataProvider scalarValueExamples
9090
*/
9191
public function test_matcher_with_scalar_values($value, $pattern)
9292
{
9393
$this->assertTrue($this->matcher->match($value, $pattern));
94-
}
95-
96-
public function scalarValues()
97-
{
98-
return array(
99-
array('Norbert Orzechowicz', '@string@'),
100-
array(6.66, '@double@'),
101-
array(1, '@integer@'),
102-
array(array('foo'), '@array@')
103-
);
94+
$this->assertTrue(PHPMatcher::match($value, $pattern));
10495
}
10596

10697
public function test_matcher_with_json()
@@ -149,6 +140,7 @@ public function test_matcher_with_json()
149140
}';
150141

151142
$this->assertTrue($this->matcher->match($json, $jsonPattern));
143+
$this->assertTrue(PHPMatcher::match($json, $jsonPattern));
152144
}
153145

154146
public function test_matcher_with_xml()
@@ -185,13 +177,15 @@ public function test_matcher_with_xml()
185177
XML;
186178

187179
$this->assertTrue($this->matcher->match($xml, $xmlPattern));
180+
$this->assertTrue(PHPMatcher::match($xml, $xmlPattern));
188181
}
189182

190183
public function test_text_matcher()
191184
{
192185
$value = "lorem ipsum 1234 random text";
193186
$pattern = "@[email protected]('lo') ipsum @[email protected](10) random text";
194187
$this->assertTrue($this->matcher->match($value, $pattern));
188+
$this->assertTrue(PHPMatcher::match($value, $pattern));
195189
}
196190

197191

@@ -202,18 +196,25 @@ public function test_error_when_json_value_does_not_match_json_pattern()
202196

203197
$this->assertFalse($this->matcher->match($value, $pattern));
204198
$this->assertSame('"5" does not match "4".', $this->matcher->getError());
199+
200+
$this->assertFalse(PHPMatcher::match($value, $pattern, $error));
201+
$this->assertSame('"5" does not match "4".', $error);
205202
}
206203

207204
public function test_matcher_with_callback()
208205
{
209206
$this->assertTrue($this->matcher->match('test', function($value) { return $value === 'test';}));
207+
$this->assertTrue(PHPMatcher::match('test', function($value) { return $value === 'test';}));
210208
$this->assertFalse($this->matcher->match('test', function($value) { return $value !== 'test';}));
209+
$this->assertFalse(PHPMatcher::match('test', function($value) { return $value !== 'test';}));
211210
}
212211

213212
public function test_matcher_with_wildcard()
214213
{
215214
$this->assertTrue($this->matcher->match('test', '@*@'));
215+
$this->assertTrue(PHPMatcher::match('test', '@*@'));
216216
$this->assertTrue($this->matcher->match('test', '@wildcard@'));
217+
$this->assertTrue(PHPMatcher::match('test', '@wildcard@'));
217218
}
218219

219220
/**
@@ -222,8 +223,19 @@ public function test_matcher_with_wildcard()
222223
public function test_expanders($value, $pattern, $expectedResult)
223224
{
224225
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern));
226+
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern));
225227
}
226228

229+
public function scalarValueExamples()
230+
{
231+
return array(
232+
array('Norbert Orzechowicz', '@string@'),
233+
array(6.66, '@double@'),
234+
array(1, '@integer@'),
235+
array(array('foo'), '@array@')
236+
);
237+
}
238+
227239
public static function expanderExamples()
228240
{
229241
return array(

0 commit comments

Comments
 (0)