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