1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ddlzz\AmoAPI\Utils; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class StringUtil. |
7
|
|
|
* |
8
|
|
|
* @author ddlzz |
9
|
|
|
*/ |
10
|
|
|
class StringUtil |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $value |
14
|
|
|
* |
15
|
|
|
* @return bool |
16
|
|
|
*/ |
17
|
9 |
|
public static function isAlNum($value) |
18
|
|
|
{ |
19
|
9 |
|
return !(!self::isCorrectType($value) || !ctype_alnum((string)$value)); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $value |
24
|
|
|
* |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
8 |
|
public static function isEmail($value) |
28
|
|
|
{ |
29
|
8 |
|
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { |
30
|
6 |
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $value |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
6 |
|
public static function isDomain($value) |
42
|
|
|
{ |
43
|
6 |
|
return self::isCorrectType($value) |
44
|
3 |
|
&& (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $value) // valid chars check |
45
|
3 |
|
&& preg_match('/^.{1,253}$/', $value) // overall length check |
46
|
6 |
|
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $value)); // length of each label |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $value |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
8 |
|
public static function isOnlyLetters($value) |
55
|
|
|
{ |
56
|
8 |
|
return !(!self::isCorrectType($value) || !ctype_alpha($value)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Checks if the variable contains letters and punctuation marks only. |
61
|
|
|
* |
62
|
|
|
* @param string $value |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
5 |
|
public static function isText($value) |
67
|
|
|
{ |
68
|
5 |
|
return self::isCorrectType($value) && preg_match('/^[A-Za-z0-9\.\-\'"!,;:\?()_\/\s]+$/', $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks if the variable is a valid URL path. |
73
|
|
|
* |
74
|
|
|
* @param string $value |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
5 |
|
public static function isUrlPath($value) |
79
|
|
|
{ |
80
|
5 |
|
return self::isCorrectType($value) && preg_match('/^\/[A-Za-z0-9\/?&=,;@\[\]\.#]+$/', $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The valid file path starts with '/' or 'vfs://'. |
85
|
|
|
* |
86
|
|
|
* @param string $value |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
6 |
|
public static function isFilePath($value) |
91
|
|
|
{ |
92
|
6 |
|
return self::isCorrectType($value) && preg_match('/^(vfs:\/)?\/[A-Za-z0-9\/\._\s-]+$/', $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param mixed $value |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
39 |
|
private static function isCorrectType($value) |
101
|
|
|
{ |
102
|
39 |
|
return is_int($value) || is_string($value); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|