From 4dcc2c8f944e245e8517926922b879c8a0185061 Mon Sep 17 00:00:00 2001 From: sebastianwalker Date: Sun, 8 Apr 2018 16:46:23 +0200 Subject: [PATCH] Implemented fixed length prefix matching functionality --- src/Matchers/PrefixMatcher.php | 9 ++++- .../PrefixMatcherWithFixedLengthTest.php | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/Matching/PrefixMatcherWithFixedLengthTest.php diff --git a/src/Matchers/PrefixMatcher.php b/src/Matchers/PrefixMatcher.php index 14bf914..f356d25 100644 --- a/src/Matchers/PrefixMatcher.php +++ b/src/Matchers/PrefixMatcher.php @@ -12,13 +12,16 @@ class PrefixMatcher implements Matcher */ private $prefix; + private $fixedLength; + /** * PrefixMatcher constructor. * @param string $prefix */ - public function __construct($prefix) + public function __construct($prefix, $fixedLength = null) { $this->prefix = $prefix; + $this->fixedLength = $fixedLength; } /** @@ -71,6 +74,10 @@ public function stringToEntity($match_string) */ private function getRegex() { + if($this->fixedLength){ + return '/'.$this->prefix.'(.{'.intval($this->fixedLength).'})/'; + } + return '/(?:^|\s)'.$this->prefix.'{1}([^\s]+)/'; } } \ No newline at end of file diff --git a/tests/Matching/PrefixMatcherWithFixedLengthTest.php b/tests/Matching/PrefixMatcherWithFixedLengthTest.php new file mode 100644 index 0000000..c8a1a1a --- /dev/null +++ b/tests/Matching/PrefixMatcherWithFixedLengthTest.php @@ -0,0 +1,34 @@ +getEntities($transaction); + + $this->assertSame(["1234"], $matches); + } + + public function testMatchesMultipleStrings() + { + $matcher = new \SebastianWalker\Statement\Matchers\PrefixMatcher("PFIX-", 4); + $transaction = new \SebastianWalker\Statement\Transaction(10, "PAYMENT FOR PFIX-1234SOME OTHER STUFF AND PFIX-5678SOME MORE STUFF"); + + $matches = $matcher->getEntities($transaction); + + $this->assertSame(["1234","5678"], $matches); + } + + public function testMatchesNothing() + { + $matcher = new \SebastianWalker\Statement\Matchers\PrefixMatcher("PFIX-", 4); + $transaction = new \SebastianWalker\Statement\Transaction(10, "PAYMENT FOR SOMETHING ELSE"); + + $matches = $matcher->getEntities($transaction); + + $this->assertSame([], $matches); + } +} \ No newline at end of file