Skip to content

Commit c8c6d57

Browse files
committed
Added possibility to add new expander definitions
1 parent 7a21f7a commit c8c6d57

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
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 UnknownExpanderClassException extends Exception
6+
{
7+
}

src/Coduo/PHPMatcher/Parser.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Coduo\PHPMatcher\Exception\Exception;
77
use Coduo\PHPMatcher\Exception\InvalidExpanderTypeException;
88
use Coduo\PHPMatcher\Exception\PatternException;
9+
use Coduo\PHPMatcher\Exception\UnknownExpanderClassException;
910
use Coduo\PHPMatcher\Exception\UnknownExpanderException;
1011
use Coduo\PHPMatcher\Matcher\Pattern;
1112

@@ -77,6 +78,20 @@ public function getAST($pattern)
7778
return $this->getPattern();
7879
}
7980

81+
/**
82+
* @param $expanderName
83+
* @param $expanderFQCN Fully-Qualified Class Name that implements PatternExpander interface
84+
* @throws UnknownExpanderClassException
85+
*/
86+
public function addExpanderDefinition($expanderName, $expanderFQCN)
87+
{
88+
if (!class_exists($expanderFQCN)) {
89+
throw new UnknownExpanderClassException(sprintf("Class \"%s\" does not exists.", $expanderFQCN));
90+
}
91+
92+
$this->expanderDefinitions[$expanderName] = $expanderFQCN;
93+
}
94+
8095
/**
8196
* Create AST root
8297
*

tests/Coduo/PHPMatcher/Matcher/StringMatcherTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class StringMatcherTest extends \PHPUnit_Framework_TestCase
1515

1616
public function setUp()
1717
{
18-
$this->matcher = new StringMatcher(new Parser(new Lexer()));
18+
$parser = new Parser(new Lexer());
19+
$this->matcher = new StringMatcher($parser);
1920
}
2021
/**
2122
* @dataProvider positiveCanMatchData

0 commit comments

Comments
 (0)