Skip to content

Replaced Type matcher with String, Integer, Number, Double, Boolean and ... #26

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 2 commits into from
Sep 9, 2014
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
6 changes: 5 additions & 1 deletion src/Coduo/PHPMatcher/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ protected function buildScalarMatchers()
return new Matcher\ChainMatcher(array(
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\TypeMatcher(),
new Matcher\NullMatcher(),
new Matcher\StringMatcher(),
new Matcher\IntegerMatcher(),
new Matcher\BooleanMatcher(),
new Matcher\DoubleMatcher(),
new Matcher\NumberMatcher(),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher()
));
Expand Down
19 changes: 18 additions & 1 deletion src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Coduo\PHPMatcher\Matcher;

use Coduo\ToString\String;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class ArrayMatcher extends Matcher
{
const ARRAY_PATTERN = "/^@array@$/";

const UNBOUNDED_PATTERN = '@...@';

/**
Expand All @@ -33,9 +36,14 @@ public function __construct(ValueMatcher $propertyMatcher)
public function match($value, $pattern)
{
if (!is_array($value)) {
$this->error = sprintf("%s \"%s\" is not a valid array.", gettype($value), new String($value));
return false;
}

if ($this->isArrayPattern($pattern)) {
return true;
}

if (false === $this->iterateMatch($value, $pattern)) {
return false;
}
Expand All @@ -48,7 +56,12 @@ public function match($value, $pattern)
*/
public function canMatch($pattern)
{
return is_array($pattern);
return is_array($pattern) || $this->isArrayPattern($pattern);
}

private function isArrayPattern($pattern)
{
return is_string($pattern) && 0 !== preg_match(self::ARRAY_PATTERN, $pattern);
}

/**
Expand Down Expand Up @@ -86,6 +99,10 @@ private function iterateMatch(array $values, array $patterns, $parentPath = "")
return false;
}

if ($this->isArrayPattern($pattern)) {
continue;
}

if (false === $this->iterateMatch($value, $pattern, $this->formatFullPath($parentPath, $path))) {
return false;
}
Expand Down
31 changes: 31 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/BooleanMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Coduo\PHPMatcher\Matcher;

use Coduo\ToString\String;

class BooleanMatcher extends Matcher
{
const BOOLEAN_PATTERN = '/^@boolean@$/';

/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
if (!is_bool($value)) {
$this->error = sprintf("%s \"%s\" is not a valid boolean.", gettype($value), new String($value));
return false;
}

return true;
}

/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match(self::BOOLEAN_PATTERN, $pattern);
}
}
31 changes: 31 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/DoubleMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Coduo\PHPMatcher\Matcher;

use Coduo\ToString\String;

class DoubleMatcher extends Matcher
{
const DOUBLE_PATTERN = '/^@double@$/';

/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
if (!is_double($value)) {
$this->error = sprintf("%s \"%s\" is not a valid double.", gettype($value), new String($value));
return false;
}

return true;
}

/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match(self::DOUBLE_PATTERN, $pattern);
}
}
31 changes: 31 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/IntegerMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Coduo\PHPMatcher\Matcher;

use Coduo\ToString\String;

class IntegerMatcher extends Matcher
{
const INTEGER_PATTERN = '/^@integer@$/';

/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
if (!is_integer($value)) {
$this->error = sprintf("%s \"%s\" is not a valid integer.", gettype($value), new String($value));
return false;
}

return true;
}

/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match(self::INTEGER_PATTERN, $pattern);
}
}
31 changes: 31 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/NumberMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Coduo\PHPMatcher\Matcher;

use Coduo\ToString\String;

class NumberMatcher extends Matcher
{
const NUMBER_PATTERN = '/^@number@$/';

/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
if (!is_numeric($value)) {
$this->error = sprintf("%s \"%s\" is not a valid number.", gettype($value), new String($value));
return false;
}

return true;
}

/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match(self::NUMBER_PATTERN, $pattern);
}
}
31 changes: 31 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/StringMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Coduo\PHPMatcher\Matcher;

use Coduo\ToString\String;

class StringMatcher extends Matcher
{
const STRING_PATTERN = '/^@string@$/';

/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
if (!is_string($value)) {
$this->error = sprintf("%s \"%s\" is not a valid string.", gettype($value), new String($value));
return false;
}

return true;
}

/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match(self::STRING_PATTERN, $pattern);
}
}
4 changes: 4 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/TypeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Coduo\ToString\String;

/**
* @deprecated since 1.1, to be removed in 2.0. Use StringMatcher, NumberMatcher, IntegerMatcher,
* BooleanMatcher, ArrayMatcher instead.
*/
class TypeMatcher extends Matcher
{
const MATCH_PATTERN = "/^@(string|integer|boolean|double|array)@$/";
Expand Down
42 changes: 28 additions & 14 deletions tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<?php
namespace Coduo\PHPMatcher\Tests\Matcher;

use Coduo\PHPMatcher\Matcher\ArrayMatcher;
use Coduo\PHPMatcher\Matcher\ChainMatcher;
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
use Coduo\PHPMatcher\Matcher\TypeMatcher;
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
use Coduo\PHPMatcher\Matcher;

class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ArrayMatcher
* @var Matcher\ArrayMatcher
*/
private $matcher;

public function setUp()
{
$this->matcher = new ArrayMatcher(
new ChainMatcher(array(
new ScalarMatcher(),
new TypeMatcher(),
new WildcardMatcher()
$this->matcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher(array(
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\NullMatcher(),
new Matcher\StringMatcher(),
new Matcher\IntegerMatcher(),
new Matcher\BooleanMatcher(),
new Matcher\DoubleMatcher(),
new Matcher\NumberMatcher(),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
))
);
}
Expand All @@ -43,9 +46,9 @@ public function test_negative_match_arrays($value, $pattern)

public function test_negative_match_when_cant_find_matcher_that_can_match_array_element()
{
$matcher = new ArrayMatcher(
new ChainMatcher(array(
new WildcardMatcher()
$matcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher(array(
new Matcher\WildcardMatcher()
))
);

Expand Down Expand Up @@ -94,6 +97,17 @@ public function test_error_when_matching_fail()
$this->assertEquals($this->matcher->getError(), '"foo value" does not match "bar value".');
}

public function test_error_message_when_matching_non_array_value()
{
$this->assertFalse($this->matcher->match(new \DateTime(), "@array@"));
$this->assertEquals($this->matcher->getError(), "object \"\\DateTime\" is not a valid array.");
}

public function test_matching_array_to_array_pattern()
{
$this->assertTrue($this->matcher->match(array("foo", "bar"), "@array@"));
}

public static function positiveMatchData()
{
$simpleArr = array(
Expand Down
Loading