ValidationUtils::isURLMagnet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rico\Slib;
6
7
abstract class ValidationUtils
8
{
9
    /**
10
     * Checks that $mixed value is an email.
11
     *
12
     * @param mixed $mixed
13
     *
14
     * @return bool
15
     */
16
    public static function isEmail($mixed): bool
17
    {
18
        return
19
            is_string($mixed) &&
20
            // Use of preg_replace instead of preg_match, as preg_match acts unexpectedly with this regular expression
21
            preg_replace('#[a-zA-Z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+(?:\.[a-zA-Z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+)*\@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+(?:[a-z]{2}|aero|asia|biz|cat|com|coop|info|int|jobs|mobi|museum|name|net|org|pro|tel|travel|xxx|edu|gov|mil)$#', 'ok', $mixed) === 'ok'
22
        ;
23
    }
24
25
    /**
26
     * Checks that $mixed value is a hexadecimal value.
27
     *
28
     * @param mixed $mixed
29
     *
30
     * @return bool
31
     */
32
    public static function isHexadecimal($mixed)
33
    {
34
        return
35
            is_string($mixed) &&
36
            preg_match('/^\b[0-9A-F]{6}\b$/i', $mixed)
37
        ;
38
    }
39
40
    /**
41
     * Checks that $mixed value is an IP (v4 or v6).
42
     *
43
     * @param mixed $mixed
44
     *
45
     * @return bool
46
     */
47
    public static function isIp($mixed): bool
48
    {
49
        return
50
            is_string($mixed) &&
51
            (
52
                // IPv4 check
53
                preg_match('#^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|0?[0-9]?[1-9]|0?[1-9][0-9])\.)((25[0-5]|(2[0-4]|(1|0)?[0-9])?[0-9])\.){2}(25[0-5]|(2[0-4]|(1|0)?[0-9])?[0-9])$#', $mixed) ||
54
                // IPv6 check
55
                preg_match('#^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$#', $mixed)
56
            )
57
        ;
58
    }
59
60
    /**
61
     * Checks that $mixed value is a decimal number (float or integer).
62
     *
63
     * @param mixed $mixed
64
     *
65
     * @return bool
66
     */
67
    public static function isNumber($mixed): bool
68
    {
69
        if (!is_numeric($mixed)) {
70
            return false;
71
        }
72
73
        return (bool) preg_match('/^\-?[0-9]+\.?[0-9]*$/', (string) $mixed);
74
    }
75
76
    /**
77
     * Checks that $string value is a phone number.
78
     *
79
     * @param string $string
80
     *
81
     * @return bool
82
     */
83
    public static function isPhoneNumber(string $string): bool
84
    {
85
        // Sanitize string before the test itself
86
        $string = preg_replace('/(\(([0-9]+)\))+/', '$2', $string);
87
        $string = preg_replace('/([0-9]+)([ \\\–\-\.\/]{1})/', '$1$3', $string);
88
89
        return (bool) preg_match('/^(\(?\+\ ?[0-9]{1,4}\)?)?\ ?[0-9]{7,15}$/', $string);
90
    }
91
92
    /**
93
     * Checks that $mixed value is a positive integer (primary key).
94
     *
95
     * @param mixed $mixed
96
     *
97
     * @return bool
98
     */
99
    public static function isPositiveInt($mixed): bool
100
    {
101
        if (!isset($mixed) || is_bool($mixed)) {
102
            return false;
103
        }
104
105
        return filter_var($mixed, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]) ? true : false;
106
    }
107
108
    /**
109
     * Checks that $mixed value is an URL.
110
     *
111
     * @param mixed $mixed
112
     *
113
     * @return bool
114
     */
115
    public static function isURL($mixed): bool
116
    {
117
        return
118
            is_string($mixed) &&
119
            preg_match('_^(https?|ftp)://(\S+(:\S*)?@)?(([1-9]|[1-9]\d|1\d\d|2[0-1]\d|22[0-3])(\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])){2}(\.([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-4]))|(([a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(\.([a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(\.([a-z\x{00a1}-\x{ffff}]{2,})))(:\d{2,5})?(/[^\s]*)?$_iuS', $mixed)
120
        ;
121
    }
122
123
    /**
124
     * Checks that $mixed value is magnet URL.
125
     *
126
     * @param mixed $mixed
127
     *
128
     * @return bool
129
     */
130
    public static function isURLMagnet($mixed): bool
131
    {
132
        return
133
            is_string($mixed) &&
134
            preg_match('#^magnet:\?xt=urn:[a-z0-9][a-z0-9-]{0,31}:[a-z0-9]{32,40}#i', $mixed)
135
        ;
136
    }
137
}
138