|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FRZB\Component\RequestMapper\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use Fp\Collections\ArrayList; |
|
8
|
|
|
use JetBrains\PhpStorm\Immutable; |
|
9
|
|
|
|
|
10
|
|
|
/** @internal */ |
|
11
|
|
|
#[Immutable] |
|
12
|
|
|
final class ClassHelper |
|
13
|
|
|
{ |
|
14
|
|
|
private function __construct() |
|
15
|
|
|
{ |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
23 |
|
public static function isNotBuiltinAndExists(string $className): bool |
|
19
|
|
|
{ |
|
20
|
23 |
|
return (class_exists($className) || interface_exists($className)) |
|
21
|
23 |
|
&& !empty((new \ReflectionClass($className))->getNamespaceName()) |
|
22
|
23 |
|
&& !self::isEnum($className) |
|
23
|
|
|
; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
23 |
|
public static function isEnum(string $className): bool |
|
27
|
|
|
{ |
|
28
|
23 |
|
return enum_exists($className); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
17 |
|
public static function getShortName(string $className): string |
|
32
|
|
|
{ |
|
33
|
|
|
try { |
|
34
|
17 |
|
return (new \ReflectionClass($className))->getShortName(); |
|
35
|
5 |
|
} catch (\ReflectionException) { |
|
36
|
5 |
|
return $className; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
11 |
|
public static function isNameContains(string $className, string ...$haystack): bool |
|
41
|
|
|
{ |
|
42
|
11 |
|
return ArrayList::collect($haystack) |
|
43
|
11 |
|
->filter(static fn (string $value): bool => StringHelper::contains(self::getShortName($className), $value)) |
|
44
|
11 |
|
->isNonEmpty() |
|
45
|
|
|
; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @return \ReflectionParameter[] */ |
|
49
|
6 |
|
public static function getMethodParameters(string $className, string $classMethod): array |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
6 |
|
return (new \ReflectionMethod($className, $classMethod))->getParameters(); |
|
53
|
1 |
|
} catch (\ReflectionException) { |
|
54
|
1 |
|
return []; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
9 |
|
public static function isArrayHasAllPropertiesFromClass(array $array, string $class): bool |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
9 |
|
$rClass = new \ReflectionClass($class); |
|
62
|
1 |
|
} catch (\ReflectionException) { |
|
63
|
1 |
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
8 |
|
foreach ($rClass->getProperties() as $property) { |
|
67
|
8 |
|
$propertyValue = $array[$property->getName()] ?? $array[StringHelper::toSnakeCase($property->getName())] ?? null; |
|
68
|
|
|
|
|
69
|
8 |
|
if (!$propertyValue) { |
|
70
|
2 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
6 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @template T |
|
79
|
|
|
* |
|
80
|
|
|
* @param class-string<T> $attributeClass |
|
|
|
|
|
|
81
|
|
|
* |
|
82
|
|
|
* @return null|T |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function getAttribute(string|object $target, string $attributeClass): ?object |
|
85
|
|
|
{ |
|
86
|
|
|
return ArrayList::collect(self::getAttributes($target, $attributeClass)) |
|
87
|
|
|
->firstElement() |
|
88
|
|
|
->get() |
|
89
|
|
|
; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @template T |
|
94
|
|
|
* |
|
95
|
|
|
* @param class-string<T> $attributeClass |
|
|
|
|
|
|
96
|
|
|
* |
|
97
|
|
|
* @return array<T> |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function getAttributes(string|object $target, string $attributeClass): array |
|
100
|
|
|
{ |
|
101
|
|
|
try { |
|
102
|
|
|
$attributes = (new \ReflectionClass($target))->getAttributes($attributeClass); |
|
103
|
|
|
} catch (\ReflectionException) { |
|
104
|
|
|
$attributes = []; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return ArrayList::collect($attributes) |
|
108
|
|
|
->map(static fn (\ReflectionAttribute $a) => $a->newInstance()) |
|
109
|
|
|
->toArray() |
|
110
|
|
|
; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|