1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
/*
4
6
* This file is part of the PHP-CRON-EXPR package.
5
7
*
@@ -28,7 +30,7 @@ class Validator
28
30
*
29
31
* @return bool
30
32
*/
31
- public function inRange ($ value , $ offset )
33
+ public function inRange (int $ value , string $ offset ): bool
32
34
{
33
35
$ parts = \explode ('- ' , $ offset );
34
36
@@ -43,7 +45,7 @@ public function inRange($value, $offset)
43
45
*
44
46
* @return bool
45
47
*/
46
- public function inStep ($ value , $ offset )
48
+ public function inStep (int $ value , string $ offset ): bool
47
49
{
48
50
$ parts = \explode ('/ ' , $ offset , 2 );
49
51
@@ -58,7 +60,7 @@ public function inStep($value, $offset)
58
60
$ parts = \explode ('/ ' , $ offset , 2 );
59
61
$ subparts = \explode ('- ' , $ parts [0 ], 2 ) + [1 => $ value ];
60
62
61
- return $ this ->inStepRange ($ value , $ subparts [0 ], $ subparts [1 ], $ parts [1 ]);
63
+ return $ this ->inStepRange (( int ) $ value , ( int ) $ subparts [0 ], ( int ) $ subparts [1 ], ( int ) $ parts [1 ]);
62
64
}
63
65
64
66
/**
@@ -71,7 +73,7 @@ public function inStep($value, $offset)
71
73
*
72
74
* @return bool
73
75
*/
74
- public function inStepRange ($ value , $ start , $ end , $ step )
76
+ public function inStepRange (int $ value , int $ start , int $ end , int $ step ): bool
75
77
{
76
78
if (($ start + $ step ) > $ end ) {
77
79
return false ;
@@ -92,31 +94,33 @@ public function inStepRange($value, $start, $end, $step)
92
94
* @param string $value
93
95
* @param array $time
94
96
*
95
- * @return bool|null
97
+ * @return bool
96
98
*/
97
- public function isValidMonthDay ($ value , $ time )
99
+ public function isValidMonthDay (string $ value , array $ time ): bool
98
100
{
99
101
if ($ value == 'L ' ) {
100
102
return $ time [2 ] == $ time [6 ];
101
103
}
102
104
103
105
if ($ pos = \strpos ($ value , 'W ' )) {
104
106
$ value = \substr ($ value , 0 , $ pos );
105
- $ month = \str_pad ($ time [8 ], 2 , ' 0 ' , \ STR_PAD_LEFT );
107
+ $ month = $ this -> zeroPad ($ time [8 ]);
106
108
107
- return $ this ->isClosestWeekDay ($ value , $ month , $ time );
109
+ return $ this ->isClosestWeekDay (( int ) $ value , $ month , $ time );
108
110
}
111
+
112
+ $ this ->unexpectedValue (2 , $ value );
109
113
}
110
114
111
- protected function isClosestWeekDay ($ value , $ month , $ time )
115
+ protected function isClosestWeekDay (int $ value , string $ month , array $ time ): bool
112
116
{
113
117
foreach ([0 , -1 , 1 , -2 , 2 ] as $ i ) {
114
118
$ incr = $ value + $ i ;
115
119
if ($ incr < 1 || $ incr > $ time [6 ]) {
116
120
continue ;
117
121
}
118
122
119
- $ incr = \str_pad ($ incr, 2 , ' 0 ' , \ STR_PAD_LEFT );
123
+ $ incr = $ this -> zeroPad ($ incr );
120
124
$ parts = \explode (' ' , \date ('N m j ' , \strtotime ("{$ time [5 ]}- $ month- $ incr " )));
121
125
if ($ parts [0 ] < 6 && $ parts [1 ] == $ month ) {
122
126
return $ time [2 ] == $ parts [2 ];
@@ -136,30 +140,43 @@ protected function isClosestWeekDay($value, $month, $time)
136
140
* @param string $value
137
141
* @param array $time
138
142
*
139
- * @return bool|null
143
+ * @return bool
140
144
*/
141
- public function isValidWeekDay ($ value , $ time )
145
+ public function isValidWeekDay (string $ value , array $ time ): bool
142
146
{
143
- $ month = \str_pad ($ time [8 ], 2 , ' 0 ' , \ STR_PAD_LEFT );
147
+ $ month = $ this -> zeroPad ($ time [8 ]);
144
148
145
149
if (\strpos ($ value , 'L ' )) {
146
150
return $ this ->isLastWeekDay ($ value , $ month , $ time );
147
151
}
148
152
149
153
if (!\strpos ($ value , '# ' )) {
150
- return null ;
154
+ $ this -> unexpectedValue ( 4 , $ value ); ;
151
155
}
152
156
153
157
list ($ day , $ nth ) = \explode ('# ' , \str_replace ('0# ' , '7# ' , $ value ));
154
158
155
- if (!$ this ->isNthWeekDay ($ day , $ nth ) || $ time [9 ] != $ day ) {
159
+ if (!$ this ->isNthWeekDay (( int ) $ day , ( int ) $ nth ) || $ time [9 ] != $ day ) {
156
160
return false ;
157
161
}
158
162
159
163
return \intval ($ time [7 ] / 7 ) == $ nth - 1 ;
160
164
}
161
165
162
- protected function isLastWeekDay ($ value , $ month , $ time )
166
+ /**
167
+ * @param int $pos
168
+ * @param string $value
169
+ *
170
+ * @throws \UnexpectedValueException
171
+ */
172
+ public function unexpectedValue (int $ pos , string $ value )
173
+ {
174
+ throw new \UnexpectedValueException (
175
+ \sprintf ('Invalid offset value at segment #%d: %s ' , $ pos , $ value )
176
+ );
177
+ }
178
+
179
+ protected function isLastWeekDay (string $ value , string $ month , array $ time ): bool
163
180
{
164
181
$ value = \explode ('L ' , \str_replace ('7L ' , '0L ' , $ value ));
165
182
$ decr = $ time [6 ];
@@ -174,8 +191,13 @@ protected function isLastWeekDay($value, $month, $time)
174
191
return false ;
175
192
}
176
193
177
- protected function isNthWeekDay ($ day , $ nth )
194
+ protected function isNthWeekDay (int $ day , int $ nth ): bool
178
195
{
179
196
return !($ day < 0 || $ day > 7 || $ nth < 1 || $ nth > 5 );
180
197
}
198
+
199
+ protected function zeroPad ($ value ): string
200
+ {
201
+ return \str_pad ((string ) $ value , 2 , '0 ' , \STR_PAD_LEFT );
202
+ }
181
203
}
0 commit comments