Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| DtoStrategy | |
100.00% |
17 / 17 |
|
100.00% |
7 / 7 |
15 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isReadOnly | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| getDtoMetadata | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
| getCreationMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getModificationMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getResultMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Metadata\Strategy; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Context\MetadataFieldHashmap; |
| 6 | use Apie\Core\Dto\DtoInterface; |
| 7 | use Apie\Core\Metadata\CompositeMetadata; |
| 8 | use Apie\Core\Metadata\Fields\PublicProperty; |
| 9 | use Apie\Core\Metadata\StrategyInterface; |
| 10 | use Apie\Core\Utils\DtoUtils; |
| 11 | use ReflectionClass; |
| 12 | use ReflectionProperty; |
| 13 | |
| 14 | final class DtoStrategy implements StrategyInterface |
| 15 | { |
| 16 | public static function supports(ReflectionClass $class): bool |
| 17 | { |
| 18 | return DtoUtils::isDto($class); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @param ReflectionClass<DtoInterface> $class |
| 23 | */ |
| 24 | public function __construct(private ReflectionClass $class) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | private function isReadOnly(ReflectionProperty $property, bool $setterHooks): bool |
| 29 | { |
| 30 | if (PHP_VERSION_ID > 80400 && $setterHooks) { |
| 31 | $modifiers = $property->getModifiers(); |
| 32 | if ($modifiers & (ReflectionProperty::IS_PROTECTED_SET|ReflectionProperty::IS_PRIVATE_SET)) { |
| 33 | return true; |
| 34 | } |
| 35 | } |
| 36 | return $property->isReadOnly(); |
| 37 | } |
| 38 | private function getDtoMetadata(ApieContext $context, bool $optional, bool $setterHooks, bool $allowPromoted): CompositeMetadata |
| 39 | { |
| 40 | $list = []; |
| 41 | foreach ($this->class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
| 42 | if ($this->isReadOnly($property, $setterHooks)) { |
| 43 | if ($optional || !($allowPromoted && $property->isPromoted())) { |
| 44 | continue; |
| 45 | } |
| 46 | } |
| 47 | $list[$property->getName()] = new PublicProperty($property, $optional, $setterHooks); |
| 48 | } |
| 49 | |
| 50 | return new CompositeMetadata(new MetadataFieldHashmap($list), $this->class); |
| 51 | } |
| 52 | |
| 53 | public function getCreationMetadata(ApieContext $context): CompositeMetadata |
| 54 | { |
| 55 | return $this->getDtoMetadata($context, false, true, true); |
| 56 | } |
| 57 | |
| 58 | public function getModificationMetadata(ApieContext $context): CompositeMetadata |
| 59 | { |
| 60 | return $this->getDtoMetadata($context, true, true, false); |
| 61 | } |
| 62 | |
| 63 | public function getResultMetadata(ApieContext $context): CompositeMetadata |
| 64 | { |
| 65 | return $this->getDtoMetadata($context, false, false, true); |
| 66 | } |
| 67 | } |