• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 7293561159

21 Dec 2023 09:55PM UTC coverage: 85.237% (+0.004%) from 85.233%
7293561159

push

github

web-flow
Merge pull request #8355 from paulbalandan/replace

Add `replace` to composer.json

18597 of 21818 relevant lines covered (85.24%)

199.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.72
/system/Validation/FormatRules.php
1
<?php
2

3
/**
4
 * This file is part of CodeIgniter 4 framework.
5
 *
6
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11

12
namespace CodeIgniter\Validation;
13

14
use DateTime;
15

16
/**
17
 * Format validation Rules.
18
 *
19
 * @see \CodeIgniter\Validation\FormatRulesTest
20
 */
21
class FormatRules
22
{
23
    /**
24
     * Alpha
25
     */
26
    public function alpha(?string $str = null): bool
27
    {
28
        return ctype_alpha($str ?? '');
17✔
29
    }
30

31
    /**
32
     * Alpha with spaces.
33
     *
34
     * @param string|null $value Value.
35
     *
36
     * @return bool True if alpha with spaces, else false.
37
     */
38
    public function alpha_space(?string $value = null): bool
39
    {
40
        if ($value === null) {
13✔
41
            return true;
1✔
42
        }
43

44
        // @see https://regex101.com/r/LhqHPO/1
45
        return (bool) preg_match('/\A[A-Z ]+\z/i', $value);
12✔
46
    }
47

48
    /**
49
     * Alphanumeric with underscores and dashes
50
     *
51
     * @see https://regex101.com/r/XfVY3d/1
52
     */
53
    public function alpha_dash(?string $str = null): bool
54
    {
55
        if ($str === null) {
7✔
56
            return false;
1✔
57
        }
58

59
        return preg_match('/\A[a-z0-9_-]+\z/i', $str) === 1;
6✔
60
    }
61

62
    /**
63
     * Alphanumeric, spaces, and a limited set of punctuation characters.
64
     * Accepted punctuation characters are: ~ tilde, ! exclamation,
65
     * # number, $ dollar, % percent, & ampersand, * asterisk, - dash,
66
     * _ underscore, + plus, = equals, | vertical bar, : colon, . period
67
     * ~ ! # $ % & * - _ + = | : .
68
     *
69
     * @param string|null $str
70
     *
71
     * @return bool
72
     *
73
     * @see https://regex101.com/r/6N8dDY/1
74
     */
75
    public function alpha_numeric_punct($str)
76
    {
77
        if ($str === null) {
35✔
78
            return false;
1✔
79
        }
80

81
        return preg_match('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i', $str) === 1;
34✔
82
    }
83

84
    /**
85
     * Alphanumeric
86
     */
87
    public function alpha_numeric(?string $str = null): bool
88
    {
89
        return ctype_alnum($str ?? '');
9✔
90
    }
91

92
    /**
93
     * Alphanumeric w/ spaces
94
     */
95
    public function alpha_numeric_space(?string $str = null): bool
96
    {
97
        // @see https://regex101.com/r/0AZDME/1
98
        return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str ?? '');
7✔
99
    }
100

101
    /**
102
     * Any type of string
103
     *
104
     * Note: we specifically do NOT type hint $str here so that
105
     * it doesn't convert numbers into strings.
106
     *
107
     * @param string|null $str
108
     */
109
    public function string($str = null): bool
110
    {
111
        return is_string($str);
6✔
112
    }
113

114
    /**
115
     * Decimal number
116
     */
117
    public function decimal(?string $str = null): bool
118
    {
119
        // @see https://regex101.com/r/HULifl/2/
120
        return (bool) preg_match('/\A[-+]?\d{0,}\.?\d+\z/', $str ?? '');
19✔
121
    }
122

123
    /**
124
     * String of hexidecimal characters
125
     */
126
    public function hex(?string $str = null): bool
127
    {
128
        return ctype_xdigit($str ?? '');
7✔
129
    }
130

131
    /**
132
     * Integer
133
     */
134
    public function integer(?string $str = null): bool
135
    {
136
        return (bool) preg_match('/\A[\-+]?\d+\z/', $str ?? '');
18✔
137
    }
138

139
    /**
140
     * Is a Natural number  (0,1,2,3, etc.)
141
     */
142
    public function is_natural(?string $str = null): bool
143
    {
144
        return ctype_digit($str ?? '');
9✔
145
    }
146

147
    /**
148
     * Is a Natural number, but not a zero  (1,2,3, etc.)
149
     */
150
    public function is_natural_no_zero(?string $str = null): bool
151
    {
152
        return $str !== '0' && ctype_digit($str ?? '');
39✔
153
    }
154

155
    /**
156
     * Numeric
157
     */
158
    public function numeric(?string $str = null): bool
159
    {
160
        // @see https://regex101.com/r/bb9wtr/2
161
        return (bool) preg_match('/\A[\-+]?\d*\.?\d+\z/', $str ?? '');
34✔
162
    }
163

164
    /**
165
     * Compares value against a regular expression pattern.
166
     */
167
    public function regex_match(?string $str, string $pattern): bool
168
    {
169
        if (strpos($pattern, '/') !== 0) {
8✔
170
            $pattern = "/{$pattern}/";
2✔
171
        }
172

173
        return (bool) preg_match($pattern, $str ?? '');
8✔
174
    }
175

176
    /**
177
     * Validates that the string is a valid timezone as per the
178
     * timezone_identifiers_list function.
179
     *
180
     * @see http://php.net/manual/en/datetimezone.listidentifiers.php
181
     */
182
    public function timezone(?string $str = null): bool
183
    {
184
        return in_array($str ?? '', timezone_identifiers_list(), true);
7✔
185
    }
186

187
    /**
188
     * Valid Base64
189
     *
190
     * Tests a string for characters outside of the Base64 alphabet
191
     * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
192
     */
193
    public function valid_base64(?string $str = null): bool
194
    {
195
        if ($str === null) {
5✔
196
            return false;
1✔
197
        }
198

199
        return base64_encode(base64_decode($str, true)) === $str;
4✔
200
    }
201

202
    /**
203
     * Valid JSON
204
     */
205
    public function valid_json(?string $str = null): bool
206
    {
207
        json_decode($str ?? '');
17✔
208

209
        return json_last_error() === JSON_ERROR_NONE;
17✔
210
    }
211

212
    /**
213
     * Checks for a correctly formatted email address
214
     */
215
    public function valid_email(?string $str = null): bool
216
    {
217
        // @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)) {
29✔
219
            $str = $matches[1] . '@' . idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46);
10✔
220
        }
221

222
        return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
29✔
223
    }
224

225
    /**
226
     * Validate a comma-separated list of email addresses.
227
     *
228
     * Example:
229
     *     valid_emails[one@example.com,two@example.com]
230
     */
231
    public function valid_emails(?string $str = null): bool
232
    {
233
        foreach (explode(',', $str ?? '') as $email) {
14✔
234
            $email = trim($email);
14✔
235

236
            if ($email === '') {
14✔
237
                return false;
3✔
238
            }
239

240
            if ($this->valid_email($email) === false) {
11✔
241
                return false;
5✔
242
            }
243
        }
244

245
        return true;
6✔
246
    }
247

248
    /**
249
     * Validate an IP address (human readable format or binary string - inet_pton)
250
     *
251
     * @param string|null $which IP protocol: 'ipv4' or 'ipv6'
252
     */
253
    public function valid_ip(?string $ip = null, ?string $which = null): bool
254
    {
255
        if ($ip === null || $ip === '') {
53✔
256
            return false;
3✔
257
        }
258

259
        switch (strtolower($which ?? '')) {
50✔
260
            case 'ipv4':
50✔
261
                $option = FILTER_FLAG_IPV4;
8✔
262
                break;
8✔
263

264
            case 'ipv6':
42✔
265
                $option = FILTER_FLAG_IPV6;
11✔
266
                break;
11✔
267

268
            default:
269
                $option = 0;
34✔
270
        }
271

272
        return filter_var($ip, FILTER_VALIDATE_IP, $option) !== false
50✔
273
            || (! ctype_print($ip) && filter_var(inet_ntop($ip), FILTER_VALIDATE_IP, $option) !== false);
50✔
274
    }
275

276
    /**
277
     * Checks a string to ensure it is (loosely) a URL.
278
     *
279
     * Warning: this rule will pass basic strings like
280
     * "banana"; use valid_url_strict for a stricter rule.
281
     */
282
    public function valid_url(?string $str = null): bool
283
    {
284
        if ($str === null || $str === '') {
35✔
285
            return false;
3✔
286
        }
287

288
        if (preg_match('/\A(?:([^:]*)\:)?\/\/(.+)\z/', $str, $matches)) {
32✔
289
            if (! in_array($matches[1], ['http', 'https'], true)) {
14✔
290
                return false;
4✔
291
            }
292

293
            $str = $matches[2];
10✔
294
        }
295

296
        $str = 'http://' . $str;
28✔
297

298
        return filter_var($str, FILTER_VALIDATE_URL) !== false;
28✔
299
    }
300

301
    /**
302
     * Checks a URL to ensure it's formed correctly.
303
     *
304
     * @param string|null $validSchemes comma separated list of allowed schemes
305
     */
306
    public function valid_url_strict(?string $str = null, ?string $validSchemes = null): bool
307
    {
308
        if ($str === null || $str === '' || $str === '0') {
37✔
309
            return false;
3✔
310
        }
311

312
        // parse_url() may return null and false
313
        $scheme       = strtolower((string) parse_url($str, PHP_URL_SCHEME));
34✔
314
        $validSchemes = explode(
34✔
315
            ',',
34✔
316
            strtolower($validSchemes ?? 'http,https')
34✔
317
        );
34✔
318

319
        return in_array($scheme, $validSchemes, true)
34✔
320
            && filter_var($str, FILTER_VALIDATE_URL) !== false;
34✔
321
    }
322

323
    /**
324
     * Checks for a valid date and matches a given date format
325
     *
326
     * @param non-empty-string|null $format
327
     */
328
    public function valid_date(?string $str = null, ?string $format = null): bool
329
    {
330
        if ($str === null) {
71✔
331
            return false;
1✔
332
        }
333

334
        if ($format === null || $format === '') {
70✔
335
            return strtotime($str) !== false;
16✔
336
        }
337

338
        $date   = DateTime::createFromFormat($format, $str);
54✔
339
        $errors = DateTime::getLastErrors();
54✔
340

341
        if ($date === false) {
54✔
342
            return false;
18✔
343
        }
344

345
        // PHP 8.2 or later.
346
        if ($errors === false) {
36✔
347
            return true;
×
348
        }
349

350
        return $errors['warning_count'] === 0 && $errors['error_count'] === 0;
36✔
351
    }
352
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc