11<?php
22
3+ declare (strict_types=1 );
4+
35/*
46 * This file is part of the PHP-CRON-EXPR package.
57 *
@@ -28,7 +30,7 @@ class Validator
2830 *
2931 * @return bool
3032 */
31- public function inRange ($ value , $ offset )
33+ public function inRange (int $ value , string $ offset ): bool
3234 {
3335 $ parts = \explode ('- ' , $ offset );
3436
@@ -43,7 +45,7 @@ public function inRange($value, $offset)
4345 *
4446 * @return bool
4547 */
46- public function inStep ($ value , $ offset )
48+ public function inStep (int $ value , string $ offset ): bool
4749 {
4850 $ parts = \explode ('/ ' , $ offset , 2 );
4951
@@ -58,7 +60,7 @@ public function inStep($value, $offset)
5860 $ parts = \explode ('/ ' , $ offset , 2 );
5961 $ subparts = \explode ('- ' , $ parts [0 ], 2 ) + [1 => $ value ];
6062
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 ]);
6264 }
6365
6466 /**
@@ -71,7 +73,7 @@ public function inStep($value, $offset)
7173 *
7274 * @return bool
7375 */
74- public function inStepRange ($ value , $ start , $ end , $ step )
76+ public function inStepRange (int $ value , int $ start , int $ end , int $ step ): bool
7577 {
7678 if (($ start + $ step ) > $ end ) {
7779 return false ;
@@ -92,31 +94,33 @@ public function inStepRange($value, $start, $end, $step)
9294 * @param string $value
9395 * @param array $time
9496 *
95- * @return bool|null
97+ * @return bool
9698 */
97- public function isValidMonthDay ($ value , $ time )
99+ public function isValidMonthDay (string $ value , array $ time ): bool
98100 {
99101 if ($ value == 'L ' ) {
100102 return $ time [2 ] == $ time [6 ];
101103 }
102104
103105 if ($ pos = \strpos ($ value , 'W ' )) {
104106 $ value = \substr ($ value , 0 , $ pos );
105- $ month = \str_pad ($ time [8 ], 2 , ' 0 ' , \ STR_PAD_LEFT );
107+ $ month = $ this -> zeroPad ($ time [8 ]);
106108
107- return $ this ->isClosestWeekDay ($ value , $ month , $ time );
109+ return $ this ->isClosestWeekDay (( int ) $ value , $ month , $ time );
108110 }
111+
112+ $ this ->unexpectedValue (2 , $ value );
109113 }
110114
111- protected function isClosestWeekDay ($ value , $ month , $ time )
115+ protected function isClosestWeekDay (int $ value , string $ month , array $ time ): bool
112116 {
113117 foreach ([0 , -1 , 1 , -2 , 2 ] as $ i ) {
114118 $ incr = $ value + $ i ;
115119 if ($ incr < 1 || $ incr > $ time [6 ]) {
116120 continue ;
117121 }
118122
119- $ incr = \str_pad ($ incr, 2 , ' 0 ' , \ STR_PAD_LEFT );
123+ $ incr = $ this -> zeroPad ($ incr );
120124 $ parts = \explode (' ' , \date ('N m j ' , \strtotime ("{$ time [5 ]}- $ month- $ incr " )));
121125 if ($ parts [0 ] < 6 && $ parts [1 ] == $ month ) {
122126 return $ time [2 ] == $ parts [2 ];
@@ -136,30 +140,43 @@ protected function isClosestWeekDay($value, $month, $time)
136140 * @param string $value
137141 * @param array $time
138142 *
139- * @return bool|null
143+ * @return bool
140144 */
141- public function isValidWeekDay ($ value , $ time )
145+ public function isValidWeekDay (string $ value , array $ time ): bool
142146 {
143- $ month = \str_pad ($ time [8 ], 2 , ' 0 ' , \ STR_PAD_LEFT );
147+ $ month = $ this -> zeroPad ($ time [8 ]);
144148
145149 if (\strpos ($ value , 'L ' )) {
146150 return $ this ->isLastWeekDay ($ value , $ month , $ time );
147151 }
148152
149153 if (!\strpos ($ value , '# ' )) {
150- return null ;
154+ $ this -> unexpectedValue ( 4 , $ value ) ;
151155 }
152156
153157 list ($ day , $ nth ) = \explode ('# ' , \str_replace ('0# ' , '7# ' , $ value ));
154158
155- if (!$ this ->isNthWeekDay ($ day , $ nth ) || $ time [9 ] != $ day ) {
159+ if (!$ this ->isNthWeekDay (( int ) $ day , ( int ) $ nth ) || $ time [9 ] != $ day ) {
156160 return false ;
157161 }
158162
159163 return \intval ($ time [7 ] / 7 ) == $ nth - 1 ;
160164 }
161165
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
163180 {
164181 $ value = \explode ('L ' , \str_replace ('7L ' , '0L ' , $ value ));
165182 $ decr = $ time [6 ];
@@ -174,8 +191,13 @@ protected function isLastWeekDay($value, $month, $time)
174191 return false ;
175192 }
176193
177- protected function isNthWeekDay ($ day , $ nth )
194+ protected function isNthWeekDay (int $ day , int $ nth ): bool
178195 {
179196 return !($ day < 0 || $ day > 7 || $ nth < 1 || $ nth > 5 );
180197 }
198+
199+ protected function zeroPad ($ value ): string
200+ {
201+ return \str_pad ((string ) $ value , 2 , '0 ' , \STR_PAD_LEFT );
202+ }
181203}
0 commit comments