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