Skip to content

Commit

Permalink
Implemented fixed length prefix matching functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sebwalk committed Apr 8, 2018
1 parent a7f5f1f commit 4dcc2c8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Matchers/PrefixMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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]+)/';
}
}
34 changes: 34 additions & 0 deletions tests/Matching/PrefixMatcherWithFixedLengthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class PrefixMatcherWithFixedLengthTest extends \PHPUnit\Framework\TestCase
{
public function testMatchesStrings()
{
$matcher = new \SebastianWalker\Statement\Matchers\PrefixMatcher("PFIX-", 4);
$transaction = new \SebastianWalker\Statement\Transaction(10, "PAYMENT FOR PFIX-1234SOME OTHER STUFF");

$matches = $matcher->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);
}
}

0 comments on commit 4dcc2c8

Please sign in to comment.