-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add expanders 'After' and 'Before' for date comparaison (#141)
- Loading branch information
1 parent
c15fdbe
commit fc2a8be
Showing
7 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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".'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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".'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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], | ||
|