|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace FRZB\Component\TransactionalMessenger\Helper; |
|
17
|
|
|
|
|
18
|
|
|
use Fp\Collections\ArrayList; |
|
19
|
|
|
use JetBrains\PhpStorm\Immutable; |
|
20
|
|
|
|
|
21
|
|
|
/** @internal */ |
|
22
|
|
|
#[Immutable] |
|
23
|
|
|
final class AttributeHelper |
|
24
|
|
|
{ |
|
25
|
|
|
private function __construct() {} |
|
26
|
|
|
|
|
27
|
|
|
public static function hasAttribute(object|string $target, string $attributeClass): bool |
|
28
|
|
|
{ |
|
29
|
|
|
return !empty(self::getReflectionAttributes($target, $attributeClass)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @psalm-template TAttribute |
|
34
|
|
|
* |
|
35
|
|
|
* @psalm-param class-string<TAttribute> $attributeClass |
|
36
|
|
|
* |
|
37
|
|
|
* @psalm-return ?TAttribute |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function getAttribute(object|string $target, object|string $attributeClass): ?object |
|
40
|
|
|
{ |
|
41
|
|
|
return ArrayList::collect(self::getAttributes($target, $attributeClass)) |
|
|
|
|
|
|
42
|
|
|
->firstElement() |
|
43
|
|
|
->get() |
|
44
|
|
|
; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @psalm-template TAttribute |
|
49
|
|
|
* |
|
50
|
|
|
* @psalm-param class-string<TAttribute> $attributeClass |
|
51
|
|
|
* |
|
52
|
|
|
* @psalm-return TAttribute[] |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function getAttributes(object|string $target, string $attributeClass): array |
|
55
|
|
|
{ |
|
56
|
|
|
return ArrayList::collect(self::getReflectionAttributes($target, $attributeClass)) |
|
57
|
|
|
->map(static fn (\ReflectionAttribute $a) => $a->newInstance()) |
|
58
|
|
|
->toList() |
|
59
|
|
|
; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @psalm-template TAttribute |
|
64
|
|
|
* |
|
65
|
|
|
* @psalm-param class-string<TAttribute> $attributeClass |
|
66
|
|
|
* |
|
67
|
|
|
* @psalm-return \ReflectionAttribute<TAttribute>[] |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function getReflectionAttributes(object|string $target, string $attributeClass): array |
|
70
|
|
|
{ |
|
71
|
|
|
return ArrayList::collect(ClassHelper::getInheritanceList($target)) |
|
72
|
|
|
->map(static fn (string $class) => new \ReflectionClass($class)) |
|
73
|
|
|
->map(static fn (\ReflectionClass $rClass) => ClassHelper::getReflectionAttributes($rClass, $attributeClass)) |
|
74
|
|
|
->toMergedArray() |
|
75
|
|
|
; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|