UriValidator   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 20
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tleckie\Validator\Validators;
6
7
use function preg_match;
8
9
/**
10
 * Class UriValidator
11
 *
12
 * @package  Tleckie\Validator\Validators
13
 * @category UriValidator
14
 * @author   Teodoro Leckie Westberg <[email protected]>
15
 */
16
class UriValidator implements ValidatorInterface
17
{
18
    protected string $regex = '/^(https?):\/\/'.               // protocol
19
    '(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+'.         // username
20
    '(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?'.      // password
21
    '@)?(?#'.                                                  // auth requires @
22
    ')((([a-z0-9]\.|[a-z0-9][a-z0-9-]*[a-z0-9]\.)*'.           // domain segments AND
23
    '[a-z][a-z0-9-]*[a-z0-9]'.                                 // top level domain  OR
24
    '|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}'.
25
    '(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])'.                 // IP address
26
    ')(:\d+)?'.                                                // port
27
    ')(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'. // path
28
    '(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'.      // query string
29
    '?)?)?'.                                                   // path and query string optional
30
    '(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'.      // fragment
31
    '$/i';
32
33
    public function isValid(mixed $toValidate): bool
34
    {
35
        return (bool)preg_match($this->regex, (string)$toValidate);
36
    }
37
}
38