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