-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding matcher class, interface and tests
- Loading branch information
Showing
4 changed files
with
275 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php namespace Masquerade\Data; | ||
|
||
use Masquerade\Interfaces\MatchInterface, | ||
Masquerade\Collection\Collection; | ||
|
||
/** | ||
* The matcher class matches and formats mask names in strings | ||
* | ||
* @package Masquerade | ||
* @subpackage Data | ||
* @author Dan Cox | ||
*/ | ||
class Matcher implements MatchInterface | ||
{ | ||
/** | ||
* The initial string | ||
* | ||
* @var string | ||
*/ | ||
protected $str; | ||
|
||
/** | ||
* An array of matches | ||
* | ||
* @var Array | ||
*/ | ||
protected $matches; | ||
|
||
/** | ||
* An Array of matches that have been formatted | ||
* | ||
* @var Array | ||
*/ | ||
protected $formatted; | ||
|
||
/** | ||
* The collection object | ||
* | ||
* @var Collection | ||
*/ | ||
protected $collection; | ||
|
||
/** | ||
* Loads string and collection object | ||
* | ||
* @return Matcher | ||
* @author Dan Cox | ||
*/ | ||
public function load($str, Collection $collection) | ||
{ | ||
$this->str = $str; | ||
$this->collection = $collection; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Searchs the string for possible matches | ||
* | ||
* @return Matcher | ||
* @author Dan Cox | ||
*/ | ||
public function search() | ||
{ | ||
preg_match('/[[A-Za-z0-9 ,"=]+?]/', $this->str, $this->matches); | ||
|
||
// Format matches | ||
if(!empty($this->matches)) | ||
{ | ||
$this->format(); | ||
} | ||
} | ||
|
||
/** | ||
* Formats the matches into a an array with mapped params and * | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function format() | ||
{ | ||
foreach($this->matches as $key => $match) | ||
{ | ||
$potential = str_replace(['[', ']'], '', $match); | ||
|
||
// Seperate by comma | ||
$potential = explode(',', $potential); | ||
|
||
// The first value will be the mask name | ||
if($this->collection->getMethod()->has($potential[0])) | ||
{ | ||
$name = $potential[0]; | ||
|
||
$this->formatted[$name] = Array(); | ||
|
||
// Remove the name from the array | ||
unset($potential[0]); | ||
|
||
$this->processValues($name, $potential); | ||
|
||
} else { | ||
// This isnt a mask | ||
unset($this->matches[$key]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Processes a mask values | ||
* | ||
* @return Void | ||
* @author Dan Cox | ||
*/ | ||
public function processValues($name, $arr) | ||
{ | ||
$this->formatted[$name] = Array(); | ||
|
||
foreach($arr as $key => $value) | ||
{ | ||
if(strstr($value, '=') !== false) | ||
{ | ||
// Extract it's value | ||
$values = explode('=', $value); | ||
$val = str_replace(['\'', '"'], '', $values[1]); | ||
|
||
$this->formatted[$name]['params'][trim($values[0])] = $val; | ||
|
||
} else { | ||
// This gets added as a flag | ||
$this->formatted[$name]['params'][trim($value)] = TRUE; | ||
} | ||
} | ||
|
||
$this->formatted[$name]['value'] = $this->collection->getMethod()->get($name); | ||
} | ||
|
||
/** | ||
* Returns the set of matches | ||
* | ||
* @return Array | ||
* @author Dan Cox | ||
*/ | ||
public function getMatches() | ||
{ | ||
return $this->formatted; | ||
} | ||
|
||
} // END class Matcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php namespace Masquerade\Interfaces; | ||
|
||
use Masquerade\Collection\Collection; | ||
|
||
/** | ||
* This interface allows people to rip out the match class | ||
* and implement their own without losing any functionality | ||
* | ||
*/ | ||
Interface MatchInterface | ||
{ | ||
/** | ||
* Loads a string and the collection class into the | ||
* matcher | ||
* | ||
*/ | ||
public function load($str, Collection $collection); | ||
|
||
/** | ||
* Searches through the string and picks out any matches | ||
* | ||
*/ | ||
public function search(); | ||
|
||
/** | ||
* Returns the matches gathered in the search function | ||
* | ||
*/ | ||
public function getMatches(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
use Masquerade\Data\Matcher, | ||
Masquerade\Collection\Collection; | ||
|
||
/** | ||
* Test case for the Matcher class | ||
* | ||
* @package Masquerade | ||
* @subpackage Tests\Data | ||
* @author Dan Cox | ||
*/ | ||
class MatcherTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* A collection instance | ||
* | ||
* @var Object | ||
*/ | ||
protected $collection; | ||
|
||
/** | ||
* Instance of matcher | ||
* | ||
* @var Object | ||
*/ | ||
protected $matcher; | ||
|
||
/** | ||
* Set up test env | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->collection = new Collection; | ||
$this->matcher = new Matcher; | ||
|
||
// Add a basic mask | ||
$this->collection->add('foo', 'bar'); | ||
} | ||
|
||
/** | ||
* Test loading a basic mask into the collection and rreturning value mapping | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function test_loadBasicMask() | ||
{ | ||
$str = '[foo] will be bar'; | ||
|
||
$this->matcher | ||
->load($str, $this->collection) | ||
->search(); | ||
|
||
$matches = $this->matcher->getMatches(); | ||
|
||
$this->assertEquals(Array('foo' => Array('value' => 'bar')), $matches); | ||
} | ||
|
||
/** | ||
* This test will look at return params from within the mask | ||
* | ||
* @return void | ||
* @author Dan Cox | ||
*/ | ||
public function test_advancedMask() | ||
{ | ||
$str = '[foo, test="value", verbose] will be bar'; | ||
|
||
$this->matcher | ||
->load($str, $this->collection) | ||
->search(); | ||
|
||
$matches = $this->matcher->getMatches(); | ||
|
||
$this->assertEquals( | ||
Array('foo' => Array('value' => 'bar', 'params' => Array('test' => 'value', 'verbose' => TRUE))), | ||
$matches | ||
); | ||
} | ||
|
||
} // END class MatcherTest extends \PHPUnit_Framework_TestCase |