Skip to content

Commit abd4d5b

Browse files
committed
Refactoring
1 parent e093f96 commit abd4d5b

File tree

14 files changed

+289
-126
lines changed

14 files changed

+289
-126
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Exception;
4+
5+
class InvalidExpanderTypeException extends Exception
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Exception;
4+
5+
class UnknownExpanderException extends Exception
6+
{
7+
}

src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private function setMissingElementInError($place, $path)
206206
*/
207207
private function formatAccessPath($key)
208208
{
209-
return sprintf("[%s]", $key);;
209+
return sprintf("[%s]", $key);
210210
}
211211

212212
/**

src/Coduo/PHPMatcher/Matcher/Pattern.php

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
4+
5+
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
6+
use Coduo\ToString\String;
7+
8+
class StartsWith implements PatternExpander
9+
{
10+
/**
11+
* @var
12+
*/
13+
private $stringBeginning;
14+
15+
/**
16+
* @var null|string
17+
*/
18+
private $error;
19+
20+
/**
21+
* @param string $stringBeginning
22+
*/
23+
public function __construct($stringBeginning)
24+
{
25+
$this->stringBeginning = $stringBeginning;
26+
}
27+
28+
/**
29+
* @param $value
30+
* @return boolean
31+
*/
32+
public function match($value)
33+
{
34+
if (!is_string($value)) {
35+
$this->error = sprintf("StartsWith expander require \"string\", got \"%s\".", new String($value));
36+
return false;
37+
}
38+
39+
if (!empty($this->stringBeginning) && strpos($value, $this->stringBeginning) !== 0) {
40+
$this->error = sprintf("string \"%s\" doesn't starts with string \"%s\".", $value, $this->stringBeginning);
41+
return false;
42+
}
43+
44+
return true;
45+
}
46+
47+
/**
48+
* @return string|null
49+
*/
50+
public function getError()
51+
{
52+
return $this->error;
53+
}
54+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher\Pattern;
4+
5+
interface Pattern
6+
{
7+
/**
8+
* @param PatternExpander $expander
9+
*/
10+
public function addExpander(PatternExpander $expander);
11+
12+
/**
13+
* @param $value
14+
* @return boolean
15+
*/
16+
public function matchExpanders($value);
17+
18+
/**
19+
* Return error message from first expander that doesn't match.
20+
*
21+
* @return null|string
22+
*/
23+
public function getError();
24+
}

src/Coduo/PHPMatcher/Matcher/Pattern/Expander.php renamed to src/Coduo/PHPMatcher/Matcher/Pattern/PatternExpander.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
namespace Coduo\PHPMatcher\Matcher\Pattern;
44

5-
interface Expander
5+
interface PatternExpander
66
{
77
/**
88
* @param $value
99
* @return boolean
1010
*/
1111
public function match($value);
12+
13+
/**
14+
* @return string|null
15+
*/
16+
public function getError();
1217
}

src/Coduo/PHPMatcher/Matcher/Pattern/StartsWith.php

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher\Pattern;
4+
5+
class TypePattern implements Pattern
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $type;
11+
12+
/**
13+
* @var PatternExpander[]|array
14+
*/
15+
private $expanders;
16+
17+
/**
18+
* @var null|string
19+
*/
20+
private $error;
21+
22+
/**
23+
* @param string $type
24+
*/
25+
public function __construct($type)
26+
{
27+
$this->type = $type;
28+
$this->expanders = array();
29+
}
30+
31+
/**
32+
* @param $type
33+
* @return boolean
34+
*/
35+
public function is($type)
36+
{
37+
return strtolower($this->type) === strtolower($type);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function addExpander(PatternExpander $expander)
44+
{
45+
$this->expanders[] = $expander;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function matchExpanders($value)
52+
{
53+
foreach ($this->expanders as $expander) {
54+
if (!$expander->match($value)) {
55+
$this->error = $expander->getError();
56+
return false;
57+
}
58+
}
59+
60+
return true;
61+
}
62+
63+
/**
64+
* @return null|string
65+
*/
66+
public function getError()
67+
{
68+
return $this->error;
69+
}
70+
}

src/Coduo/PHPMatcher/Matcher/StringMatcher.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ public function match($value, $pattern)
1717
return false;
1818
}
1919

20-
$parser = new Parser($pattern);
21-
return $parser->parse()->match($value);
20+
$parser = new Parser($pattern);
21+
$typePattern = $parser->parse();
22+
if (!$typePattern->matchExpanders($value)) {
23+
$this->error = $typePattern->getError();
24+
return false;
25+
}
26+
27+
return true;
2228
}
2329

2430
/**
@@ -29,8 +35,9 @@ public function canMatch($pattern)
2935
if (!is_string($pattern)) {
3036
return false;
3137
}
38+
3239
$parser = new Parser($pattern);
3340

34-
return $parser->patternSyntaxIsValid() && $parser->parse()->getType() === 'string';
41+
return $parser->patternSyntaxIsValid() && $parser->parse()->is('string');
3542
}
3643
}

0 commit comments

Comments
 (0)