@@ -22,10 +22,16 @@ class FormatRules
22
22
{
23
23
/**
24
24
* Alpha
25
+ *
26
+ * @param string|null $str
25
27
*/
26
- public function alpha (? string $ str = null ): bool
28
+ public function alpha ($ str = null ): bool
27
29
{
28
- return ctype_alpha ($ str ?? '' );
30
+ if (! is_string ($ str )) {
31
+ $ str = (string ) $ str ;
32
+ }
33
+
34
+ return ctype_alpha ($ str );
29
35
}
30
36
31
37
/**
@@ -35,12 +41,16 @@ public function alpha(?string $str = null): bool
35
41
*
36
42
* @return bool True if alpha with spaces, else false.
37
43
*/
38
- public function alpha_space (? string $ value = null ): bool
44
+ public function alpha_space ($ value = null ): bool
39
45
{
40
46
if ($ value === null ) {
41
47
return true ;
42
48
}
43
49
50
+ if (! is_string ($ value )) {
51
+ $ value = (string ) $ value ;
52
+ }
53
+
44
54
// @see https://regex101.com/r/LhqHPO/1
45
55
return (bool ) preg_match ('/\A[A-Z ]+\z/i ' , $ value );
46
56
}
@@ -49,13 +59,19 @@ public function alpha_space(?string $value = null): bool
49
59
* Alphanumeric with underscores and dashes
50
60
*
51
61
* @see https://regex101.com/r/XfVY3d/1
62
+ *
63
+ * @param string|null $str
52
64
*/
53
- public function alpha_dash (? string $ str = null ): bool
65
+ public function alpha_dash ($ str = null ): bool
54
66
{
55
67
if ($ str === null ) {
56
68
return false ;
57
69
}
58
70
71
+ if (! is_string ($ str )) {
72
+ $ str = (string ) $ str ;
73
+ }
74
+
59
75
return preg_match ('/\A[a-z0-9_-]+\z/i ' , $ str ) === 1 ;
60
76
}
61
77
@@ -78,24 +94,40 @@ public function alpha_numeric_punct($str)
78
94
return false ;
79
95
}
80
96
97
+ if (! is_string ($ str )) {
98
+ $ str = (string ) $ str ;
99
+ }
100
+
81
101
return preg_match ('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i ' , $ str ) === 1 ;
82
102
}
83
103
84
104
/**
85
105
* Alphanumeric
106
+ *
107
+ * @param string|null $str
86
108
*/
87
- public function alpha_numeric (? string $ str = null ): bool
109
+ public function alpha_numeric ($ str = null ): bool
88
110
{
89
- return ctype_alnum ($ str ?? '' );
111
+ if (! is_string ($ str )) {
112
+ $ str = (string ) $ str ;
113
+ }
114
+
115
+ return ctype_alnum ($ str );
90
116
}
91
117
92
118
/**
93
119
* Alphanumeric w/ spaces
120
+ *
121
+ * @param string|null $str
94
122
*/
95
- public function alpha_numeric_space (? string $ str = null ): bool
123
+ public function alpha_numeric_space ($ str = null ): bool
96
124
{
125
+ if (! is_string ($ str )) {
126
+ $ str = (string ) $ str ;
127
+ }
128
+
97
129
// @see https://regex101.com/r/0AZDME/1
98
- return (bool ) preg_match ('/\A[A-Z0-9 ]+\z/i ' , $ str ?? '' );
130
+ return (bool ) preg_match ('/\A[A-Z0-9 ]+\z/i ' , $ str );
99
131
}
100
132
101
133
/**
@@ -113,109 +145,175 @@ public function string($str = null): bool
113
145
114
146
/**
115
147
* Decimal number
148
+ *
149
+ * @param string|null $str
116
150
*/
117
- public function decimal (? string $ str = null ): bool
151
+ public function decimal ($ str = null ): bool
118
152
{
153
+ if (! is_string ($ str )) {
154
+ $ str = (string ) $ str ;
155
+ }
156
+
119
157
// @see https://regex101.com/r/HULifl/2/
120
- return (bool ) preg_match ('/\A[-+]?\d{0,}\.?\d+\z/ ' , $ str ?? '' );
158
+ return (bool ) preg_match ('/\A[-+]?\d{0,}\.?\d+\z/ ' , $ str );
121
159
}
122
160
123
161
/**
124
162
* String of hexidecimal characters
163
+ *
164
+ * @param string|null $str
125
165
*/
126
- public function hex (? string $ str = null ): bool
166
+ public function hex ($ str = null ): bool
127
167
{
128
- return ctype_xdigit ($ str ?? '' );
168
+ if (! is_string ($ str )) {
169
+ $ str = (string ) $ str ;
170
+ }
171
+
172
+ return ctype_xdigit ($ str );
129
173
}
130
174
131
175
/**
132
176
* Integer
177
+ *
178
+ * @param string|null $str
133
179
*/
134
- public function integer (? string $ str = null ): bool
180
+ public function integer ($ str = null ): bool
135
181
{
136
- return (bool ) preg_match ('/\A[\-+]?\d+\z/ ' , $ str ?? '' );
182
+ if (! is_string ($ str )) {
183
+ $ str = (string ) $ str ;
184
+ }
185
+
186
+ return (bool ) preg_match ('/\A[\-+]?\d+\z/ ' , $ str );
137
187
}
138
188
139
189
/**
140
190
* Is a Natural number (0,1,2,3, etc.)
191
+ *
192
+ * @param string|null $str
141
193
*/
142
- public function is_natural (? string $ str = null ): bool
194
+ public function is_natural ($ str = null ): bool
143
195
{
144
- return ctype_digit ($ str ?? '' );
196
+ if (! is_string ($ str )) {
197
+ $ str = (string ) $ str ;
198
+ }
199
+
200
+ return ctype_digit ($ str );
145
201
}
146
202
147
203
/**
148
204
* Is a Natural number, but not a zero (1,2,3, etc.)
205
+ *
206
+ * @param string|null $str
149
207
*/
150
- public function is_natural_no_zero (? string $ str = null ): bool
208
+ public function is_natural_no_zero ($ str = null ): bool
151
209
{
152
- return $ str !== '0 ' && ctype_digit ($ str ?? '' );
210
+ if (! is_string ($ str )) {
211
+ $ str = (string ) $ str ;
212
+ }
213
+
214
+ return $ str !== '0 ' && ctype_digit ($ str );
153
215
}
154
216
155
217
/**
156
218
* Numeric
219
+ *
220
+ * @param string|null $str
157
221
*/
158
- public function numeric (? string $ str = null ): bool
222
+ public function numeric ($ str = null ): bool
159
223
{
224
+ if (! is_string ($ str )) {
225
+ $ str = (string ) $ str ;
226
+ }
227
+
160
228
// @see https://regex101.com/r/bb9wtr/2
161
- return (bool ) preg_match ('/\A[\-+]?\d*\.?\d+\z/ ' , $ str ?? '' );
229
+ return (bool ) preg_match ('/\A[\-+]?\d*\.?\d+\z/ ' , $ str );
162
230
}
163
231
164
232
/**
165
233
* Compares value against a regular expression pattern.
234
+ *
235
+ * @param string|null $str
166
236
*/
167
- public function regex_match (? string $ str , string $ pattern ): bool
237
+ public function regex_match ($ str , string $ pattern ): bool
168
238
{
239
+ if (! is_string ($ str )) {
240
+ $ str = (string ) $ str ;
241
+ }
242
+
169
243
if (strpos ($ pattern , '/ ' ) !== 0 ) {
170
244
$ pattern = "/ {$ pattern }/ " ;
171
245
}
172
246
173
- return (bool ) preg_match ($ pattern , $ str ?? '' );
247
+ return (bool ) preg_match ($ pattern , $ str );
174
248
}
175
249
176
250
/**
177
251
* Validates that the string is a valid timezone as per the
178
252
* timezone_identifiers_list function.
179
253
*
180
254
* @see http://php.net/manual/en/datetimezone.listidentifiers.php
255
+ *
256
+ * @param string|null $str
181
257
*/
182
- public function timezone (? string $ str = null ): bool
258
+ public function timezone ($ str = null ): bool
183
259
{
184
- return in_array ($ str ?? '' , timezone_identifiers_list (), true );
260
+ if (! is_string ($ str )) {
261
+ $ str = (string ) $ str ;
262
+ }
263
+
264
+ return in_array ($ str , timezone_identifiers_list (), true );
185
265
}
186
266
187
267
/**
188
268
* Valid Base64
189
269
*
190
270
* Tests a string for characters outside of the Base64 alphabet
191
271
* as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
272
+ *
273
+ * @param string|null $str
192
274
*/
193
- public function valid_base64 (? string $ str = null ): bool
275
+ public function valid_base64 ($ str = null ): bool
194
276
{
195
277
if ($ str === null ) {
196
278
return false ;
197
279
}
198
280
281
+ if (! is_string ($ str )) {
282
+ $ str = (string ) $ str ;
283
+ }
284
+
199
285
return base64_encode (base64_decode ($ str , true )) === $ str ;
200
286
}
201
287
202
288
/**
203
289
* Valid JSON
290
+ *
291
+ * @param string|null $str
204
292
*/
205
- public function valid_json (? string $ str = null ): bool
293
+ public function valid_json ($ str = null ): bool
206
294
{
207
- json_decode ($ str ?? '' );
295
+ if (! is_string ($ str )) {
296
+ $ str = (string ) $ str ;
297
+ }
298
+
299
+ json_decode ($ str );
208
300
209
301
return json_last_error () === JSON_ERROR_NONE ;
210
302
}
211
303
212
304
/**
213
305
* Checks for a correctly formatted email address
306
+ *
307
+ * @param string|null $str
214
308
*/
215
- public function valid_email (? string $ str = null ): bool
309
+ public function valid_email ($ str = null ): bool
216
310
{
311
+ if (! is_string ($ str )) {
312
+ $ str = (string ) $ str ;
313
+ }
314
+
217
315
// @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 )) {
316
+ if (function_exists ('idn_to_ascii ' ) && defined ('INTL_IDNA_VARIANT_UTS46 ' ) && preg_match ('#\A([^@]+)@(.+)\z# ' , $ str , $ matches )) {
219
317
$ str = $ matches [1 ] . '@ ' . idn_to_ascii ($ matches [2 ], 0 , INTL_IDNA_VARIANT_UTS46 );
220
318
}
221
319
@@ -227,10 +325,16 @@ public function valid_email(?string $str = null): bool
227
325
*
228
326
* Example:
229
327
328
+ *
329
+ * @param string|null $str
230
330
*/
231
- public function valid_emails (? string $ str = null ): bool
331
+ public function valid_emails ($ str = null ): bool
232
332
{
233
- foreach (explode (', ' , $ str ?? '' ) as $ email ) {
333
+ if (! is_string ($ str )) {
334
+ $ str = (string ) $ str ;
335
+ }
336
+
337
+ foreach (explode (', ' , $ str ) as $ email ) {
234
338
$ email = trim ($ email );
235
339
236
340
if ($ email === '' ) {
@@ -248,11 +352,16 @@ public function valid_emails(?string $str = null): bool
248
352
/**
249
353
* Validate an IP address (human readable format or binary string - inet_pton)
250
354
*
355
+ * @param string|null $ip
251
356
* @param string|null $which IP protocol: 'ipv4' or 'ipv6'
252
357
*/
253
- public function valid_ip (? string $ ip = null , ?string $ which = null ): bool
358
+ public function valid_ip ($ ip = null , ?string $ which = null ): bool
254
359
{
255
- if (empty ($ ip )) {
360
+ if (! is_string ($ ip )) {
361
+ $ ip = (string ) $ ip ;
362
+ }
363
+
364
+ if ($ ip === '' ) {
256
365
return false ;
257
366
}
258
367
@@ -278,13 +387,19 @@ public function valid_ip(?string $ip = null, ?string $which = null): bool
278
387
*
279
388
* Warning: this rule will pass basic strings like
280
389
* "banana"; use valid_url_strict for a stricter rule.
390
+ *
391
+ * @param string|null $str
281
392
*/
282
- public function valid_url (? string $ str = null ): bool
393
+ public function valid_url ($ str = null ): bool
283
394
{
284
395
if (empty ($ str )) {
285
396
return false ;
286
397
}
287
398
399
+ if (! is_string ($ str )) {
400
+ $ str = (string ) $ str ;
401
+ }
402
+
288
403
if (preg_match ('/\A(?:([^:]*)\:)?\/\/(.+)\z/ ' , $ str , $ matches )) {
289
404
if (! in_array ($ matches [1 ], ['http ' , 'https ' ], true )) {
290
405
return false ;
@@ -301,14 +416,19 @@ public function valid_url(?string $str = null): bool
301
416
/**
302
417
* Checks a URL to ensure it's formed correctly.
303
418
*
419
+ * @param string|null $str
304
420
* @param string|null $validSchemes comma separated list of allowed schemes
305
421
*/
306
- public function valid_url_strict (? string $ str = null , ?string $ validSchemes = null ): bool
422
+ public function valid_url_strict ($ str = null , ?string $ validSchemes = null ): bool
307
423
{
308
424
if (empty ($ str )) {
309
425
return false ;
310
426
}
311
427
428
+ if (! is_string ($ str )) {
429
+ $ str = (string ) $ str ;
430
+ }
431
+
312
432
// parse_url() may return null and false
313
433
$ scheme = strtolower ((string ) parse_url ($ str , PHP_URL_SCHEME ));
314
434
$ validSchemes = explode (
@@ -322,10 +442,16 @@ public function valid_url_strict(?string $str = null, ?string $validSchemes = nu
322
442
323
443
/**
324
444
* Checks for a valid date and matches a given date format
445
+ *
446
+ * @param string|null $str
325
447
*/
326
- public function valid_date (? string $ str = null , ?string $ format = null ): bool
448
+ public function valid_date ($ str = null , ?string $ format = null ): bool
327
449
{
328
- if ($ str === null ) {
450
+ if (! is_string ($ str )) {
451
+ $ str = (string ) $ str ;
452
+ }
453
+
454
+ if ($ str === '' ) {
329
455
return false ;
330
456
}
331
457
0 commit comments