|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FRZB\Component\RequestMapper\Helper; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @internal |
|
9
|
|
|
*/ |
|
10
|
|
|
final class ClassHelper |
|
11
|
|
|
{ |
|
12
|
23 |
|
public static function isNotBuiltinAndExists(string $className): bool |
|
13
|
|
|
{ |
|
14
|
23 |
|
return class_exists($className) && !empty((new \ReflectionClass($className))->getNamespaceName()) && !self::isEnum($className); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
23 |
|
public static function isEnum(string $className): bool |
|
18
|
|
|
{ |
|
19
|
23 |
|
return enum_exists($className); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
17 |
|
public static function getShortName(string $className): string |
|
23
|
|
|
{ |
|
24
|
|
|
try { |
|
25
|
17 |
|
return (new \ReflectionClass($className))->getShortName(); |
|
26
|
5 |
|
} catch (\ReflectionException) { |
|
27
|
5 |
|
return $className; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
11 |
|
public static function isNameContains(string $className, string ...$haystack): bool |
|
32
|
|
|
{ |
|
33
|
11 |
|
$filter = static fn (string $value): bool => StringHelper::contains(self::getShortName($className), $value); |
|
34
|
|
|
|
|
35
|
11 |
|
return \count(array_filter($haystack, $filter)) > 0; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
public static function getPropertyMapping(string $className): array |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
4 |
|
$properties = (new \ReflectionClass($className))->getProperties(); |
|
42
|
1 |
|
} catch (\ReflectionException) { |
|
43
|
1 |
|
$properties = []; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
$map = static fn (\ReflectionProperty $p): array => match (true) { |
|
47
|
3 |
|
ConstraintsHelper::hasArrayTypeAttribute($p) => [SerializerHelper::getSerializedNameAttribute($p)->getSerializedName() => [ConstraintsHelper::getArrayTypeAttribute($p)->typeName]], |
|
48
|
3 |
|
default => [SerializerHelper::getSerializedNameAttribute($p)->getSerializedName() => $p->getType()?->/** @scrutinizer ignore-call */ getName()], |
|
49
|
|
|
}; |
|
50
|
|
|
|
|
51
|
4 |
|
return array_merge(...array_map($map, $properties)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @return \ReflectionParameter[] */ |
|
55
|
6 |
|
public static function getMethodParameters(string $className, string $classMethod): array |
|
56
|
|
|
{ |
|
57
|
|
|
try { |
|
58
|
6 |
|
return (new \ReflectionMethod($className, $classMethod))->getParameters(); |
|
59
|
1 |
|
} catch (\ReflectionException) { |
|
60
|
1 |
|
return []; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|