Skip to content

Commit

Permalink
add expanders 'After' and 'Before' for date comparaison (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
Canadadry authored and norberttech committed Jan 3, 2019
1 parent c15fdbe commit fc2a8be
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ $matcher->match("lorem ipsum dolor", "@[email protected]('lorem').contains('ips

```

### Date matching

```php
<?php

use Coduo\PHPMatcher\Factory\SimpleFactory;

$factory = new SimpleFactory();
$matcher = $factory->createMatcher();

$matcher->match('2014-08-19', '@[email protected]()');
$matcher->match('2014-08-19', '@[email protected]().before("2016-08-19")');
$matcher->match('2014-08-19', '@[email protected]().before("today").after("+ 100year")');

```

### Integer matching

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

declare(strict_types=1);

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

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

final class After implements PatternExpander
{
const NAME = 'after';

private $boundary;

private $error;


public function __construct($boundary)
{
if (false === \is_string($boundary)) {
$this->error = \sprintf('After expander require "string", got "%s".', new StringConverter($boundary));
return false;
}

if (!$this->is_datetime($boundary)) {
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date.', new StringConverter($boundary)));
}

$this->boundary = new \DateTime($boundary);
}

public static function is(string $name) : bool
{
return self::NAME === $name;
}

public function match($value) : bool
{
if (false === \is_string($value)) {
$this->error = \sprintf('After expander require "string", got "%s".', new StringConverter($value));
return false;
}

if (!$this->is_datetime($value)) {
$this->error = \sprintf('Value "%s" is not a valid date.', new StringConverter($value));
return false;
}

$value = new \DateTime($value);

if ($value <= $this->boundary) {
$this->error = \sprintf('Value "%s" is not after "%s".', new StringConverter($value), new StringConverter($this->boundary));
return false;
}

return $value > $this->boundary;
}

private function is_datetime(string $value) : bool
{
try {
new \DateTime($value);
return true;
} catch (\Exception $e) {
return false;
}
}

public function getError()
{
return $this->error;
}
}
73 changes: 73 additions & 0 deletions src/Matcher/Pattern/Expander/Before.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

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

final class Before implements PatternExpander
{
const NAME = 'before';

private $boundary;

private $error;


public function __construct($boundary)
{
if (!\is_string($boundary)) {
throw new \InvalidArgumentException(\sprintf('Before expander require "string", got "%s".', new StringConverter($boundary)));
}

if (!$this->is_datetime($boundary)) {
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date.', new StringConverter($boundary)));
}

$this->boundary = new \DateTime($boundary);
}

public static function is(string $name) : bool
{
return self::NAME === $name;
}

public function match($value) : bool
{
if (!\is_string($value)) {
$this->error = \sprintf('Before expander require "string", got "%s".', new StringConverter($value));
return false;
}

if (!$this->is_datetime($value)) {
$this->error = \sprintf('Value "%s" is not a valid date.', new StringConverter($value));
return false;
}

$value = new \DateTime($value);

if ($value >= $this->boundary) {
$this->error = \sprintf('Value "%s" is before "%s".', new StringConverter($value), new StringConverter($this->boundary));
return false;
}

return $value < $this->boundary;
}

private function is_datetime(string $value) : bool
{
try {
new \DateTime($value);
return true;
} catch (\Exception $e) {
return false;
}
}

public function getError()
{
return $this->error;
}
}
2 changes: 2 additions & 0 deletions src/Parser/ExpanderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
final class ExpanderInitializer
{
private $expanderDefinitions = [
Expander\After::NAME => Expander\After::class,
Expander\Before::NAME => Expander\Before::class,
Expander\Contains::NAME => Expander\Contains::class,
Expander\Count::NAME => Expander\Count::class,
Expander\EndsWith::NAME => Expander\EndsWith::class,
Expand Down
48 changes: 48 additions & 0 deletions tests/Matcher/Pattern/Expander/AfterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

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

use Coduo\PHPMatcher\Matcher\Pattern\Expander\After;
use PHPUnit\Framework\TestCase;

class AfterTest extends TestCase
{
/**
* @dataProvider examplesProvider
*/
public function test_examples($boundary, $value, $expectedResult)
{
$expander = new After($boundary);
$this->assertEquals($expectedResult, $expander->match($value));
}

public static function examplesProvider()
{
return [
['+ 2 day','today',false],
['2018-02-06T04:20:33','2017-02-06T04:20:33',false],
['2017-02-06T04:20:33','2018-02-06T04:20:33',true],
];
}

/**
* @dataProvider invalidCasesProvider
*/
public function test_error_when_matching_fail($boundary, $value, $errorMessage)
{
$expander = new After($boundary);
$this->assertFalse($expander->match($value));
$this->assertEquals($errorMessage, $expander->getError());
}

public static function invalidCasesProvider()
{
return [
['today', 'ipsum lorem', 'Value "ipsum lorem" is not a valid date.'],
['2017-02-06T04:20:33', 'ipsum lorem', 'Value "ipsum lorem" is not a valid date.'],
['today',5, 'After expander require "string", got "5".'],
];
}
}
48 changes: 48 additions & 0 deletions tests/Matcher/Pattern/Expander/BeforeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

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

use Coduo\PHPMatcher\Matcher\Pattern\Expander\Before;
use PHPUnit\Framework\TestCase;

class BeforeTest extends TestCase
{
/**
* @dataProvider examplesProvider
*/
public function test_examples($boundary, $value, $expectedResult)
{
$expander = new Before($boundary);
$this->assertEquals($expectedResult, $expander->match($value));
}

public static function examplesProvider()
{
return [
['+ 2 day','today',true],
['2018-02-06T04:20:33','2017-02-06T04:20:33',true],
['2017-02-06T04:20:33','2018-02-06T04:20:33',false],
];
}

/**
* @dataProvider invalidCasesProvider
*/
public function test_error_when_matching_fail($boundary, $value, $errorMessage)
{
$expander = new Before($boundary);
$this->assertFalse($expander->match($value));
$this->assertEquals($errorMessage, $expander->getError());
}

public static function invalidCasesProvider()
{
return [
['today', 'ipsum lorem', 'Value "ipsum lorem" is not a valid date.'],
['2017-02-06T04:20:33', 'ipsum lorem', 'Value "ipsum lorem" is not a valid date.'],
['today',5, 'Before expander require "string", got "5".'],
];
}
}
4 changes: 4 additions & 0 deletions tests/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ public static function expanderExamples()
['http://coduo.pl/', '@[email protected]()', true],
['lorem ipsum', '@[email protected]()', false],
['2014-08-19', '@[email protected]()', true],
['3014-08-19', '@[email protected]("today")', false],
['1014-08-19', '@[email protected]("+ 1day")', true],
['3014-08-19', '@[email protected]("today")', true],
['1014-08-19', '@[email protected]("+ 1day")', false],
[100, '@[email protected](101).greaterThan(10)', true],
['', '@[email protected]()', false],
['lorem ipsum', '@[email protected]()', true],
Expand Down

0 comments on commit fc2a8be

Please sign in to comment.