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