Skip to content

Add Match expander #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ $match = $matcher->match("lorem ipsum dolor", "@string@")
* ``greaterThan($boundry)``
* ``inArray($value)``
* ``oneOf(...$expanders)`` - example usage ``"@[email protected](contains('foo'), contains('bar'), contains('baz'))"``
* ``matchRegex($regex)`` - example usage ``"@[email protected]('/^lorem.+/')"``

##Example usage

Expand Down
68 changes: 68 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/Pattern/Expander/MatchRegex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
use Coduo\ToString\StringConverter;

/**
* @author Benjamin Lazarecki <[email protected]>
*/
class MatchRegex implements PatternExpander
{
/**
* @var null|string
*/
private $error;

/**
* @var string
*/
private $pattern;

/**
* @param string $pattern
*/
public function __construct($pattern)
{
if (!is_string($pattern)) {
throw new \InvalidArgumentException("Regex pattern must be a string.");
}

if (!is_string($pattern) || @preg_match($pattern, '') === false) {
throw new \InvalidArgumentException("Regex pattern must be a valid one.");
}

$this->pattern = $pattern;
}

/**
* @param string $value
*
* @return boolean
*/
public function match($value)
{
if (false === is_string($value)) {
$this->error = sprintf("Match expander require \"string\", got \"%s\".", new StringConverter($value));

return false;
}

if (1 !== preg_match($this->pattern, $value)) {
$this->error = sprintf("string \"%s\" don't match pattern %s.", $value, $this->pattern);

return false;
}

return true;
}

/**
* @return string|null
*/
public function getError()
{
return $this->error;
}
}
1 change: 1 addition & 0 deletions src/Coduo/PHPMatcher/Parser/ExpanderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ExpanderInitializer
"greaterThan" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\GreaterThan",
"inArray" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\InArray",
"contains" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\Contains",
"match" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\MatchRegex",

"oneOf" => "Coduo\\PHPMatcher\\Matcher\\Pattern\\Expander\\OneOf"
);
Expand Down
48 changes: 48 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/Pattern/Expander/MatchRegexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\Expander\MatchRegex;

/**
* @author Benjamin Lazarecki <[email protected]>
*/
class MatchRegexTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider examplesProvider
*/
public function test_match_expander($expectedResult, $expectedError, $pattern, $value)
{
$expander = new MatchRegex($pattern);
$this->assertEquals($expectedResult, $expander->match($value));
$this->assertSame($expectedError, $expander->getError());
}

public static function examplesProvider()
{
return array(
array(true, null, '/^\w$/', 'a'),
array(false, 'string "aa" don\'t match pattern /^\w$/.', '/^\w$/', 'aa'),
array(false, 'Match expander require "string", got "Array(0)".', '/^\w$/', array()),
);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Regex pattern must be a string.
*/
public function test_that_it_only_work_with_string()
{
new MatchRegex(null);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Regex pattern must be a valid one.
*/
public function test_that_it_only_work_with_valid_pattern()
{
new MatchRegex('///');
}
}
2 changes: 2 additions & 0 deletions tests/Coduo/PHPMatcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ public static function expanderExamples()
array(array("foo", "bar"), "@[email protected](\"bar\")", true),
array("lorem ipsum", "@[email protected](contains(\"lorem\"), contains(\"test\"))", true),
array("lorem ipsum", "@[email protected](contains(\"lorem\"), contains(\"test\")).endsWith(\"ipsum\")", true),
array("lorem ipsum", "@[email protected](\"/^lorem \\w+$/\")", true),
array("lorem ipsum", "@[email protected](\"/^foo/\")", false),
);
}
}