Skip to content

Commit 279790c

Browse files
author
Stephane Leveugle
committed
Fix After/Before expanders error message when date/time is equal to boundary
1 parent 2d82377 commit 279790c

File tree

9 files changed

+342
-847
lines changed

9 files changed

+342
-847
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"ext-filter": "*",
2020
"ext-json": "*",
2121
"ext-simplexml": "*",
22-
"aeon-php/calendar": "^1.0",
22+
"aeon-php/calendar": "^1.0.6",
2323
"coduo/php-to-string": "^3",
2424
"doctrine/lexer": "^3.0"
2525
},
@@ -29,6 +29,7 @@
2929
"openlss/lib-array2xml": "^1.0",
3030
"symfony/expression-language": "^2.3|^3.0|^4.0|^5.0|^6.0",
3131
"symfony/cache": "^2.3|^3.0|^4.0|^5.0|^6.0",
32+
"nikic/php-parser": "^4.18",
3233
"symfony/var-exporter": "^2.3|^3.0|^4.0|^5.0|^6.0"
3334
},
3435
"suggest": {

composer.lock

Lines changed: 124 additions & 114 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Matcher/Pattern/Expander/After.php

Lines changed: 8 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -5,139 +5,34 @@
55
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
66

77
use Aeon\Calendar\Gregorian\DateTime;
8-
use Aeon\Calendar\Gregorian\Time;
98
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
109
use Coduo\ToString\StringConverter;
1110

1211
final class After implements PatternExpander
1312
{
14-
use BacktraceBehavior;
13+
use DateTimeComparisonTrait;
1514

1615
/**
1716
* @var string
1817
*/
1918
public const NAME = 'after';
2019

21-
private ?DateTime $boundaryDateTime;
22-
23-
private ?Time $boundaryTime;
24-
25-
private ?string $error;
26-
27-
public function __construct($boundary)
28-
{
29-
$this->error = null;
30-
$this->boundaryTime = null;
31-
$this->boundaryDateTime = null;
32-
33-
if (!\is_string($boundary)) {
34-
$this->error = \sprintf('After expander require "string", got "%s".', new StringConverter($boundary));
35-
}
36-
37-
try {
38-
$this->boundaryDateTime = DateTime::fromString($boundary);
39-
} catch (\Exception $exception) {
40-
try {
41-
$this->boundaryTime = Time::fromString($boundary);
42-
} catch (\Exception $exception) {
43-
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date, date time or time.', new StringConverter($boundary)));
44-
}
45-
}
46-
}
47-
48-
public static function is(string $name) : bool
49-
{
50-
return self::NAME === $name;
51-
}
52-
53-
public function match($value) : bool
20+
protected function handleComparison(string $value, DateTime $datetime) : bool
5421
{
55-
$this->backtrace->expanderEntrance(self::NAME, $value);
56-
57-
if (!\is_string($value)) {
58-
$this->error = \sprintf('After expander require "string", got "%s".', new StringConverter($value));
22+
if ($datetime->isBeforeOrEqualTo($this->boundary)) {
23+
$this->error = \sprintf('Value "%s" is before or equal to "%s".', new StringConverter($value), new StringConverter($this->boundary));
5924
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
6025

6126
return false;
6227
}
6328

64-
if ($this->boundaryDateTime instanceof DateTime) {
65-
return $this->compareDateTime($value);
66-
}
29+
$this->backtrace->expanderSucceed(self::NAME, $value);
6730

68-
return $this->compareTime($value);
31+
return true;
6932
}
7033

71-
public function getError() : ?string
34+
protected static function getName() : string
7235
{
73-
return $this->error;
74-
}
75-
76-
/**
77-
* @param string $value
78-
*
79-
* @return bool
80-
*/
81-
private function compareDateTime(string $value) : bool
82-
{
83-
try {
84-
$datetime = DateTime::fromString($value);
85-
86-
if ($datetime->isBefore($this->boundaryDateTime)) {
87-
$this->error = \sprintf('Value "%s" is after "%s".', new StringConverter($value), new StringConverter($this->boundaryDateTime));
88-
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
89-
90-
return false;
91-
}
92-
93-
$result = $datetime->isAfter($this->boundaryDateTime);
94-
95-
if ($result) {
96-
$this->backtrace->expanderSucceed(self::NAME, $value);
97-
} else {
98-
$this->backtrace->expanderFailed(self::NAME, $value, '');
99-
}
100-
101-
return $result;
102-
} catch (\Exception $e) {
103-
$this->error = \sprintf('Value "%s" is not a valid date.', new StringConverter($value));
104-
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
105-
106-
return false;
107-
}
108-
}
109-
110-
/**
111-
* @param string $value
112-
*
113-
* @return bool
114-
*/
115-
private function compareTime(string $value) : bool
116-
{
117-
try {
118-
$datetime = Time::fromString($value);
119-
120-
if ($datetime->isLessThan($this->boundaryTime)) {
121-
$this->error = \sprintf('Value "%s" is after "%s".', new StringConverter($value), new StringConverter($this->boundaryTime));
122-
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
123-
124-
return false;
125-
}
126-
127-
$result = $datetime->isGreaterThan($this->boundaryTime);
128-
129-
if ($result) {
130-
$this->backtrace->expanderSucceed(self::NAME, $value);
131-
} else {
132-
$this->backtrace->expanderFailed(self::NAME, $value, '');
133-
}
134-
135-
return $result;
136-
} catch (\Exception $e) {
137-
$this->error = \sprintf('Value "%s" is not a valid time.', new StringConverter($value));
138-
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
139-
140-
return false;
141-
}
36+
return self::NAME;
14237
}
14338
}

src/Matcher/Pattern/Expander/Before.php

Lines changed: 8 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -5,139 +5,34 @@
55
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
66

77
use Aeon\Calendar\Gregorian\DateTime;
8-
use Aeon\Calendar\Gregorian\Time;
98
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
109
use Coduo\ToString\StringConverter;
1110

1211
final class Before implements PatternExpander
1312
{
14-
use BacktraceBehavior;
13+
use DateTimeComparisonTrait;
1514

1615
/**
1716
* @var string
1817
*/
1918
public const NAME = 'before';
2019

21-
private ?DateTime $boundaryDateTime;
22-
23-
private ?Time $boundaryTime;
24-
25-
private ?string $error;
26-
27-
public function __construct(string $boundary)
28-
{
29-
$this->error = null;
30-
$this->boundaryTime = null;
31-
$this->boundaryDateTime = null;
32-
33-
if (!\is_string($boundary)) {
34-
throw new \InvalidArgumentException(\sprintf('Before expander require "string", got "%s".', new StringConverter($boundary)));
35-
}
36-
37-
try {
38-
$this->boundaryDateTime = DateTime::fromString($boundary);
39-
} catch (\Exception $exception) {
40-
try {
41-
$this->boundaryTime = Time::fromString($boundary);
42-
} catch (\Exception $exception) {
43-
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date, date time or time.', new StringConverter($boundary)));
44-
}
45-
}
46-
}
47-
48-
public static function is(string $name) : bool
49-
{
50-
return self::NAME === $name;
51-
}
52-
53-
public function match($value) : bool
20+
protected function handleComparison(string $value, DateTime $datetime) : bool
5421
{
55-
$this->backtrace->expanderEntrance(self::NAME, $value);
56-
57-
if (!\is_string($value)) {
58-
$this->error = \sprintf('Before expander require "string", got "%s".', new StringConverter($value));
22+
if ($datetime->isAfterOrEqualTo($this->boundary)) {
23+
$this->error = \sprintf('Value "%s" is after or equal to "%s".', $value, new StringConverter($this->boundary));
5924
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
6025

6126
return false;
6227
}
6328

64-
if ($this->boundaryDateTime instanceof DateTime) {
65-
return $this->compareDateTime($value);
66-
}
29+
$this->backtrace->expanderSucceed(self::NAME, $value);
6730

68-
return $this->compareTime($value);
31+
return true;
6932
}
7033

71-
public function getError() : ?string
34+
protected static function getName() : string
7235
{
73-
return $this->error;
74-
}
75-
76-
/**
77-
* @param string $value
78-
*
79-
* @return bool
80-
*/
81-
private function compareDateTime(string $value) : bool
82-
{
83-
try {
84-
$datetime = DateTime::fromString($value);
85-
86-
if ($datetime->isAfter($this->boundaryDateTime)) {
87-
$this->error = \sprintf('Value "%s" is before "%s".', new StringConverter($value), new StringConverter($this->boundaryDateTime));
88-
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
89-
90-
return false;
91-
}
92-
93-
$result = $datetime->isBefore($this->boundaryDateTime);
94-
95-
if ($result) {
96-
$this->backtrace->expanderSucceed(self::NAME, $value);
97-
} else {
98-
$this->backtrace->expanderFailed(self::NAME, $value, '');
99-
}
100-
101-
return $result;
102-
} catch (\Exception $e) {
103-
$this->error = \sprintf('Value "%s" is not a valid date.', new StringConverter($value));
104-
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
105-
106-
return false;
107-
}
108-
}
109-
110-
/**
111-
* @param string $value
112-
*
113-
* @return bool
114-
*/
115-
private function compareTime(string $value) : bool
116-
{
117-
try {
118-
$datetime = Time::fromString($value);
119-
120-
if ($datetime->isGreaterThan($this->boundaryTime)) {
121-
$this->error = \sprintf('Value "%s" is before "%s".', new StringConverter($value), new StringConverter($this->boundaryTime));
122-
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
123-
124-
return false;
125-
}
126-
127-
$result = $datetime->isLessThan($this->boundaryTime);
128-
129-
if ($result) {
130-
$this->backtrace->expanderSucceed(self::NAME, $value);
131-
} else {
132-
$this->backtrace->expanderFailed(self::NAME, $value, '');
133-
}
134-
135-
return $result;
136-
} catch (\Exception $e) {
137-
$this->error = \sprintf('Value "%s" is not a valid time.', new StringConverter($value));
138-
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
139-
140-
return false;
141-
}
36+
return self::NAME;
14237
}
14338
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
6+
7+
use Aeon\Calendar\Gregorian\DateTime;
8+
use Coduo\ToString\StringConverter;
9+
10+
trait DateTimeComparisonTrait
11+
{
12+
use BacktraceBehavior;
13+
14+
private readonly DateTime $boundary;
15+
16+
private ?string $error = null;
17+
18+
public function __construct(string $boundary)
19+
{
20+
if (!\is_string($boundary)) {
21+
throw new \InvalidArgumentException(\sprintf('Before expander require "string", got "%s".', new StringConverter($boundary)));
22+
}
23+
24+
try {
25+
$this->boundary = DateTime::fromString($boundary);
26+
} catch (\Exception $exception) {
27+
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date, date time or time.', new StringConverter($boundary)));
28+
}
29+
}
30+
31+
public static function is(string $name) : bool
32+
{
33+
return static::getName() === $name;
34+
}
35+
36+
public function match($value) : bool
37+
{
38+
$this->backtrace->expanderEntrance(static::getName(), $value);
39+
40+
if (!\is_string($value)) {
41+
$this->error = \sprintf('%s expander require "string", got "%s".', static::getName(), new StringConverter($value));
42+
$this->backtrace->expanderFailed(static::getName(), $value, $this->error);
43+
44+
return false;
45+
}
46+
47+
return $this->compare($value);
48+
}
49+
50+
public function getError() : ?string
51+
{
52+
return $this->error;
53+
}
54+
55+
abstract protected static function getName() : string;
56+
57+
/**
58+
* @param string $value raw value
59+
* @param DateTime $datetime value converted in DateTime object
60+
*/
61+
abstract protected function handleComparison(string $value, DateTime $datetime) : bool;
62+
63+
private function compare(string $value) : bool
64+
{
65+
try {
66+
$datetime = DateTime::fromString($value);
67+
} catch (\Exception $e) {
68+
$this->error = \sprintf('Value "%s" is not a valid date, date time or time.', new StringConverter($value));
69+
$this->backtrace->expanderFailed(static::getName(), $value, $this->error);
70+
71+
return false;
72+
}
73+
74+
return $this->handleComparison($value, $datetime);
75+
}
76+
}

0 commit comments

Comments
 (0)