Completed
Pull Request — 5.1 (#125)
by
unknown
03:20
created
src/System/EntityBuilder.php 1 patch
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -11,194 +11,194 @@
 block discarded – undo
11 11
  */
12 12
 class EntityBuilder
13 13
 {
14
-    /**
15
-     * The mapper for the entity to build
16
-     * @var \Analogue\ORM\System\Mapper
17
-     */
18
-    protected $mapper;
19
-
20
-    /**
21
-     * The Entity Map for the entity to build.
22
-     *
23
-     * @var \Analogue\ORM\EntityMap
24
-     */
25
-    protected $entityMap;
26
-
27
-    /**
28
-     * Relations that will be eager loaded on this query
29
-     *
30
-     * @var array
31
-     */
32
-    protected $eagerLoads;
33
-
34
-    /**
35
-     * Relations that will be lazy loaded on this query
36
-     *
37
-     * @var array
38
-     */
39
-    protected $lazyLoads;
40
-
41
-    /**
42
-     * Entity Wrapper Factory
43
-     * @var \Analogue\ORM\System\Wrappers\Factory
44
-     */
45
-    protected $factory;
46
-
47
-    /**
48
-     * EntityBuilder constructor.
49
-     * @param Mapper $mapper
50
-     * @param array  $eagerLoads
51
-     */
52
-    public function __construct(Mapper $mapper, array $eagerLoads)
53
-    {
54
-        $this->mapper = $mapper;
55
-
56
-        $this->entityMap = $mapper->getEntityMap();
57
-
58
-        $this->eagerLoads = $eagerLoads;
59
-
60
-        $this->lazyLoads = $this->prepareLazyLoading();
61
-
62
-        $this->entityMap = $mapper->getEntityMap();
63
-
64
-        $this->factory = new Factory;
65
-    }
66
-
67
-    /**
68
-     * Convert a result set into an array of entities
69
-     *
70
-     * @param  array $results
71
-     * @return array
72
-     */
73
-    public function build($results)
74
-    {
75
-        $entities = [];
76
-
77
-        //$prototype = $this->getWrapperPrototype();
78
-        //$prototype = $this->mapper->newInstance();
79
-
80
-        $keyName = $this->entityMap->getKeyName();
81
-
82
-        $tmpCache = [];
83
-
84
-        foreach ($results as $result) {
85
-            //$instance = clone $prototype;
86
-            $instance = $this->getWrapperInstance();
87
-
88
-            $resultArray = (array) $result;
89
-
90
-            $tmpCache[$resultArray[$keyName]] = $resultArray;
91
-
92
-            // Hydrate any embedded Value Object
93
-            $this->hydrateValueObjects($resultArray);
94
-
95
-            $instance->setEntityAttributes($resultArray);
96
-
97
-            // Hydrate relation attributes with lazyloading proxies
98
-            if (count($this->lazyLoads) > 0) {
99
-                $proxies = $this->getLazyLoadingProxies($instance);
100
-                $instance->setEntityAttributes($resultArray + $proxies);
101
-            }
102
-
103
-            // Directly Unwrap the entity now that it has been hydrated
104
-            $entities[] = $instance->getObject();
105
-        }
106
-
107
-        $this->mapper->getEntityCache()->add($tmpCache);
108
-
109
-        return $entities;
110
-    }
111
-
112
-    /**
113
-     * Get the correct wrapper prototype corresponding to the object type
114
-     *
115
-     * @throws \Analogue\ORM\Exceptions\MappingException
116
-     * @return InternallyMappable
117
-     */
118
-    protected function getWrapperInstance()
119
-    {
120
-        return $this->factory->make($this->mapper->newInstance());
121
-    }
122
-
123
-    /**
124
-     * Hydrate value object embedded in this entity
125
-     *
126
-     * @param  array $attributes
127
-     * @throws \Analogue\ORM\Exceptions\MappingException
128
-     * @return void
129
-     */
130
-    protected function hydrateValueObjects(& $attributes)
131
-    {
132
-        foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
133
-            $this->hydrateValueObject($attributes, $localKey, $valueClass);
134
-        }
135
-    }
136
-
137
-    /**
138
-     * Hydrate a single value object
139
-     *
140
-     * @param  array  $attributes
141
-     * @param  string $localKey
142
-     * @param  string $valueClass
143
-     * @throws \Analogue\ORM\Exceptions\MappingException
144
-     * @return void
145
-     */
146
-    protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
147
-    {
148
-        $map = $this->mapper->getManager()->getValueMap($valueClass);
149
-
150
-        $embeddedAttributes = $map->getAttributes();
151
-
152
-        $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
14
+	/**
15
+	 * The mapper for the entity to build
16
+	 * @var \Analogue\ORM\System\Mapper
17
+	 */
18
+	protected $mapper;
19
+
20
+	/**
21
+	 * The Entity Map for the entity to build.
22
+	 *
23
+	 * @var \Analogue\ORM\EntityMap
24
+	 */
25
+	protected $entityMap;
26
+
27
+	/**
28
+	 * Relations that will be eager loaded on this query
29
+	 *
30
+	 * @var array
31
+	 */
32
+	protected $eagerLoads;
33
+
34
+	/**
35
+	 * Relations that will be lazy loaded on this query
36
+	 *
37
+	 * @var array
38
+	 */
39
+	protected $lazyLoads;
40
+
41
+	/**
42
+	 * Entity Wrapper Factory
43
+	 * @var \Analogue\ORM\System\Wrappers\Factory
44
+	 */
45
+	protected $factory;
46
+
47
+	/**
48
+	 * EntityBuilder constructor.
49
+	 * @param Mapper $mapper
50
+	 * @param array  $eagerLoads
51
+	 */
52
+	public function __construct(Mapper $mapper, array $eagerLoads)
53
+	{
54
+		$this->mapper = $mapper;
55
+
56
+		$this->entityMap = $mapper->getEntityMap();
57
+
58
+		$this->eagerLoads = $eagerLoads;
59
+
60
+		$this->lazyLoads = $this->prepareLazyLoading();
61
+
62
+		$this->entityMap = $mapper->getEntityMap();
63
+
64
+		$this->factory = new Factory;
65
+	}
66
+
67
+	/**
68
+	 * Convert a result set into an array of entities
69
+	 *
70
+	 * @param  array $results
71
+	 * @return array
72
+	 */
73
+	public function build($results)
74
+	{
75
+		$entities = [];
76
+
77
+		//$prototype = $this->getWrapperPrototype();
78
+		//$prototype = $this->mapper->newInstance();
79
+
80
+		$keyName = $this->entityMap->getKeyName();
81
+
82
+		$tmpCache = [];
83
+
84
+		foreach ($results as $result) {
85
+			//$instance = clone $prototype;
86
+			$instance = $this->getWrapperInstance();
87
+
88
+			$resultArray = (array) $result;
89
+
90
+			$tmpCache[$resultArray[$keyName]] = $resultArray;
91
+
92
+			// Hydrate any embedded Value Object
93
+			$this->hydrateValueObjects($resultArray);
94
+
95
+			$instance->setEntityAttributes($resultArray);
96
+
97
+			// Hydrate relation attributes with lazyloading proxies
98
+			if (count($this->lazyLoads) > 0) {
99
+				$proxies = $this->getLazyLoadingProxies($instance);
100
+				$instance->setEntityAttributes($resultArray + $proxies);
101
+			}
102
+
103
+			// Directly Unwrap the entity now that it has been hydrated
104
+			$entities[] = $instance->getObject();
105
+		}
106
+
107
+		$this->mapper->getEntityCache()->add($tmpCache);
108
+
109
+		return $entities;
110
+	}
111
+
112
+	/**
113
+	 * Get the correct wrapper prototype corresponding to the object type
114
+	 *
115
+	 * @throws \Analogue\ORM\Exceptions\MappingException
116
+	 * @return InternallyMappable
117
+	 */
118
+	protected function getWrapperInstance()
119
+	{
120
+		return $this->factory->make($this->mapper->newInstance());
121
+	}
122
+
123
+	/**
124
+	 * Hydrate value object embedded in this entity
125
+	 *
126
+	 * @param  array $attributes
127
+	 * @throws \Analogue\ORM\Exceptions\MappingException
128
+	 * @return void
129
+	 */
130
+	protected function hydrateValueObjects(& $attributes)
131
+	{
132
+		foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
133
+			$this->hydrateValueObject($attributes, $localKey, $valueClass);
134
+		}
135
+	}
136
+
137
+	/**
138
+	 * Hydrate a single value object
139
+	 *
140
+	 * @param  array  $attributes
141
+	 * @param  string $localKey
142
+	 * @param  string $valueClass
143
+	 * @throws \Analogue\ORM\Exceptions\MappingException
144
+	 * @return void
145
+	 */
146
+	protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
147
+	{
148
+		$map = $this->mapper->getManager()->getValueMap($valueClass);
149
+
150
+		$embeddedAttributes = $map->getAttributes();
151
+
152
+		$valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
153 153
 
154
-        foreach ($embeddedAttributes as $key) {
155
-            $prefix = snake_case(class_basename($valueClass)) . '_';
156
-
157
-            $voWrapper = $this->factory->make($valueObject);
158
-
159
-            $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
154
+		foreach ($embeddedAttributes as $key) {
155
+			$prefix = snake_case(class_basename($valueClass)) . '_';
156
+
157
+			$voWrapper = $this->factory->make($valueObject);
158
+
159
+			$voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
160 160
             
161
-            unset($attributes[$prefix . $key]);
162
-        }
161
+			unset($attributes[$prefix . $key]);
162
+		}
163 163
         
164
-        $attributes[$localKey] = $valueObject;
165
-    }
166
-
167
-    /**
168
-     * Deduce the relationships that will be lazy loaded from the eagerLoads array
169
-     *
170
-     * @return array
171
-     */
172
-    protected function prepareLazyLoading()
173
-    {
174
-        $relations = $this->entityMap->getRelationships();
164
+		$attributes[$localKey] = $valueObject;
165
+	}
166
+
167
+	/**
168
+	 * Deduce the relationships that will be lazy loaded from the eagerLoads array
169
+	 *
170
+	 * @return array
171
+	 */
172
+	protected function prepareLazyLoading()
173
+	{
174
+		$relations = $this->entityMap->getRelationships();
175 175
        
176
-        return array_diff($relations, $this->eagerLoads);
177
-    }
178
-
179
-    /**
180
-     * Build lazy loading proxies for the current entity
181
-     *
182
-     * @param InternallyMappable $entity
183
-     *
184
-     * @return array
185
-     */
186
-    protected function getLazyLoadingProxies(InternallyMappable $entity)
187
-    {
188
-        $proxies = [];
189
-
190
-        $singleRelations = $this->entityMap->getSingleRelationships();
191
-        $manyRelations = $this->entityMap->getManyRelationships();
192
-
193
-        foreach ($this->lazyLoads as $relation) {
194
-            if (in_array($relation, $singleRelations)) {
195
-                $proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
196
-            }
197
-            if (in_array($relation, $manyRelations)) {
198
-                $proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
199
-            }
200
-        }
176
+		return array_diff($relations, $this->eagerLoads);
177
+	}
178
+
179
+	/**
180
+	 * Build lazy loading proxies for the current entity
181
+	 *
182
+	 * @param InternallyMappable $entity
183
+	 *
184
+	 * @return array
185
+	 */
186
+	protected function getLazyLoadingProxies(InternallyMappable $entity)
187
+	{
188
+		$proxies = [];
189
+
190
+		$singleRelations = $this->entityMap->getSingleRelationships();
191
+		$manyRelations = $this->entityMap->getManyRelationships();
192
+
193
+		foreach ($this->lazyLoads as $relation) {
194
+			if (in_array($relation, $singleRelations)) {
195
+				$proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
196
+			}
197
+			if (in_array($relation, $manyRelations)) {
198
+				$proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
199
+			}
200
+		}
201 201
         
202
-        return $proxies;
203
-    }
202
+		return $proxies;
203
+	}
204 204
 }
Please login to merge, or discard this patch.