Skip to content

Commit

Permalink
Adding matcher class, interface and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzabar committed Jan 19, 2015
1 parent 10431b8 commit 627567d
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 2 deletions.
148 changes: 148 additions & 0 deletions src/Data/Matcher.php
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
31 changes: 31 additions & 0 deletions src/Interfaces/MatchInterface.php
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();

}
13 changes: 11 additions & 2 deletions src/Masquerade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Masquerade;

use Masquerade\Collection\Collection;
use Masquerade\Collection\Collection,
Masquerade\Data\Matcher;

/**
* The control class, can be used to setup configuration.
Expand All @@ -18,15 +19,23 @@ class Masquerade
*/
protected $collection;

/**
* An instance of a MatchInterface enabled class
*
* @var Object
*/
protected $matcher;

/**
* Set up the class vars
*
* @return void
* @author Dan Cox
*/
public function __construct($colMethod = NULL)
public function __construct($colMethod = NULL, $matcher = NULL)
{
$this->collection = new Collection($colMethod);
$this->matcher = (!is_null($matcher) ? $matcher : new Matcher);
}

/**
Expand Down
85 changes: 85 additions & 0 deletions tests/Data/MatcherTest.php
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

0 comments on commit 627567d

Please sign in to comment.