Test Failed
Push — master ( 29bcc5...f2f49d )
by Matt
02:21
created

PropertiesStrategy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 19
c 5
b 1
f 0
dl 0
loc 73
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
    public function __construct(int $propertyTypes = ReflectionProperty::IS_PUBLIC)
36
    {
37
        $this->propertyTypes = $propertyTypes;
38
    }
39
40
    /**
41
     * @param object $object
42
     *
43
     * @return void
44
     */
45
    public function initialize(object $object): void
46
    {
47
        $this->reflectionClass = new ReflectionClass($object);
48
        $this->mappings = iterator_to_array($this->generateKeyMap());
49
    }
50
51
    /**
52
     * @return Generator
53
     */
54
    private function generateKeyMap(): Generator
55
    {
56
        foreach ($this->reflectionClass->getProperties($this->propertyTypes) as $property) {
57
            $attributes = $property->getAttributes(HydrationKey::class);
58
            yield from $attributes
59
                ? $this->generateKeysFromAttributes($property, $attributes)
60
                : $this->generateKeysFromNames($property);
61
        }
62
    }
63
64
    /**
65
     * @param ReflectionProperty $property
66
     * @param array              $attributes
67
     *
68
     * @return Generator
69
     */
70
    private function generateKeysFromAttributes(ReflectionProperty $property, array $attributes): Generator
71
    {
72
        foreach ($attributes as $attribute) {
73
            $mapping = new PropertyMapping($property, $attribute->newInstance());
74
            yield $mapping->getKey() => $mapping;
75
        }
76
    }
77
78
    /**
79
     * @param ReflectionProperty $property
80
     *
81
     * @return Generator
82
     */
83
    private function generateKeysFromNames(ReflectionProperty $property): Generator
84
    {
85
        $mapping = new PropertyMapping($property, new HydrationKey($property->getName()));
86
        yield $mapping->getKey() => $mapping;
87
    }
88
}
89