Completed
Branch master (cbd196)
by Rémi
08:54
created

EntityBuilder::getRelationshipsToProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Analogue\ORM\System;
4
5
use Analogue\ORM\System\Wrappers\Factory;
6
7
/**
8
 * This class builds an array of Entity object(s) from a result set.
9
 */
10
class EntityBuilder
11
{
12
    /**
13
     * The mapper for the entity to build.
14
     *
15
     * @var \Analogue\ORM\System\Mapper
16
     */
17
    protected $mapper;
18
19
    /**
20
     * The Entity Map for the entity to build.
21
     *
22
     * @var \Analogue\ORM\EntityMap
23
     */
24
    protected $entityMap;
25
26
    /**
27
     * Relations that are eager loaded on this query.
28
     *
29
     * @var array
30
     */
31
    protected $eagerLoads;
32
33
    /**
34
     * @var array
35
     */
36
    protected $casts;
37
38
    /**
39
     * Entity Wrapper Factory.
40
     *
41
     * @var \Analogue\ORM\System\Wrappers\Factory
42
     */
43
    protected $factory;
44
45
    /**
46
     * EntityBuilder constructor.
47
     *
48
     * @param Mapper $mapper
49
     * @param array  $eagerLoads
50
     */
51
    public function __construct(Mapper $mapper, array $eagerLoads)
52
    {
53
        $this->mapper = $mapper;
54
55
        $this->entityMap = $mapper->getEntityMap();
56
57
        $this->eagerLoads = $eagerLoads;
58
59
        $this->factory = new Factory();
60
    }
61
62
    /**
63
     * Convert an array of values into an entity.
64
     *
65
     * @param array $result
66
     *
67
     * @return array
68
     */
69
    public function build(array $result)
70
    {
71
        $wrapper = $this->getWrapperInstance();
72
73
        // Hydrate any embedded Value Object
74
        //
75
        // TODO Move this to the result builder instead,
76
        // as we'll handle this the same way as they were
77
        // eager loaded relationships.
78
        $this->hydrateValueObjects($result);
79
80
        $wrapper->setEntityAttributes($result);
81
82
        $wrapper->setProxies();
83
        
84
        // Hydrate and return the instance
85
        $wrapper->hydrate();
86
        $entity = $wrapper->getObject();
87
88
        return $entity;
89
    }
90
91
    /**
92
     * Get the correct wrapper prototype corresponding to the object type.
93
     *
94
     * @throws \Analogue\ORM\Exceptions\MappingException
95
     *
96
     * @return InternallyMappable
97
     */
98
    protected function getWrapperInstance()
99
    {
100
        return $this->factory->make($this->mapper->newInstance());
101
    }
102
103
    /**
104
     * Hydrate value object embedded in this entity.
105
     *
106
     * @param array $attributes
107
     *
108
     * @throws \Analogue\ORM\Exceptions\MappingException
109
     *
110
     * @return void
111
     */
112
    protected function hydrateValueObjects(&$attributes)
113
    {
114
        foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
115
            $this->hydrateValueObject($attributes, $localKey, $valueClass);
116
        }
117
    }
118
119
    /**
120
     * Hydrate a single value object.
121
     *
122
     * @param array  $attributes
123
     * @param string $localKey
124
     * @param string $valueClass
125
     *
126
     * @throws \Analogue\ORM\Exceptions\MappingException
127
     *
128
     * @return void
129
     */
130
    protected function hydrateValueObject(&$attributes, $localKey, $valueClass)
131
    {
132
        $map = $this->mapper->getManager()->getValueMap($valueClass);
133
134
        $embeddedAttributes = $map->getAttributes();
135
136
        $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
137
        $voWrapper = $this->factory->make($valueObject);
138
139
        foreach ($embeddedAttributes as $key) {
140
            $prefix = snake_case(class_basename($valueClass)).'_';
141
142
            $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]);
143
144
            unset($attributes[$prefix.$key]);
145
        }
146
147
        $attributes[$localKey] = $voWrapper->getObject();
148
    }
149
}
150