Skip to content

Commit fc2a8be

Browse files
Canadadrynorberttech
authored andcommitted
add expanders 'After' and 'Before' for date comparaison (coduo#141)
1 parent c15fdbe commit fc2a8be

File tree

7 files changed

+265
-0
lines changed

7 files changed

+265
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ $matcher->match("lorem ipsum dolor", "@[email protected]('lorem').contains('ips
120120

121121
```
122122

123+
### Date matching
124+
125+
```php
126+
<?php
127+
128+
use Coduo\PHPMatcher\Factory\SimpleFactory;
129+
130+
$factory = new SimpleFactory();
131+
$matcher = $factory->createMatcher();
132+
133+
$matcher->match('2014-08-19', '@[email protected]()');
134+
$matcher->match('2014-08-19', '@[email protected]().before("2016-08-19")');
135+
$matcher->match('2014-08-19', '@[email protected]().before("today").after("+ 100year")');
136+
137+
```
138+
123139
### Integer matching
124140

125141
```php
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
6+
7+
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
8+
use Coduo\ToString\StringConverter;
9+
10+
final class After implements PatternExpander
11+
{
12+
const NAME = 'after';
13+
14+
private $boundary;
15+
16+
private $error;
17+
18+
19+
public function __construct($boundary)
20+
{
21+
if (false === \is_string($boundary)) {
22+
$this->error = \sprintf('After expander require "string", got "%s".', new StringConverter($boundary));
23+
return false;
24+
}
25+
26+
if (!$this->is_datetime($boundary)) {
27+
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date.', new StringConverter($boundary)));
28+
}
29+
30+
$this->boundary = new \DateTime($boundary);
31+
}
32+
33+
public static function is(string $name) : bool
34+
{
35+
return self::NAME === $name;
36+
}
37+
38+
public function match($value) : bool
39+
{
40+
if (false === \is_string($value)) {
41+
$this->error = \sprintf('After expander require "string", got "%s".', new StringConverter($value));
42+
return false;
43+
}
44+
45+
if (!$this->is_datetime($value)) {
46+
$this->error = \sprintf('Value "%s" is not a valid date.', new StringConverter($value));
47+
return false;
48+
}
49+
50+
$value = new \DateTime($value);
51+
52+
if ($value <= $this->boundary) {
53+
$this->error = \sprintf('Value "%s" is not after "%s".', new StringConverter($value), new StringConverter($this->boundary));
54+
return false;
55+
}
56+
57+
return $value > $this->boundary;
58+
}
59+
60+
private function is_datetime(string $value) : bool
61+
{
62+
try {
63+
new \DateTime($value);
64+
return true;
65+
} catch (\Exception $e) {
66+
return false;
67+
}
68+
}
69+
70+
public function getError()
71+
{
72+
return $this->error;
73+
}
74+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
6+
7+
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
8+
use Coduo\ToString\StringConverter;
9+
10+
final class Before implements PatternExpander
11+
{
12+
const NAME = 'before';
13+
14+
private $boundary;
15+
16+
private $error;
17+
18+
19+
public function __construct($boundary)
20+
{
21+
if (!\is_string($boundary)) {
22+
throw new \InvalidArgumentException(\sprintf('Before expander require "string", got "%s".', new StringConverter($boundary)));
23+
}
24+
25+
if (!$this->is_datetime($boundary)) {
26+
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date.', new StringConverter($boundary)));
27+
}
28+
29+
$this->boundary = new \DateTime($boundary);
30+
}
31+
32+
public static function is(string $name) : bool
33+
{
34+
return self::NAME === $name;
35+
}
36+
37+
public function match($value) : bool
38+
{
39+
if (!\is_string($value)) {
40+
$this->error = \sprintf('Before expander require "string", got "%s".', new StringConverter($value));
41+
return false;
42+
}
43+
44+
if (!$this->is_datetime($value)) {
45+
$this->error = \sprintf('Value "%s" is not a valid date.', new StringConverter($value));
46+
return false;
47+
}
48+
49+
$value = new \DateTime($value);
50+
51+
if ($value >= $this->boundary) {
52+
$this->error = \sprintf('Value "%s" is before "%s".', new StringConverter($value), new StringConverter($this->boundary));
53+
return false;
54+
}
55+
56+
return $value < $this->boundary;
57+
}
58+
59+
private function is_datetime(string $value) : bool
60+
{
61+
try {
62+
new \DateTime($value);
63+
return true;
64+
} catch (\Exception $e) {
65+
return false;
66+
}
67+
}
68+
69+
public function getError()
70+
{
71+
return $this->error;
72+
}
73+
}

src/Parser/ExpanderInitializer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
final class ExpanderInitializer
1616
{
1717
private $expanderDefinitions = [
18+
Expander\After::NAME => Expander\After::class,
19+
Expander\Before::NAME => Expander\Before::class,
1820
Expander\Contains::NAME => Expander\Contains::class,
1921
Expander\Count::NAME => Expander\Count::class,
2022
Expander\EndsWith::NAME => Expander\EndsWith::class,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
6+
7+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\After;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class AfterTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider examplesProvider
14+
*/
15+
public function test_examples($boundary, $value, $expectedResult)
16+
{
17+
$expander = new After($boundary);
18+
$this->assertEquals($expectedResult, $expander->match($value));
19+
}
20+
21+
public static function examplesProvider()
22+
{
23+
return [
24+
['+ 2 day','today',false],
25+
['2018-02-06T04:20:33','2017-02-06T04:20:33',false],
26+
['2017-02-06T04:20:33','2018-02-06T04:20:33',true],
27+
];
28+
}
29+
30+
/**
31+
* @dataProvider invalidCasesProvider
32+
*/
33+
public function test_error_when_matching_fail($boundary, $value, $errorMessage)
34+
{
35+
$expander = new After($boundary);
36+
$this->assertFalse($expander->match($value));
37+
$this->assertEquals($errorMessage, $expander->getError());
38+
}
39+
40+
public static function invalidCasesProvider()
41+
{
42+
return [
43+
['today', 'ipsum lorem', 'Value "ipsum lorem" is not a valid date.'],
44+
['2017-02-06T04:20:33', 'ipsum lorem', 'Value "ipsum lorem" is not a valid date.'],
45+
['today',5, 'After expander require "string", got "5".'],
46+
];
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
6+
7+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\Before;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class BeforeTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider examplesProvider
14+
*/
15+
public function test_examples($boundary, $value, $expectedResult)
16+
{
17+
$expander = new Before($boundary);
18+
$this->assertEquals($expectedResult, $expander->match($value));
19+
}
20+
21+
public static function examplesProvider()
22+
{
23+
return [
24+
['+ 2 day','today',true],
25+
['2018-02-06T04:20:33','2017-02-06T04:20:33',true],
26+
['2017-02-06T04:20:33','2018-02-06T04:20:33',false],
27+
];
28+
}
29+
30+
/**
31+
* @dataProvider invalidCasesProvider
32+
*/
33+
public function test_error_when_matching_fail($boundary, $value, $errorMessage)
34+
{
35+
$expander = new Before($boundary);
36+
$this->assertFalse($expander->match($value));
37+
$this->assertEquals($errorMessage, $expander->getError());
38+
}
39+
40+
public static function invalidCasesProvider()
41+
{
42+
return [
43+
['today', 'ipsum lorem', 'Value "ipsum lorem" is not a valid date.'],
44+
['2017-02-06T04:20:33', 'ipsum lorem', 'Value "ipsum lorem" is not a valid date.'],
45+
['today',5, 'Before expander require "string", got "5".'],
46+
];
47+
}
48+
}

tests/MatcherTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ public static function expanderExamples()
359359
['http://coduo.pl/', '@[email protected]()', true],
360360
['lorem ipsum', '@[email protected]()', false],
361361
['2014-08-19', '@[email protected]()', true],
362+
['3014-08-19', '@[email protected]("today")', false],
363+
['1014-08-19', '@[email protected]("+ 1day")', true],
364+
['3014-08-19', '@[email protected]("today")', true],
365+
['1014-08-19', '@[email protected]("+ 1day")', false],
362366
[100, '@[email protected](101).greaterThan(10)', true],
363367
['', '@[email protected]()', false],
364368
['lorem ipsum', '@[email protected]()', true],

0 commit comments

Comments
 (0)