1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Utility; |
4: | |
5: | use Stringable; |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | final class Test extends AbstractUtility |
13: | { |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | public static function isBoolean($value): bool |
31: | { |
32: | return is_bool($value) || ( |
33: | is_string($value) |
34: | && Regex::match('/^' . Regex::BOOLEAN_STRING . '$/', $value) |
35: | ); |
36: | } |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | public static function isInteger($value): bool |
45: | { |
46: | return is_int($value) || ( |
47: | is_string($value) |
48: | && Regex::match('/^' . Regex::INTEGER_STRING . '$/', $value) |
49: | ); |
50: | } |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | public static function isFloat($value): bool |
61: | { |
62: | return is_float($value) || ( |
63: | is_string($value) |
64: | && is_numeric(trim($value)) |
65: | && !Regex::match('/^' . Regex::INTEGER_STRING . '$/', $value) |
66: | ); |
67: | } |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | public static function isNumericKey($value): bool |
77: | { |
78: | return is_int($value) |
79: | || is_float($value) |
80: | || is_bool($value) |
81: | || (is_string($value) && Regex::match('/^(-?[1-9][0-9]*|0)$/D', $value)); |
82: | } |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | public static function isDateString($value): bool |
91: | { |
92: | return is_string($value) && strtotime($value) !== false; |
93: | } |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | public static function isStringable($value): bool |
102: | { |
103: | return is_string($value) |
104: | || $value instanceof Stringable |
105: | || (is_object($value) && method_exists($value, '__toString')); |
106: | } |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | public static function isBetween($value, $min, $max): bool |
116: | { |
117: | return $value >= $min && $value <= $max; |
118: | } |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | public static function isBuiltinType(string $value): bool |
126: | { |
127: | return in_array(Str::lower($value), [ |
128: | 'array', |
129: | 'bool', |
130: | 'callable', |
131: | 'enum', |
132: | 'false', |
133: | 'float', |
134: | 'int', |
135: | 'iterable', |
136: | 'mixed', |
137: | 'never', |
138: | 'null', |
139: | 'numeric', |
140: | 'object', |
141: | 'parent', |
142: | 'resource', |
143: | 'self', |
144: | 'static', |
145: | 'string', |
146: | 'true', |
147: | 'void', |
148: | ], true); |
149: | } |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | public static function isFqcn($value): bool |
158: | { |
159: | return is_string($value) |
160: | && Regex::match('/^' . Regex::PHP_TYPE . '$/D', $value); |
161: | } |
162: | } |
163: | |