|
5 | 5 | namespace Coduo\PHPMatcher\Matcher\Pattern\Expander; |
6 | 6 |
|
7 | 7 | use Aeon\Calendar\Gregorian\DateTime; |
8 | | -use Aeon\Calendar\Gregorian\Time; |
9 | 8 | use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander; |
10 | 9 | use Coduo\ToString\StringConverter; |
11 | 10 |
|
12 | 11 | final class Before implements PatternExpander |
13 | 12 | { |
14 | | - use BacktraceBehavior; |
| 13 | + use DateTimeComparisonTrait; |
15 | 14 |
|
16 | 15 | /** |
17 | 16 | * @var string |
18 | 17 | */ |
19 | 18 | public const NAME = 'before'; |
20 | 19 |
|
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 |
54 | 21 | { |
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)); |
59 | 24 | $this->backtrace->expanderFailed(self::NAME, $value, $this->error); |
60 | 25 |
|
61 | 26 | return false; |
62 | 27 | } |
63 | 28 |
|
64 | | - if ($this->boundaryDateTime instanceof DateTime) { |
65 | | - return $this->compareDateTime($value); |
66 | | - } |
| 29 | + $this->backtrace->expanderSucceed(self::NAME, $value); |
67 | 30 |
|
68 | | - return $this->compareTime($value); |
| 31 | + return true; |
69 | 32 | } |
70 | 33 |
|
71 | | - public function getError() : ?string |
| 34 | + protected static function getName() : string |
72 | 35 | { |
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; |
142 | 37 | } |
143 | 38 | } |
0 commit comments