Skip to content

Commit 1c99056

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

File tree

8 files changed

+713
-805
lines changed

8 files changed

+713
-805
lines changed

composer.lock

Lines changed: 127 additions & 115 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\PHPMatcher\Matcher\Pattern\PatternExpander;
9+
use Coduo\ToString\StringConverter;
10+
11+
abstract class AbstractDateTimeComparison implements PatternExpander
12+
{
13+
use BacktraceBehavior;
14+
15+
/**
16+
* @var string
17+
*/
18+
public const NAME = 'after';
19+
20+
protected readonly DateTime $boundary;
21+
22+
protected ?string $error = null;
23+
24+
public function __construct(string $boundary)
25+
{
26+
if (!\is_string($boundary)) {
27+
throw new \InvalidArgumentException(\sprintf('Before expander require "string", got "%s".', new StringConverter($boundary)));
28+
}
29+
30+
try {
31+
$this->boundary = DateTime::fromString($boundary);
32+
} catch (\Exception $exception) {
33+
throw new \InvalidArgumentException(\sprintf('Boundary value "%s" is not a valid date, date time or time.', new StringConverter($boundary)));
34+
}
35+
}
36+
37+
public static function is(string $name) : bool
38+
{
39+
return static::getName() === $name;
40+
}
41+
42+
public function match($value) : bool
43+
{
44+
$this->backtrace->expanderEntrance(static::getName(), $value);
45+
46+
if (!\is_string($value)) {
47+
$this->error = \sprintf('%s expander require "string", got "%s".', static::getName(), new StringConverter($value));
48+
$this->backtrace->expanderFailed(static::getName(), $value, $this->error);
49+
50+
return false;
51+
}
52+
53+
return $this->compare($value);
54+
}
55+
56+
public function getError() : ?string
57+
{
58+
return $this->error;
59+
}
60+
61+
abstract protected static function getName() : string;
62+
63+
/**
64+
* @param string $value raw value
65+
* @param DateTime $datetime value converted in DateTime object
66+
*/
67+
abstract protected function handleComparison(string $value, DateTime $datetime) : bool;
68+
69+
private function compare(string $value) : bool
70+
{
71+
try {
72+
$datetime = DateTime::fromString($value);
73+
} catch (\Exception $e) {
74+
$this->error = \sprintf('Value "%s" is not a valid date, date time or time.', new StringConverter($value));
75+
$this->backtrace->expanderFailed(static::getName(), $value, $this->error);
76+
77+
return false;
78+
}
79+
80+
return $this->handleComparison($value, $datetime);
81+
}
82+
}

src/Matcher/Pattern/Expander/After.php

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

77
use Aeon\Calendar\Gregorian\DateTime;
8-
use Aeon\Calendar\Gregorian\Time;
9-
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
108
use Coduo\ToString\StringConverter;
119

12-
final class After implements PatternExpander
10+
final class After extends AbstractDateTimeComparison
1311
{
14-
use BacktraceBehavior;
15-
1612
/**
1713
* @var string
1814
*/
1915
public const NAME = 'after';
2016

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
17+
protected function handleComparison(string $value, DateTime $datetime) : bool
4918
{
50-
return self::NAME === $name;
51-
}
52-
53-
public function match($value) : bool
54-
{
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));
19+
if ($datetime->isBeforeOrEqualTo($this->boundary)) {
20+
$this->error = \sprintf('Value "%s" is before or equal to "%s".', new StringConverter($value), new StringConverter($this->boundary));
5921
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
6022

6123
return false;
6224
}
6325

64-
if ($this->boundaryDateTime instanceof DateTime) {
65-
return $this->compareDateTime($value);
66-
}
67-
68-
return $this->compareTime($value);
69-
}
70-
71-
public function getError() : ?string
72-
{
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);
26+
$this->backtrace->expanderSucceed(self::NAME, $value);
9427

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-
}
28+
return true;
10829
}
10930

110-
/**
111-
* @param string $value
112-
*
113-
* @return bool
114-
*/
115-
private function compareTime(string $value) : bool
31+
protected static function getName() : string
11632
{
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-
}
33+
return self::NAME;
14234
}
14335
}

src/Matcher/Pattern/Expander/Before.php

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

77
use Aeon\Calendar\Gregorian\DateTime;
8-
use Aeon\Calendar\Gregorian\Time;
9-
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
108
use Coduo\ToString\StringConverter;
119

12-
final class Before implements PatternExpander
10+
final class Before extends AbstractDateTimeComparison
1311
{
14-
use BacktraceBehavior;
15-
1612
/**
1713
* @var string
1814
*/
1915
public const NAME = 'before';
2016

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
17+
protected function handleComparison(string $value, DateTime $datetime) : bool
4918
{
50-
return self::NAME === $name;
51-
}
52-
53-
public function match($value) : bool
54-
{
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));
19+
if ($datetime->isAfterOrEqualTo($this->boundary)) {
20+
$this->error = \sprintf('Value "%s" is after or equal to "%s".', $value, new StringConverter($this->boundary));
5921
$this->backtrace->expanderFailed(self::NAME, $value, $this->error);
6022

6123
return false;
6224
}
6325

64-
if ($this->boundaryDateTime instanceof DateTime) {
65-
return $this->compareDateTime($value);
66-
}
67-
68-
return $this->compareTime($value);
69-
}
70-
71-
public function getError() : ?string
72-
{
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);
26+
$this->backtrace->expanderSucceed(self::NAME, $value);
9427

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-
}
28+
return true;
10829
}
10930

110-
/**
111-
* @param string $value
112-
*
113-
* @return bool
114-
*/
115-
private function compareTime(string $value) : bool
31+
protected static function getName() : string
11632
{
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-
}
33+
return self::NAME;
14234
}
14335
}

0 commit comments

Comments
 (0)