@@ -22,10 +22,16 @@ class FormatRules
2222{
2323 /**
2424 * Alpha
25+ *
26+ * @param string|null $str
2527 */
26- public function alpha (? string $ str = null ): bool
28+ public function alpha ($ str = null ): bool
2729 {
28- return ctype_alpha ($ str ?? '' );
30+ if (! is_string ($ str )) {
31+ $ str = (string ) $ str ;
32+ }
33+
34+ return ctype_alpha ($ str );
2935 }
3036
3137 /**
@@ -35,12 +41,16 @@ public function alpha(?string $str = null): bool
3541 *
3642 * @return bool True if alpha with spaces, else false.
3743 */
38- public function alpha_space (? string $ value = null ): bool
44+ public function alpha_space ($ value = null ): bool
3945 {
4046 if ($ value === null ) {
4147 return true ;
4248 }
4349
50+ if (! is_string ($ value )) {
51+ $ value = (string ) $ value ;
52+ }
53+
4454 // @see https://regex101.com/r/LhqHPO/1
4555 return (bool ) preg_match ('/\A[A-Z ]+\z/i ' , $ value );
4656 }
@@ -56,6 +66,10 @@ public function alpha_dash(?string $str = null): bool
5666 return false ;
5767 }
5868
69+ if (! is_string ($ str )) {
70+ $ str = (string ) $ str ;
71+ }
72+
5973 return preg_match ('/\A[a-z0-9_-]+\z/i ' , $ str ) === 1 ;
6074 }
6175
@@ -78,24 +92,40 @@ public function alpha_numeric_punct($str)
7892 return false ;
7993 }
8094
95+ if (! is_string ($ str )) {
96+ $ str = (string ) $ str ;
97+ }
98+
8199 return preg_match ('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i ' , $ str ) === 1 ;
82100 }
83101
84102 /**
85103 * Alphanumeric
104+ *
105+ * @param string|null $str
86106 */
87- public function alpha_numeric (? string $ str = null ): bool
107+ public function alpha_numeric ($ str = null ): bool
88108 {
89- return ctype_alnum ($ str ?? '' );
109+ if (! is_string ($ str )) {
110+ $ str = (string ) $ str ;
111+ }
112+
113+ return ctype_alnum ($ str );
90114 }
91115
92116 /**
93117 * Alphanumeric w/ spaces
118+ *
119+ * @param string|null $str
94120 */
95- public function alpha_numeric_space (? string $ str = null ): bool
121+ public function alpha_numeric_space ($ str = null ): bool
96122 {
123+ if (! is_string ($ str )) {
124+ $ str = (string ) $ str ;
125+ }
126+
97127 // @see https://regex101.com/r/0AZDME/1
98- return (bool ) preg_match ('/\A[A-Z0-9 ]+\z/i ' , $ str ?? '' );
128+ return (bool ) preg_match ('/\A[A-Z0-9 ]+\z/i ' , $ str );
99129 }
100130
101131 /**
@@ -113,109 +143,175 @@ public function string($str = null): bool
113143
114144 /**
115145 * Decimal number
146+ *
147+ * @param string|null $str
116148 */
117- public function decimal (? string $ str = null ): bool
149+ public function decimal ($ str = null ): bool
118150 {
151+ if (! is_string ($ str )) {
152+ $ str = (string ) $ str ;
153+ }
154+
119155 // @see https://regex101.com/r/HULifl/2/
120- return (bool ) preg_match ('/\A[-+]?\d{0,}\.?\d+\z/ ' , $ str ?? '' );
156+ return (bool ) preg_match ('/\A[-+]?\d{0,}\.?\d+\z/ ' , $ str );
121157 }
122158
123159 /**
124160 * String of hexidecimal characters
161+ *
162+ * @param string|null $str
125163 */
126- public function hex (? string $ str = null ): bool
164+ public function hex ($ str = null ): bool
127165 {
128- return ctype_xdigit ($ str ?? '' );
166+ if (! is_string ($ str )) {
167+ $ str = (string ) $ str ;
168+ }
169+
170+ return ctype_xdigit ($ str );
129171 }
130172
131173 /**
132174 * Integer
175+ *
176+ * @param string|null $str
133177 */
134- public function integer (? string $ str = null ): bool
178+ public function integer ($ str = null ): bool
135179 {
136- return (bool ) preg_match ('/\A[\-+]?\d+\z/ ' , $ str ?? '' );
180+ if (! is_string ($ str )) {
181+ $ str = (string ) $ str ;
182+ }
183+
184+ return (bool ) preg_match ('/\A[\-+]?\d+\z/ ' , $ str );
137185 }
138186
139187 /**
140188 * Is a Natural number (0,1,2,3, etc.)
189+ *
190+ * @param string|null $str
141191 */
142- public function is_natural (? string $ str = null ): bool
192+ public function is_natural ($ str = null ): bool
143193 {
144- return ctype_digit ($ str ?? '' );
194+ if (! is_string ($ str )) {
195+ $ str = (string ) $ str ;
196+ }
197+
198+ return ctype_digit ($ str );
145199 }
146200
147201 /**
148202 * Is a Natural number, but not a zero (1,2,3, etc.)
203+ *
204+ * @param string|null $str
149205 */
150- public function is_natural_no_zero (? string $ str = null ): bool
206+ public function is_natural_no_zero ($ str = null ): bool
151207 {
152- return $ str !== '0 ' && ctype_digit ($ str ?? '' );
208+ if (! is_string ($ str )) {
209+ $ str = (string ) $ str ;
210+ }
211+
212+ return $ str !== '0 ' && ctype_digit ($ str );
153213 }
154214
155215 /**
156216 * Numeric
217+ *
218+ * @param string|null $str
157219 */
158- public function numeric (? string $ str = null ): bool
220+ public function numeric ($ str = null ): bool
159221 {
222+ if (! is_string ($ str )) {
223+ $ str = (string ) $ str ;
224+ }
225+
160226 // @see https://regex101.com/r/bb9wtr/2
161- return (bool ) preg_match ('/\A[\-+]?\d*\.?\d+\z/ ' , $ str ?? '' );
227+ return (bool ) preg_match ('/\A[\-+]?\d*\.?\d+\z/ ' , $ str );
162228 }
163229
164230 /**
165231 * Compares value against a regular expression pattern.
232+ *
233+ * @param string|null $str
166234 */
167- public function regex_match (? string $ str , string $ pattern ): bool
235+ public function regex_match ($ str , string $ pattern ): bool
168236 {
237+ if (! is_string ($ str )) {
238+ $ str = (string ) $ str ;
239+ }
240+
169241 if (strpos ($ pattern , '/ ' ) !== 0 ) {
170242 $ pattern = "/ {$ pattern }/ " ;
171243 }
172244
173- return (bool ) preg_match ($ pattern , $ str ?? '' );
245+ return (bool ) preg_match ($ pattern , $ str );
174246 }
175247
176248 /**
177249 * Validates that the string is a valid timezone as per the
178250 * timezone_identifiers_list function.
179251 *
180252 * @see http://php.net/manual/en/datetimezone.listidentifiers.php
253+ *
254+ * @param string|null $str
181255 */
182- public function timezone (? string $ str = null ): bool
256+ public function timezone ($ str = null ): bool
183257 {
184- return in_array ($ str ?? '' , timezone_identifiers_list (), true );
258+ if (! is_string ($ str )) {
259+ $ str = (string ) $ str ;
260+ }
261+
262+ return in_array ($ str , timezone_identifiers_list (), true );
185263 }
186264
187265 /**
188266 * Valid Base64
189267 *
190268 * Tests a string for characters outside of the Base64 alphabet
191269 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
270+ *
271+ * @param string|null $str
192272 */
193- public function valid_base64 (? string $ str = null ): bool
273+ public function valid_base64 ($ str = null ): bool
194274 {
195275 if ($ str === null ) {
196276 return false ;
197277 }
198278
279+ if (! is_string ($ str )) {
280+ $ str = (string ) $ str ;
281+ }
282+
199283 return base64_encode (base64_decode ($ str , true )) === $ str ;
200284 }
201285
202286 /**
203287 * Valid JSON
288+ *
289+ * @param string|null $str
204290 */
205- public function valid_json (? string $ str = null ): bool
291+ public function valid_json ($ str = null ): bool
206292 {
207- json_decode ($ str ?? '' );
293+ if (! is_string ($ str )) {
294+ $ str = (string ) $ str ;
295+ }
296+
297+ json_decode ($ str );
208298
209299 return json_last_error () === JSON_ERROR_NONE ;
210300 }
211301
212302 /**
213303 * Checks for a correctly formatted email address
304+ *
305+ * @param string|null $str
214306 */
215- public function valid_email (? string $ str = null ): bool
307+ public function valid_email ($ str = null ): bool
216308 {
309+ if (! is_string ($ str )) {
310+ $ str = (string ) $ str ;
311+ }
312+
217313 // @see https://regex101.com/r/wlJG1t/1/
218- if (function_exists ('idn_to_ascii ' ) && defined ('INTL_IDNA_VARIANT_UTS46 ' ) && preg_match ('#\A([^@]+)@(.+)\z# ' , $ str ?? '' , $ matches )) {
314+ if (function_exists ('idn_to_ascii ' ) && defined ('INTL_IDNA_VARIANT_UTS46 ' ) && preg_match ('#\A([^@]+)@(.+)\z# ' , $ str , $ matches )) {
219315 $ str = $ matches [1 ] . '@ ' . idn_to_ascii ($ matches [2 ], 0 , INTL_IDNA_VARIANT_UTS46 );
220316 }
221317
@@ -227,10 +323,16 @@ public function valid_email(?string $str = null): bool
227323 *
228324 * Example:
229325326+ *
327+ * @param string|null $str
230328 */
231- public function valid_emails (? string $ str = null ): bool
329+ public function valid_emails ($ str = null ): bool
232330 {
233- foreach (explode (', ' , $ str ?? '' ) as $ email ) {
331+ if (! is_string ($ str )) {
332+ $ str = (string ) $ str ;
333+ }
334+
335+ foreach (explode (', ' , $ str ) as $ email ) {
234336 $ email = trim ($ email );
235337
236338 if ($ email === '' ) {
@@ -248,11 +350,16 @@ public function valid_emails(?string $str = null): bool
248350 /**
249351 * Validate an IP address (human readable format or binary string - inet_pton)
250352 *
353+ * @param string|null $ip
251354 * @param string|null $which IP protocol: 'ipv4' or 'ipv6'
252355 */
253- public function valid_ip (? string $ ip = null , ?string $ which = null ): bool
356+ public function valid_ip ($ ip = null , ?string $ which = null ): bool
254357 {
255- if (empty ($ ip )) {
358+ if (! is_string ($ ip )) {
359+ $ ip = (string ) $ ip ;
360+ }
361+
362+ if ($ ip === '' ) {
256363 return false ;
257364 }
258365
@@ -278,13 +385,19 @@ public function valid_ip(?string $ip = null, ?string $which = null): bool
278385 *
279386 * Warning: this rule will pass basic strings like
280387 * "banana"; use valid_url_strict for a stricter rule.
388+ *
389+ * @param string|null $str
281390 */
282- public function valid_url (? string $ str = null ): bool
391+ public function valid_url ($ str = null ): bool
283392 {
284393 if (empty ($ str )) {
285394 return false ;
286395 }
287396
397+ if (! is_string ($ str )) {
398+ $ str = (string ) $ str ;
399+ }
400+
288401 if (preg_match ('/\A(?:([^:]*)\:)?\/\/(.+)\z/ ' , $ str , $ matches )) {
289402 if (! in_array ($ matches [1 ], ['http ' , 'https ' ], true )) {
290403 return false ;
@@ -301,14 +414,19 @@ public function valid_url(?string $str = null): bool
301414 /**
302415 * Checks a URL to ensure it's formed correctly.
303416 *
417+ * @param string|null $str
304418 * @param string|null $validSchemes comma separated list of allowed schemes
305419 */
306- public function valid_url_strict (? string $ str = null , ?string $ validSchemes = null ): bool
420+ public function valid_url_strict ($ str = null , ?string $ validSchemes = null ): bool
307421 {
308422 if (empty ($ str )) {
309423 return false ;
310424 }
311425
426+ if (! is_string ($ str )) {
427+ $ str = (string ) $ str ;
428+ }
429+
312430 // parse_url() may return null and false
313431 $ scheme = strtolower ((string ) parse_url ($ str , PHP_URL_SCHEME ));
314432 $ validSchemes = explode (
@@ -322,10 +440,16 @@ public function valid_url_strict(?string $str = null, ?string $validSchemes = nu
322440
323441 /**
324442 * Checks for a valid date and matches a given date format
443+ *
444+ * @param string|null $str
325445 */
326- public function valid_date (? string $ str = null , ?string $ format = null ): bool
446+ public function valid_date ($ str = null , ?string $ format = null ): bool
327447 {
328- if ($ str === null ) {
448+ if (! is_string ($ str )) {
449+ $ str = (string ) $ str ;
450+ }
451+
452+ if ($ str === '' ) {
329453 return false ;
330454 }
331455
0 commit comments