fractalzombie /
frzb-metrics-power
| 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\MetricsPower\Helper; |
||
| 17 | |||
| 18 | use Fp\Collections\ArrayList; |
||
| 19 | use Fp\Functional\Option\Option; |
||
| 20 | use FRZB\Component\MetricsPower\Traits\WithPrivateEmptyConstructor; |
||
| 21 | use JetBrains\PhpStorm\Immutable; |
||
| 22 | |||
| 23 | /** @internal */ |
||
| 24 | #[Immutable] |
||
| 25 | final class AttributeHelper |
||
| 26 | { |
||
| 27 | use WithPrivateEmptyConstructor; |
||
| 28 | |||
| 29 | public static function hasAttribute(object|string $target, string $attributeClass): bool |
||
| 30 | { |
||
| 31 | return !empty(self::getReflectionAttributes($target, $attributeClass)); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @template T |
||
| 36 | * |
||
| 37 | * @param class-string<T> $attributeClass |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 38 | * |
||
| 39 | * @return null|T |
||
| 40 | */ |
||
| 41 | public static function getAttribute(object|string $target, string $attributeClass): ?object |
||
| 42 | { |
||
| 43 | return ArrayList::collect(self::getAttributes($target, $attributeClass)) |
||
| 44 | ->firstElement() |
||
| 45 | ->get(); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @template T |
||
| 50 | * |
||
| 51 | * @param class-string<T> $attributeClass |
||
|
0 ignored issues
–
show
|
|||
| 52 | * |
||
| 53 | * @return array<T> |
||
| 54 | */ |
||
| 55 | public static function getAttributes(object|string $target, string $attributeClass): array |
||
| 56 | { |
||
| 57 | return ArrayList::collect(self::getReflectionAttributes($target, $attributeClass)) |
||
| 58 | ->map(static fn (\ReflectionAttribute $a) => $a->newInstance()) |
||
| 59 | ->toList(); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @template T |
||
| 64 | * |
||
| 65 | * @param class-string<T> $attributeClass |
||
|
0 ignored issues
–
show
|
|||
| 66 | * |
||
| 67 | * @return array<\ReflectionAttribute<T>> |
||
|
0 ignored issues
–
show
|
|||
| 68 | */ |
||
| 69 | public static function getReflectionAttributes(object|string $target, string $attributeClass): array |
||
| 70 | { |
||
| 71 | return Option::fromNullable(ClassHelper::getReflectionClass($target)) |
||
| 72 | ->map( |
||
| 73 | static fn (\ReflectionClass $rClass) => Option::fromNullable(ClassHelper::getParentReflectionClass($rClass)) |
||
| 74 | ->map(static fn (\ReflectionClass $rClass) => [...ClassHelper::getReflectionAttributes($rClass, $attributeClass), ...self::getReflectionAttributes($rClass, $attributeClass)]) |
||
| 75 | ->getOrElse(ClassHelper::getReflectionAttributes($rClass, $attributeClass)) |
||
| 76 | ) |
||
| 77 | ->getOrElse([]); |
||
| 78 | } |
||
| 79 | } |
||
| 80 |