PropertiesStrategy   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 19
c 5
b 1
f 0
dl 0
loc 73
ccs 22
cts 22
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generateKeyMap() 0 7 3
A initialize() 0 4 1
A generateKeysFromAttributes() 0 5 2
A generateKeysFromNames() 0 4 1
1
<?php
2
3
namespace Maiorano\ObjectHydrator\Strategies\Reflection;
4
5
use Generator;
6
use Maiorano\ObjectHydrator\Attributes\HydrationKey;
7
use Maiorano\ObjectHydrator\Mappings\PropertyMapping;
8
use Maiorano\ObjectHydrator\Strategies\DirectKeyAccessTrait;
9
use Maiorano\ObjectHydrator\Strategies\HydrationStrategyInterface;
10
use Maiorano\ObjectHydrator\Strategies\RecursiveCheckTrait;
11
use ReflectionClass;
12
use ReflectionProperty;
13
14
final class PropertiesStrategy implements HydrationStrategyInterface
15
{
16
    use RecursiveCheckTrait;
17
    use DirectKeyAccessTrait;
18
19
    /**
20
     * @var int
21
     */
22
    private int $propertyTypes;
23
    /**
24
     * @var ReflectionClass
25
     */
26
    private ReflectionClass $reflectionClass;
27
    /**
28
     * @var PropertyMapping[]
29
     */
30
    private array $mappings;
31
32
    /**
33
     * @param int $propertyTypes
34
     */
35 10
    public function __construct(int $propertyTypes = ReflectionProperty::IS_PUBLIC)
36
    {
37 10
        $this->propertyTypes = $propertyTypes;
38 10
    }
39
40
    /**
41
     * @param object $object
42
     *
43
     * @return void
44
     */
45 10
    public function initialize(object $object): void
46
    {
47 10
        $this->reflectionClass = new ReflectionClass($object);
48 10
        $this->mappings = iterator_to_array($this->generateKeyMap());
49 10
    }
50
51
    /**
52
     * @return Generator
53
     */
54 10
    private function generateKeyMap(): Generator
55
    {
56 10
        foreach ($this->reflectionClass->getProperties($this->propertyTypes) as $property) {
57 10
            $attributes = $property->getAttributes(HydrationKey::class);
58 10
            yield from $attributes
59 7
                ? $this->generateKeysFromAttributes($property, $attributes)
60 10
                : $this->generateKeysFromNames($property);
61
        }
62 10
    }
63
64
    /**
65
     * @param ReflectionProperty $property
66
     * @param array              $attributes
67
     *
68
     * @return Generator
69
     */
70 7
    private function generateKeysFromAttributes(ReflectionProperty $property, array $attributes): Generator
71
    {
72 7
        foreach ($attributes as $attribute) {
73 7
            $mapping = new PropertyMapping($property, $attribute->newInstance());
74 7
            yield $mapping->getKey() => $mapping;
75
        }
76 7
    }
77
78
    /**
79
     * @param ReflectionProperty $property
80
     *
81
     * @return Generator
82
     */
83 10
    private function generateKeysFromNames(ReflectionProperty $property): Generator
84
    {
85 10
        $mapping = new PropertyMapping($property, new HydrationKey($property->getName()));
86 10
        yield $mapping->getKey() => $mapping;
87 10
    }
88
}
89