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