Skip to content

Commit 05f139d

Browse files
committed
refactor(validator): use reference time class
1 parent 7b4138f commit 05f139d

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/Validator.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,38 +92,40 @@ public function inStepRange(int $value, int $start, int $end, int $step): bool
9292
* @internal
9393
*
9494
* @param string $value
95-
* @param array $time
95+
* @param ReferenceTime $time
9696
*
9797
* @return bool
9898
*/
99-
public function isValidMonthDay(string $value, array $time): bool
99+
public function isValidMonthDay(string $value, ReferenceTime $reference): bool
100100
{
101101
if ($value == 'L') {
102-
return $time[2] == $time[6];
102+
return $reference->monthDay() == $reference->numDays();
103103
}
104104

105105
if ($pos = \strpos($value, 'W')) {
106106
$value = \substr($value, 0, $pos);
107-
$month = $this->zeroPad($time[8]);
107+
$month = $this->zeroPad($reference->month());
108108

109-
return $this->isClosestWeekDay((int) $value, $month, $time);
109+
return $this->isClosestWeekDay((int) $value, $month, $reference);
110110
}
111111

112112
$this->unexpectedValue(2, $value);
113+
// @codeCoverageIgnoreStart
113114
}
115+
// @codeCoverageIgnoreEnd
114116

115-
protected function isClosestWeekDay(int $value, string $month, array $time): bool
117+
protected function isClosestWeekDay(int $value, string $month, ReferenceTime $reference): bool
116118
{
117119
foreach ([0, -1, 1, -2, 2] as $i) {
118120
$incr = $value + $i;
119-
if ($incr < 1 || $incr > $time[6]) {
121+
if ($incr < 1 || $incr > $reference->numDays()) {
120122
continue;
121123
}
122124

123125
$incr = $this->zeroPad($incr);
124-
$parts = \explode(' ', \date('N m j', \strtotime("{$time[5]}-$month-$incr")));
126+
$parts = \explode(' ', \date('N m j', \strtotime("{$reference->year()}-$month-$incr")));
125127
if ($parts[0] < 6 && $parts[1] == $month) {
126-
return $time[2] == $parts[2];
128+
return $reference->monthDay() == $parts[2];
127129
}
128130
}
129131

@@ -138,16 +140,16 @@ protected function isClosestWeekDay(int $value, string $month, array $time): boo
138140
* @internal
139141
*
140142
* @param string $value
141-
* @param array $time
143+
* @param ReferenceTime $time
142144
*
143145
* @return bool
144146
*/
145-
public function isValidWeekDay(string $value, array $time): bool
147+
public function isValidWeekDay(string $value, ReferenceTime $reference): bool
146148
{
147-
$month = $this->zeroPad($time[8]);
149+
$month = $this->zeroPad($reference->month());
148150

149151
if (\strpos($value, 'L')) {
150-
return $this->isLastWeekDay($value, $month, $time);
152+
return $this->isLastWeekDay($value, $month, $reference);
151153
}
152154

153155
if (!\strpos($value, '#')) {
@@ -156,11 +158,11 @@ public function isValidWeekDay(string $value, array $time): bool
156158

157159
list($day, $nth) = \explode('#', \str_replace('0#', '7#', $value));
158160

159-
if (!$this->isNthWeekDay((int) $day, (int) $nth) || $time[9] != $day) {
161+
if (!$this->isNthWeekDay((int) $day, (int) $nth) || $reference->weekDay1() != $day) {
160162
return false;
161163
}
162164

163-
return \intval($time[7] / 7) == $nth - 1;
165+
return \intval($reference->day() / 7) == $nth - 1;
164166
}
165167

166168
/**
@@ -176,15 +178,15 @@ public function unexpectedValue(int $pos, string $value)
176178
);
177179
}
178180

179-
protected function isLastWeekDay(string $value, string $month, array $time): bool
181+
protected function isLastWeekDay(string $value, string $month, ReferenceTime $reference): bool
180182
{
183+
$decr = $reference->numDays();
181184
$value = \explode('L', \str_replace('7L', '0L', $value));
182-
$decr = $time[6];
183185

184186
for ($i = 0; $i < 7; $i++) {
185187
$decr -= $i;
186-
if (\date('w', \strtotime("{$time[5]}-$month-$decr")) == $value[0]) {
187-
return $time[2] == $decr;
188+
if (\date('w', \strtotime("{$reference->year()}-$month-$decr")) == $value[0]) {
189+
return $reference->monthDay() == $decr;
188190
}
189191
}
190192

0 commit comments

Comments
 (0)