@@ -8,68 +8,68 @@ |
||
8 | 8 | */ |
9 | 9 | class CachedRelationship |
10 | 10 | { |
11 | - /** |
|
12 | - * The Hash of the related entity |
|
13 | - * |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $hash; |
|
11 | + /** |
|
12 | + * The Hash of the related entity |
|
13 | + * |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $hash; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Pivot attributes, if any |
|
20 | - * |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $pivotAttributes; |
|
18 | + /** |
|
19 | + * Pivot attributes, if any |
|
20 | + * |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $pivotAttributes; |
|
24 | 24 | |
25 | - /** |
|
26 | - * CachedRelationship constructor. |
|
27 | - * @param $hash |
|
28 | - * @param array $pivotAttributes |
|
29 | - */ |
|
30 | - public function __construct($hash, $pivotAttributes = []) |
|
31 | - { |
|
32 | - $this->hash = $hash; |
|
33 | - $this->pivotAttributes = $pivotAttributes; |
|
34 | - } |
|
25 | + /** |
|
26 | + * CachedRelationship constructor. |
|
27 | + * @param $hash |
|
28 | + * @param array $pivotAttributes |
|
29 | + */ |
|
30 | + public function __construct($hash, $pivotAttributes = []) |
|
31 | + { |
|
32 | + $this->hash = $hash; |
|
33 | + $this->pivotAttributes = $pivotAttributes; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Return true if any pivot attributes are present |
|
38 | - * |
|
39 | - * @return boolean |
|
40 | - */ |
|
41 | - public function hasPivotAttributes() |
|
42 | - { |
|
43 | - return count($this->pivotAttributes) > 0; |
|
44 | - } |
|
36 | + /** |
|
37 | + * Return true if any pivot attributes are present |
|
38 | + * |
|
39 | + * @return boolean |
|
40 | + */ |
|
41 | + public function hasPivotAttributes() |
|
42 | + { |
|
43 | + return count($this->pivotAttributes) > 0; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Returns the hash of the related entity |
|
48 | - * |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function getHash() |
|
52 | - { |
|
53 | - return $this->hash; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Returns the hash of the related entity |
|
48 | + * |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function getHash() |
|
52 | + { |
|
53 | + return $this->hash; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get the cached values for the pivot attributes |
|
58 | - * |
|
59 | - * @return array |
|
60 | - */ |
|
61 | - public function getPivotAttributes() |
|
62 | - { |
|
63 | - return $this->pivotAttributes; |
|
64 | - } |
|
56 | + /** |
|
57 | + * Get the cached values for the pivot attributes |
|
58 | + * |
|
59 | + * @return array |
|
60 | + */ |
|
61 | + public function getPivotAttributes() |
|
62 | + { |
|
63 | + return $this->pivotAttributes; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Access to the hash for fast cache comparison |
|
68 | - * |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - public function __toString() |
|
72 | - { |
|
73 | - return $this->hash; |
|
74 | - } |
|
66 | + /** |
|
67 | + * Access to the hash for fast cache comparison |
|
68 | + * |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + public function __toString() |
|
72 | + { |
|
73 | + return $this->hash; |
|
74 | + } |
|
75 | 75 | } |
@@ -7,79 +7,79 @@ |
||
7 | 7 | */ |
8 | 8 | class EntityWrapper extends Wrapper |
9 | 9 | { |
10 | - /** |
|
11 | - * Method used by the mapper to set the object |
|
12 | - * attribute raw values (hydration) |
|
13 | - * |
|
14 | - * @param array $attributes |
|
15 | - * |
|
16 | - * @return void |
|
17 | - */ |
|
18 | - public function setEntityAttributes(array $attributes) |
|
19 | - { |
|
20 | - $this->entity->setEntityAttributes($attributes); |
|
21 | - } |
|
10 | + /** |
|
11 | + * Method used by the mapper to set the object |
|
12 | + * attribute raw values (hydration) |
|
13 | + * |
|
14 | + * @param array $attributes |
|
15 | + * |
|
16 | + * @return void |
|
17 | + */ |
|
18 | + public function setEntityAttributes(array $attributes) |
|
19 | + { |
|
20 | + $this->entity->setEntityAttributes($attributes); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Method used by the mapper to get the |
|
25 | - * raw object's values. |
|
26 | - * |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public function getEntityAttributes() |
|
30 | - { |
|
31 | - return $this->entity->getEntityAttributes(); |
|
32 | - } |
|
23 | + /** |
|
24 | + * Method used by the mapper to get the |
|
25 | + * raw object's values. |
|
26 | + * |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public function getEntityAttributes() |
|
30 | + { |
|
31 | + return $this->entity->getEntityAttributes(); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Method used by the mapper to set raw |
|
36 | - * key-value pair |
|
37 | - * |
|
38 | - * @param string $key |
|
39 | - * @param string $value |
|
40 | - * |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - public function setEntityAttribute($key, $value) |
|
44 | - { |
|
45 | - $attributes = $this->entity->getEntityAttributes(); |
|
34 | + /** |
|
35 | + * Method used by the mapper to set raw |
|
36 | + * key-value pair |
|
37 | + * |
|
38 | + * @param string $key |
|
39 | + * @param string $value |
|
40 | + * |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + public function setEntityAttribute($key, $value) |
|
44 | + { |
|
45 | + $attributes = $this->entity->getEntityAttributes(); |
|
46 | 46 | |
47 | - $attributes[$key] = $value; |
|
47 | + $attributes[$key] = $value; |
|
48 | 48 | |
49 | - $this->entity->setEntityAttributes($attributes); |
|
50 | - } |
|
49 | + $this->entity->setEntityAttributes($attributes); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Method used by the mapper to get single |
|
54 | - * key-value pair |
|
55 | - * |
|
56 | - * @param string $key |
|
57 | - * @return mixed|null |
|
58 | - */ |
|
59 | - public function getEntityAttribute($key) |
|
60 | - { |
|
61 | - if ($this->hasAttribute($key)) { |
|
62 | - $attributes = $this->entity->getEntityAttributes(); |
|
63 | - return $attributes[$key]; |
|
64 | - } else { |
|
65 | - return null; |
|
66 | - } |
|
67 | - } |
|
52 | + /** |
|
53 | + * Method used by the mapper to get single |
|
54 | + * key-value pair |
|
55 | + * |
|
56 | + * @param string $key |
|
57 | + * @return mixed|null |
|
58 | + */ |
|
59 | + public function getEntityAttribute($key) |
|
60 | + { |
|
61 | + if ($this->hasAttribute($key)) { |
|
62 | + $attributes = $this->entity->getEntityAttributes(); |
|
63 | + return $attributes[$key]; |
|
64 | + } else { |
|
65 | + return null; |
|
66 | + } |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Test if a given attribute exists |
|
71 | - * |
|
72 | - * @param string $key |
|
73 | - * @return boolean |
|
74 | - */ |
|
75 | - public function hasAttribute($key) |
|
76 | - { |
|
77 | - $attributes = $this->entity->getEntityAttributes(); |
|
69 | + /** |
|
70 | + * Test if a given attribute exists |
|
71 | + * |
|
72 | + * @param string $key |
|
73 | + * @return boolean |
|
74 | + */ |
|
75 | + public function hasAttribute($key) |
|
76 | + { |
|
77 | + $attributes = $this->entity->getEntityAttributes(); |
|
78 | 78 | |
79 | - if (array_key_exists($key, $attributes)) { |
|
80 | - return true; |
|
81 | - } else { |
|
82 | - return false; |
|
83 | - } |
|
84 | - } |
|
79 | + if (array_key_exists($key, $attributes)) { |
|
80 | + return true; |
|
81 | + } else { |
|
82 | + return false; |
|
83 | + } |
|
84 | + } |
|
85 | 85 | } |
@@ -7,27 +7,27 @@ |
||
7 | 7 | |
8 | 8 | class Factory |
9 | 9 | { |
10 | - /** |
|
11 | - * Build the wrapper corresponding to the object's type |
|
12 | - * |
|
13 | - * @param mixed $object |
|
14 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
15 | - * @return Wrapper |
|
16 | - */ |
|
17 | - public function make($object) |
|
18 | - { |
|
19 | - $manager = Manager::getInstance(); |
|
10 | + /** |
|
11 | + * Build the wrapper corresponding to the object's type |
|
12 | + * |
|
13 | + * @param mixed $object |
|
14 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
15 | + * @return Wrapper |
|
16 | + */ |
|
17 | + public function make($object) |
|
18 | + { |
|
19 | + $manager = Manager::getInstance(); |
|
20 | 20 | |
21 | - if ($manager->isValueObject($object)) { |
|
22 | - $entityMap = $manager->getValueMap($object); |
|
23 | - } else { |
|
24 | - $entityMap = $manager->mapper($object)->getEntityMap(); |
|
25 | - } |
|
21 | + if ($manager->isValueObject($object)) { |
|
22 | + $entityMap = $manager->getValueMap($object); |
|
23 | + } else { |
|
24 | + $entityMap = $manager->mapper($object)->getEntityMap(); |
|
25 | + } |
|
26 | 26 | |
27 | - if ($object instanceof Mappable) { |
|
28 | - return new EntityWrapper($object, $entityMap); |
|
29 | - } else { |
|
30 | - return new PlainObjectWrapper($object, $entityMap); |
|
31 | - } |
|
32 | - } |
|
27 | + if ($object instanceof Mappable) { |
|
28 | + return new EntityWrapper($object, $entityMap); |
|
29 | + } else { |
|
30 | + return new PlainObjectWrapper($object, $entityMap); |
|
31 | + } |
|
32 | + } |
|
33 | 33 | } |
@@ -81,7 +81,7 @@ |
||
81 | 81 | |
82 | 82 | // We need to filter out properties that could belong to the object |
83 | 83 | // and which are not intended to be handled by the ORM |
84 | - return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
84 | + return array_filter($objectProperties, function(\ReflectionProperty $item) use ($attributeList) { |
|
85 | 85 | if (in_array($item->getName(), $attributeList)) { |
86 | 86 | return true; |
87 | 87 | } |
@@ -6,201 +6,201 @@ |
||
6 | 6 | |
7 | 7 | class PlainObjectWrapper extends Wrapper |
8 | 8 | { |
9 | - /** |
|
10 | - * The list of attributes for the managed entity |
|
11 | - * |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected $attributeList; |
|
15 | - |
|
16 | - /** |
|
17 | - * The reflection class for the managed entity |
|
18 | - * |
|
19 | - * @var ReflectionClass |
|
20 | - */ |
|
21 | - protected $reflection; |
|
22 | - |
|
23 | - /** |
|
24 | - * PlainObjectWrapper constructor. |
|
25 | - * @param $popoEntity |
|
26 | - * @param $entityMap |
|
27 | - */ |
|
28 | - public function __construct($popoEntity, $entityMap) |
|
29 | - { |
|
30 | - $this->reflection = new ReflectionClass($popoEntity); |
|
31 | - |
|
32 | - parent::__construct($popoEntity, $entityMap); |
|
33 | - |
|
34 | - $this->attributeList = $this->getAttributeList(); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Get Compiled Attributes (key, attributes, embed, relations) |
|
39 | - * |
|
40 | - * @return array |
|
41 | - */ |
|
42 | - protected function getAttributeList() |
|
43 | - { |
|
44 | - return $this->entityMap->getCompiledAttributes(); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Extract Attributes from a Plain Php Object |
|
49 | - * |
|
50 | - * @return array $attributes |
|
51 | - */ |
|
52 | - protected function extract() |
|
53 | - { |
|
54 | - $properties = $this->getMappedProperties(); |
|
55 | - |
|
56 | - $attributes = []; |
|
57 | - |
|
58 | - foreach ($properties as $property) { |
|
59 | - $name = $property->getName(); |
|
60 | - |
|
61 | - if ($property->isPublic()) { |
|
62 | - $attributes[$name] = $this->entity->$name; |
|
63 | - } else { |
|
64 | - $property->setAccessible(true); |
|
9 | + /** |
|
10 | + * The list of attributes for the managed entity |
|
11 | + * |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected $attributeList; |
|
15 | + |
|
16 | + /** |
|
17 | + * The reflection class for the managed entity |
|
18 | + * |
|
19 | + * @var ReflectionClass |
|
20 | + */ |
|
21 | + protected $reflection; |
|
22 | + |
|
23 | + /** |
|
24 | + * PlainObjectWrapper constructor. |
|
25 | + * @param $popoEntity |
|
26 | + * @param $entityMap |
|
27 | + */ |
|
28 | + public function __construct($popoEntity, $entityMap) |
|
29 | + { |
|
30 | + $this->reflection = new ReflectionClass($popoEntity); |
|
31 | + |
|
32 | + parent::__construct($popoEntity, $entityMap); |
|
33 | + |
|
34 | + $this->attributeList = $this->getAttributeList(); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Get Compiled Attributes (key, attributes, embed, relations) |
|
39 | + * |
|
40 | + * @return array |
|
41 | + */ |
|
42 | + protected function getAttributeList() |
|
43 | + { |
|
44 | + return $this->entityMap->getCompiledAttributes(); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Extract Attributes from a Plain Php Object |
|
49 | + * |
|
50 | + * @return array $attributes |
|
51 | + */ |
|
52 | + protected function extract() |
|
53 | + { |
|
54 | + $properties = $this->getMappedProperties(); |
|
55 | + |
|
56 | + $attributes = []; |
|
57 | + |
|
58 | + foreach ($properties as $property) { |
|
59 | + $name = $property->getName(); |
|
60 | + |
|
61 | + if ($property->isPublic()) { |
|
62 | + $attributes[$name] = $this->entity->$name; |
|
63 | + } else { |
|
64 | + $property->setAccessible(true); |
|
65 | 65 | |
66 | - $attributes[$name] = $property->getValue($this->entity); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - return $attributes; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @return \ReflectionProperty[] |
|
75 | - */ |
|
76 | - protected function getMappedProperties() |
|
77 | - { |
|
78 | - $objectProperties = $this->reflection->getProperties(); |
|
79 | - |
|
80 | - $attributeList = $this->getAttributeList(); |
|
81 | - |
|
82 | - // We need to filter out properties that could belong to the object |
|
83 | - // and which are not intended to be handled by the ORM |
|
84 | - return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
85 | - if (in_array($item->getName(), $attributeList)) { |
|
86 | - return true; |
|
87 | - } |
|
88 | - }); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $name |
|
93 | - * @return \ReflectionProperty |
|
94 | - */ |
|
95 | - protected function getMappedProperty($name) |
|
96 | - { |
|
97 | - return $this->reflection->getProperty($name); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Hydrate Plain PHP Object with wrapped attributes |
|
102 | - * |
|
103 | - * @param $attributes |
|
104 | - * @return void |
|
105 | - */ |
|
106 | - protected function hydrate($attributes) |
|
107 | - { |
|
108 | - $properties = $this->getMappedProperties(); |
|
109 | - |
|
110 | - foreach ($properties as $property) { |
|
111 | - $name = $property->getName(); |
|
112 | - |
|
113 | - if ($property->isPublic()) { |
|
114 | - $this->entity->$name = $attributes[$name]; |
|
115 | - } else { |
|
116 | - $property->setAccessible(true); |
|
117 | - |
|
118 | - $property->setValue($this->entity, $attributes[$name]); |
|
119 | - } |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Method used by the mapper to set the object |
|
125 | - * attribute raw values (hydration) |
|
126 | - * |
|
127 | - * @param array $attributes |
|
128 | - * |
|
129 | - * @return void |
|
130 | - */ |
|
131 | - public function setEntityAttributes(array $attributes) |
|
132 | - { |
|
133 | - $this->hydrate($attributes); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Method used by the mapper to get the |
|
138 | - * raw object's values. |
|
139 | - * |
|
140 | - * @return array |
|
141 | - */ |
|
142 | - public function getEntityAttributes() |
|
143 | - { |
|
144 | - return $this->extract(); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Method used by the mapper to set raw |
|
149 | - * key-value pair |
|
150 | - * |
|
151 | - * @param string $key |
|
152 | - * @param string $value |
|
153 | - * |
|
154 | - * @return void |
|
155 | - */ |
|
156 | - public function setEntityAttribute($key, $value) |
|
157 | - { |
|
158 | - $property = $this->getMappedProperty($key); |
|
159 | - |
|
160 | - if ($property->isPublic()) { |
|
161 | - $this->entity->$key = $value; |
|
162 | - } else { |
|
163 | - $property->setAccessible(true); |
|
66 | + $attributes[$name] = $property->getValue($this->entity); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + return $attributes; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @return \ReflectionProperty[] |
|
75 | + */ |
|
76 | + protected function getMappedProperties() |
|
77 | + { |
|
78 | + $objectProperties = $this->reflection->getProperties(); |
|
79 | + |
|
80 | + $attributeList = $this->getAttributeList(); |
|
81 | + |
|
82 | + // We need to filter out properties that could belong to the object |
|
83 | + // and which are not intended to be handled by the ORM |
|
84 | + return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
85 | + if (in_array($item->getName(), $attributeList)) { |
|
86 | + return true; |
|
87 | + } |
|
88 | + }); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $name |
|
93 | + * @return \ReflectionProperty |
|
94 | + */ |
|
95 | + protected function getMappedProperty($name) |
|
96 | + { |
|
97 | + return $this->reflection->getProperty($name); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Hydrate Plain PHP Object with wrapped attributes |
|
102 | + * |
|
103 | + * @param $attributes |
|
104 | + * @return void |
|
105 | + */ |
|
106 | + protected function hydrate($attributes) |
|
107 | + { |
|
108 | + $properties = $this->getMappedProperties(); |
|
109 | + |
|
110 | + foreach ($properties as $property) { |
|
111 | + $name = $property->getName(); |
|
112 | + |
|
113 | + if ($property->isPublic()) { |
|
114 | + $this->entity->$name = $attributes[$name]; |
|
115 | + } else { |
|
116 | + $property->setAccessible(true); |
|
117 | + |
|
118 | + $property->setValue($this->entity, $attributes[$name]); |
|
119 | + } |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Method used by the mapper to set the object |
|
125 | + * attribute raw values (hydration) |
|
126 | + * |
|
127 | + * @param array $attributes |
|
128 | + * |
|
129 | + * @return void |
|
130 | + */ |
|
131 | + public function setEntityAttributes(array $attributes) |
|
132 | + { |
|
133 | + $this->hydrate($attributes); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Method used by the mapper to get the |
|
138 | + * raw object's values. |
|
139 | + * |
|
140 | + * @return array |
|
141 | + */ |
|
142 | + public function getEntityAttributes() |
|
143 | + { |
|
144 | + return $this->extract(); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Method used by the mapper to set raw |
|
149 | + * key-value pair |
|
150 | + * |
|
151 | + * @param string $key |
|
152 | + * @param string $value |
|
153 | + * |
|
154 | + * @return void |
|
155 | + */ |
|
156 | + public function setEntityAttribute($key, $value) |
|
157 | + { |
|
158 | + $property = $this->getMappedProperty($key); |
|
159 | + |
|
160 | + if ($property->isPublic()) { |
|
161 | + $this->entity->$key = $value; |
|
162 | + } else { |
|
163 | + $property->setAccessible(true); |
|
164 | 164 | |
165 | - $property->setValue($this->entity, $value); |
|
166 | - } |
|
167 | - |
|
168 | - $this->attributes[$key] = $value; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Method used by the mapper to get single |
|
173 | - * key-value pair |
|
174 | - * |
|
175 | - * @param string $key |
|
176 | - * @return mixed |
|
177 | - */ |
|
178 | - public function getEntityAttribute($key) |
|
179 | - { |
|
180 | - $property = $this->getMappedProperty($key); |
|
181 | - |
|
182 | - if ($property->isPublic()) { |
|
183 | - $value = $this->entity->$key; |
|
184 | - } else { |
|
185 | - $property->setAccessible(true); |
|
186 | - $value = $property->getValue($this->entity); |
|
187 | - } |
|
188 | - |
|
189 | - return $value; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Test if a given attribute exists |
|
194 | - * |
|
195 | - * @param string $key |
|
196 | - * @return boolean |
|
197 | - */ |
|
198 | - public function hasAttribute($key) |
|
199 | - { |
|
200 | - if (array_key_exists($key, $this->attributeList)) { |
|
201 | - return true; |
|
202 | - } else { |
|
203 | - return false; |
|
204 | - } |
|
205 | - } |
|
165 | + $property->setValue($this->entity, $value); |
|
166 | + } |
|
167 | + |
|
168 | + $this->attributes[$key] = $value; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Method used by the mapper to get single |
|
173 | + * key-value pair |
|
174 | + * |
|
175 | + * @param string $key |
|
176 | + * @return mixed |
|
177 | + */ |
|
178 | + public function getEntityAttribute($key) |
|
179 | + { |
|
180 | + $property = $this->getMappedProperty($key); |
|
181 | + |
|
182 | + if ($property->isPublic()) { |
|
183 | + $value = $this->entity->$key; |
|
184 | + } else { |
|
185 | + $property->setAccessible(true); |
|
186 | + $value = $property->getValue($this->entity); |
|
187 | + } |
|
188 | + |
|
189 | + return $value; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Test if a given attribute exists |
|
194 | + * |
|
195 | + * @param string $key |
|
196 | + * @return boolean |
|
197 | + */ |
|
198 | + public function hasAttribute($key) |
|
199 | + { |
|
200 | + if (array_key_exists($key, $this->attributeList)) { |
|
201 | + return true; |
|
202 | + } else { |
|
203 | + return false; |
|
204 | + } |
|
205 | + } |
|
206 | 206 | } |
@@ -17,218 +17,218 @@ |
||
17 | 17 | */ |
18 | 18 | class CollectionProxy extends Proxy implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
19 | 19 | { |
20 | - /** |
|
21 | - * Underlying Lazyloaded collection |
|
22 | - * @var EntityCollection |
|
23 | - */ |
|
24 | - protected $loadedCollection; |
|
25 | - |
|
26 | - /** |
|
27 | - * Added Items Collection |
|
28 | - * @var EntityCollection |
|
29 | - */ |
|
30 | - protected $addedItems; |
|
31 | - |
|
32 | - /** |
|
33 | - * @param mixed $parentEntity |
|
34 | - * @param string $relation relationship method handled by the proxy. |
|
35 | - */ |
|
36 | - public function __construct($parentEntity, $relation) |
|
37 | - { |
|
38 | - $this->addedItems = new EntityCollection; |
|
39 | - |
|
40 | - parent::__construct($parentEntity, $relation); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Add an entity to the proxy collection, weither it's loaded or not |
|
45 | - * |
|
46 | - * @param mixed $entity |
|
47 | - * @return self|void |
|
48 | - */ |
|
49 | - public function add($entity) |
|
50 | - { |
|
51 | - if ($this->isLoaded()) { |
|
52 | - return $this->loadedCollection->add($entity); |
|
53 | - } else { |
|
54 | - $this->addedItems->add($entity); |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Check if Proxy collection has been lazy-loaded |
|
60 | - * |
|
61 | - * @return boolean |
|
62 | - */ |
|
63 | - public function isLoaded() |
|
64 | - { |
|
65 | - return !is_null($this->loadedCollection); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Return the underlying collection |
|
70 | - * |
|
71 | - * @return EntityCollection |
|
72 | - */ |
|
73 | - public function getUnderlyingCollection() |
|
74 | - { |
|
75 | - return $this->loadedCollection; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Return Items that has been added prior to lazy-loading |
|
80 | - * |
|
81 | - * @return EntityCollection |
|
82 | - */ |
|
83 | - public function getAddedItems() |
|
84 | - { |
|
85 | - return $this->addedItems; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Load the underlying relation |
|
90 | - * |
|
91 | - * @return void |
|
92 | - */ |
|
93 | - protected function loadOnce() |
|
94 | - { |
|
95 | - if ($this->isLoaded()) { |
|
96 | - return; |
|
97 | - } |
|
20 | + /** |
|
21 | + * Underlying Lazyloaded collection |
|
22 | + * @var EntityCollection |
|
23 | + */ |
|
24 | + protected $loadedCollection; |
|
25 | + |
|
26 | + /** |
|
27 | + * Added Items Collection |
|
28 | + * @var EntityCollection |
|
29 | + */ |
|
30 | + protected $addedItems; |
|
31 | + |
|
32 | + /** |
|
33 | + * @param mixed $parentEntity |
|
34 | + * @param string $relation relationship method handled by the proxy. |
|
35 | + */ |
|
36 | + public function __construct($parentEntity, $relation) |
|
37 | + { |
|
38 | + $this->addedItems = new EntityCollection; |
|
39 | + |
|
40 | + parent::__construct($parentEntity, $relation); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Add an entity to the proxy collection, weither it's loaded or not |
|
45 | + * |
|
46 | + * @param mixed $entity |
|
47 | + * @return self|void |
|
48 | + */ |
|
49 | + public function add($entity) |
|
50 | + { |
|
51 | + if ($this->isLoaded()) { |
|
52 | + return $this->loadedCollection->add($entity); |
|
53 | + } else { |
|
54 | + $this->addedItems->add($entity); |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Check if Proxy collection has been lazy-loaded |
|
60 | + * |
|
61 | + * @return boolean |
|
62 | + */ |
|
63 | + public function isLoaded() |
|
64 | + { |
|
65 | + return !is_null($this->loadedCollection); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Return the underlying collection |
|
70 | + * |
|
71 | + * @return EntityCollection |
|
72 | + */ |
|
73 | + public function getUnderlyingCollection() |
|
74 | + { |
|
75 | + return $this->loadedCollection; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Return Items that has been added prior to lazy-loading |
|
80 | + * |
|
81 | + * @return EntityCollection |
|
82 | + */ |
|
83 | + public function getAddedItems() |
|
84 | + { |
|
85 | + return $this->addedItems; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Load the underlying relation |
|
90 | + * |
|
91 | + * @return void |
|
92 | + */ |
|
93 | + protected function loadOnce() |
|
94 | + { |
|
95 | + if ($this->isLoaded()) { |
|
96 | + return; |
|
97 | + } |
|
98 | 98 | |
99 | - $this->loadedCollection = $this->load(); |
|
99 | + $this->loadedCollection = $this->load(); |
|
100 | 100 | |
101 | - foreach ($this->addedItems as $entity) { |
|
102 | - $this->loadedCollection->add($entity); |
|
103 | - } |
|
101 | + foreach ($this->addedItems as $entity) { |
|
102 | + $this->loadedCollection->add($entity); |
|
103 | + } |
|
104 | 104 | |
105 | - $this->addedItems = null; |
|
106 | - } |
|
105 | + $this->addedItems = null; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Count the number of items in the collection. |
|
110 | - * |
|
111 | - * @return int |
|
112 | - */ |
|
113 | - public function count() |
|
114 | - { |
|
115 | - $this->loadOnce(); |
|
116 | - |
|
117 | - return $this->getUnderlyingCollection()->count(); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Determine if an item exists at an offset. |
|
122 | - * |
|
123 | - * @param mixed $key |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - public function offsetExists($key) |
|
127 | - { |
|
128 | - $this->loadOnce(); |
|
129 | - |
|
130 | - return $this->getUnderlyingCollection()->offsetExists($key); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Get an item at a given offset. |
|
135 | - * |
|
136 | - * @param mixed $key |
|
137 | - * @return mixed |
|
138 | - */ |
|
139 | - public function offsetGet($key) |
|
140 | - { |
|
141 | - $this->loadOnce(); |
|
142 | - |
|
143 | - return $this->getUnderlyingCollection()->offsetGet($key); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Set the item at a given offset. |
|
148 | - * |
|
149 | - * @param mixed $key |
|
150 | - * @param mixed $value |
|
151 | - */ |
|
152 | - public function offsetSet($key, $value) |
|
153 | - { |
|
154 | - $this->loadOnce(); |
|
155 | - |
|
156 | - $this->getUnderlyingCollection()->offsetSet($key, $value); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Unset the item at a given offset. |
|
161 | - * |
|
162 | - * @param string $key |
|
163 | - */ |
|
164 | - public function offsetUnset($key) |
|
165 | - { |
|
166 | - $this->loadOnce(); |
|
167 | - |
|
168 | - $this->getUnderlyingCollection()->offsetUnset($key); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get the collection of items as a plain array. |
|
173 | - * |
|
174 | - * @return array |
|
175 | - */ |
|
176 | - public function toArray() |
|
177 | - { |
|
178 | - $this->loadOnce(); |
|
179 | - |
|
180 | - return $this->getUnderlyingCollection()->toArray(); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Convert the object into something JSON serializable. |
|
185 | - * |
|
186 | - * @return array |
|
187 | - */ |
|
188 | - public function jsonSerialize() |
|
189 | - { |
|
190 | - $this->loadOnce(); |
|
191 | - |
|
192 | - return $this->getUnderlyingCollection()->jsonSerialize(); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Get the collection of items as JSON. |
|
197 | - * |
|
198 | - * @param int $options |
|
199 | - * @return string |
|
200 | - */ |
|
201 | - public function toJson($options = 0) |
|
202 | - { |
|
203 | - $this->loadOnce(); |
|
204 | - |
|
205 | - return $this->getUnderlyingCollection()->toJson(); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Get an iterator for the items. |
|
210 | - * |
|
211 | - * @return \ArrayIterator |
|
212 | - */ |
|
213 | - public function getIterator() |
|
214 | - { |
|
215 | - $this->loadOnce(); |
|
216 | - |
|
217 | - return $this->getUnderlyingCollection()->getIterator(); |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * @param $method |
|
223 | - * @param $parameters |
|
224 | - * @return mixed |
|
225 | - */ |
|
226 | - public function __call($method, $parameters) |
|
227 | - { |
|
228 | - if (!$this->isLoaded()) { |
|
229 | - $this->loadOnce(); |
|
230 | - } |
|
231 | - |
|
232 | - return call_user_func_array([$this->loadedCollection, $method], $parameters); |
|
233 | - } |
|
108 | + /** |
|
109 | + * Count the number of items in the collection. |
|
110 | + * |
|
111 | + * @return int |
|
112 | + */ |
|
113 | + public function count() |
|
114 | + { |
|
115 | + $this->loadOnce(); |
|
116 | + |
|
117 | + return $this->getUnderlyingCollection()->count(); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Determine if an item exists at an offset. |
|
122 | + * |
|
123 | + * @param mixed $key |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + public function offsetExists($key) |
|
127 | + { |
|
128 | + $this->loadOnce(); |
|
129 | + |
|
130 | + return $this->getUnderlyingCollection()->offsetExists($key); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Get an item at a given offset. |
|
135 | + * |
|
136 | + * @param mixed $key |
|
137 | + * @return mixed |
|
138 | + */ |
|
139 | + public function offsetGet($key) |
|
140 | + { |
|
141 | + $this->loadOnce(); |
|
142 | + |
|
143 | + return $this->getUnderlyingCollection()->offsetGet($key); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Set the item at a given offset. |
|
148 | + * |
|
149 | + * @param mixed $key |
|
150 | + * @param mixed $value |
|
151 | + */ |
|
152 | + public function offsetSet($key, $value) |
|
153 | + { |
|
154 | + $this->loadOnce(); |
|
155 | + |
|
156 | + $this->getUnderlyingCollection()->offsetSet($key, $value); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Unset the item at a given offset. |
|
161 | + * |
|
162 | + * @param string $key |
|
163 | + */ |
|
164 | + public function offsetUnset($key) |
|
165 | + { |
|
166 | + $this->loadOnce(); |
|
167 | + |
|
168 | + $this->getUnderlyingCollection()->offsetUnset($key); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get the collection of items as a plain array. |
|
173 | + * |
|
174 | + * @return array |
|
175 | + */ |
|
176 | + public function toArray() |
|
177 | + { |
|
178 | + $this->loadOnce(); |
|
179 | + |
|
180 | + return $this->getUnderlyingCollection()->toArray(); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Convert the object into something JSON serializable. |
|
185 | + * |
|
186 | + * @return array |
|
187 | + */ |
|
188 | + public function jsonSerialize() |
|
189 | + { |
|
190 | + $this->loadOnce(); |
|
191 | + |
|
192 | + return $this->getUnderlyingCollection()->jsonSerialize(); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Get the collection of items as JSON. |
|
197 | + * |
|
198 | + * @param int $options |
|
199 | + * @return string |
|
200 | + */ |
|
201 | + public function toJson($options = 0) |
|
202 | + { |
|
203 | + $this->loadOnce(); |
|
204 | + |
|
205 | + return $this->getUnderlyingCollection()->toJson(); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Get an iterator for the items. |
|
210 | + * |
|
211 | + * @return \ArrayIterator |
|
212 | + */ |
|
213 | + public function getIterator() |
|
214 | + { |
|
215 | + $this->loadOnce(); |
|
216 | + |
|
217 | + return $this->getUnderlyingCollection()->getIterator(); |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * @param $method |
|
223 | + * @param $parameters |
|
224 | + * @return mixed |
|
225 | + */ |
|
226 | + public function __call($method, $parameters) |
|
227 | + { |
|
228 | + if (!$this->isLoaded()) { |
|
229 | + $this->loadOnce(); |
|
230 | + } |
|
231 | + |
|
232 | + return call_user_func_array([$this->loadedCollection, $method], $parameters); |
|
233 | + } |
|
234 | 234 | } |
@@ -5,17 +5,17 @@ |
||
5 | 5 | |
6 | 6 | interface ProxyInterface |
7 | 7 | { |
8 | - /** |
|
9 | - * Convert a proxy into the underlying related Object |
|
10 | - * |
|
11 | - * @return Mappable|\Analogue\ORM\EntityCollection |
|
12 | - */ |
|
13 | - public function load(); |
|
8 | + /** |
|
9 | + * Convert a proxy into the underlying related Object |
|
10 | + * |
|
11 | + * @return Mappable|\Analogue\ORM\EntityCollection |
|
12 | + */ |
|
13 | + public function load(); |
|
14 | 14 | |
15 | - /** |
|
16 | - * Return true if the underlying relation has been lazy loaded |
|
17 | - * |
|
18 | - * @return boolean |
|
19 | - */ |
|
20 | - public function isLoaded(); |
|
15 | + /** |
|
16 | + * Return true if the underlying relation has been lazy loaded |
|
17 | + * |
|
18 | + * @return boolean |
|
19 | + */ |
|
20 | + public function isLoaded(); |
|
21 | 21 | } |
@@ -7,87 +7,87 @@ |
||
7 | 7 | |
8 | 8 | abstract class Proxy implements ProxyInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * The name of the relationship method handled by the proxy. |
|
12 | - * |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected $relation; |
|
10 | + /** |
|
11 | + * The name of the relationship method handled by the proxy. |
|
12 | + * |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected $relation; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Reference to parent entity object |
|
19 | - * |
|
20 | - * @var \Analogue\ORM\System\InternallyMappable |
|
21 | - */ |
|
22 | - protected $parentEntity; |
|
17 | + /** |
|
18 | + * Reference to parent entity object |
|
19 | + * |
|
20 | + * @var \Analogue\ORM\System\InternallyMappable |
|
21 | + */ |
|
22 | + protected $parentEntity; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Lazy loaded relation flag |
|
26 | - * |
|
27 | - * @var boolean |
|
28 | - */ |
|
29 | - protected $loaded = false; |
|
24 | + /** |
|
25 | + * Lazy loaded relation flag |
|
26 | + * |
|
27 | + * @var boolean |
|
28 | + */ |
|
29 | + protected $loaded = false; |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param mixed $parentEntity |
|
33 | - * @param string $relation relationship method handled by the proxy. |
|
34 | - */ |
|
35 | - public function __construct($parentEntity, $relation) |
|
36 | - { |
|
37 | - $this->parentEntity = $parentEntity; |
|
31 | + /** |
|
32 | + * @param mixed $parentEntity |
|
33 | + * @param string $relation relationship method handled by the proxy. |
|
34 | + */ |
|
35 | + public function __construct($parentEntity, $relation) |
|
36 | + { |
|
37 | + $this->parentEntity = $parentEntity; |
|
38 | 38 | |
39 | - $this->relation = $relation; |
|
40 | - } |
|
39 | + $this->relation = $relation; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Call the relationship method on the underlying entity map |
|
44 | - * |
|
45 | - * @throws MappingException |
|
46 | - * @return mixed |
|
47 | - */ |
|
48 | - public function load() |
|
49 | - { |
|
50 | - $entities = $this->query($this->parentEntity, $this->relation)->getResults($this->relation); |
|
42 | + /** |
|
43 | + * Call the relationship method on the underlying entity map |
|
44 | + * |
|
45 | + * @throws MappingException |
|
46 | + * @return mixed |
|
47 | + */ |
|
48 | + public function load() |
|
49 | + { |
|
50 | + $entities = $this->query($this->parentEntity, $this->relation)->getResults($this->relation); |
|
51 | 51 | |
52 | - $this->loaded = true; |
|
52 | + $this->loaded = true; |
|
53 | 53 | |
54 | - return $entities; |
|
55 | - } |
|
54 | + return $entities; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Return true if the underlying relation has been lazy loaded |
|
59 | - * |
|
60 | - * @return boolean |
|
61 | - */ |
|
62 | - public function isLoaded() |
|
63 | - { |
|
64 | - return $this->loaded; |
|
65 | - } |
|
57 | + /** |
|
58 | + * Return true if the underlying relation has been lazy loaded |
|
59 | + * |
|
60 | + * @return boolean |
|
61 | + */ |
|
62 | + public function isLoaded() |
|
63 | + { |
|
64 | + return $this->loaded; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Return the Query Builder on the relation |
|
69 | - * |
|
70 | - * @param \Analogue\ORM\System\InternallyMappable $entity |
|
71 | - * @param string $relation |
|
72 | - * @throws MappingException |
|
73 | - * @return \Analogue\ORM\System\Query |
|
74 | - */ |
|
75 | - protected function query($entity, $relation) |
|
76 | - { |
|
77 | - $entityMap = $this->getMapper($entity)->getEntityMap(); |
|
67 | + /** |
|
68 | + * Return the Query Builder on the relation |
|
69 | + * |
|
70 | + * @param \Analogue\ORM\System\InternallyMappable $entity |
|
71 | + * @param string $relation |
|
72 | + * @throws MappingException |
|
73 | + * @return \Analogue\ORM\System\Query |
|
74 | + */ |
|
75 | + protected function query($entity, $relation) |
|
76 | + { |
|
77 | + $entityMap = $this->getMapper($entity)->getEntityMap(); |
|
78 | 78 | |
79 | - return $entityMap->$relation($entity); |
|
80 | - } |
|
79 | + return $entityMap->$relation($entity); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Get the mapper instance for the entity |
|
84 | - * |
|
85 | - * @param \Analogue\ORM\System\InternallyMappable $entity |
|
86 | - * @throws MappingException |
|
87 | - * @return \Analogue\ORM\System\Mapper |
|
88 | - */ |
|
89 | - protected function getMapper($entity) |
|
90 | - { |
|
91 | - return Manager::getMapper($entity); |
|
92 | - } |
|
82 | + /** |
|
83 | + * Get the mapper instance for the entity |
|
84 | + * |
|
85 | + * @param \Analogue\ORM\System\InternallyMappable $entity |
|
86 | + * @throws MappingException |
|
87 | + * @return \Analogue\ORM\System\Mapper |
|
88 | + */ |
|
89 | + protected function getMapper($entity) |
|
90 | + { |
|
91 | + return Manager::getMapper($entity); |
|
92 | + } |
|
93 | 93 | } |
@@ -13,70 +13,70 @@ |
||
13 | 13 | */ |
14 | 14 | class MapperFactory |
15 | 15 | { |
16 | - /** |
|
17 | - * Manager instance |
|
18 | - * |
|
19 | - * @var \Analogue\ORM\System\Manager |
|
20 | - */ |
|
21 | - protected $manager; |
|
16 | + /** |
|
17 | + * Manager instance |
|
18 | + * |
|
19 | + * @var \Analogue\ORM\System\Manager |
|
20 | + */ |
|
21 | + protected $manager; |
|
22 | 22 | |
23 | - /** |
|
24 | - * DriverManager instance |
|
25 | - * |
|
26 | - * @var \Analogue\ORM\Drivers\Manager |
|
27 | - */ |
|
28 | - protected $drivers; |
|
23 | + /** |
|
24 | + * DriverManager instance |
|
25 | + * |
|
26 | + * @var \Analogue\ORM\Drivers\Manager |
|
27 | + */ |
|
28 | + protected $drivers; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Event dispatcher instance |
|
32 | - * |
|
33 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
34 | - */ |
|
35 | - protected $dispatcher; |
|
30 | + /** |
|
31 | + * Event dispatcher instance |
|
32 | + * |
|
33 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
34 | + */ |
|
35 | + protected $dispatcher; |
|
36 | 36 | |
37 | - /** |
|
38 | - * MapperFactory constructor. |
|
39 | - * @param DriverManager $drivers |
|
40 | - * @param Dispatcher $dispatcher |
|
41 | - * @param Manager $manager |
|
42 | - */ |
|
43 | - public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
44 | - { |
|
45 | - $this->drivers = $drivers; |
|
37 | + /** |
|
38 | + * MapperFactory constructor. |
|
39 | + * @param DriverManager $drivers |
|
40 | + * @param Dispatcher $dispatcher |
|
41 | + * @param Manager $manager |
|
42 | + */ |
|
43 | + public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
44 | + { |
|
45 | + $this->drivers = $drivers; |
|
46 | 46 | |
47 | - $this->dispatcher = $dispatcher; |
|
47 | + $this->dispatcher = $dispatcher; |
|
48 | 48 | |
49 | - $this->manager = $manager; |
|
50 | - } |
|
49 | + $this->manager = $manager; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Return a new Mapper instance |
|
54 | - * |
|
55 | - * @param string $entityClass |
|
56 | - * @param EntityMap $entityMap |
|
57 | - * @return Mapper |
|
58 | - */ |
|
59 | - public function make($entityClass, EntityMap $entityMap) |
|
60 | - { |
|
61 | - $driver = $entityMap->getDriver(); |
|
52 | + /** |
|
53 | + * Return a new Mapper instance |
|
54 | + * |
|
55 | + * @param string $entityClass |
|
56 | + * @param EntityMap $entityMap |
|
57 | + * @return Mapper |
|
58 | + */ |
|
59 | + public function make($entityClass, EntityMap $entityMap) |
|
60 | + { |
|
61 | + $driver = $entityMap->getDriver(); |
|
62 | 62 | |
63 | - $connection = $entityMap->getConnection(); |
|
63 | + $connection = $entityMap->getConnection(); |
|
64 | 64 | |
65 | - $adapter = $this->drivers->getAdapter($driver, $connection); |
|
65 | + $adapter = $this->drivers->getAdapter($driver, $connection); |
|
66 | 66 | |
67 | - $entityMap->setDateFormat($adapter->getDateFormat()); |
|
67 | + $entityMap->setDateFormat($adapter->getDateFormat()); |
|
68 | 68 | |
69 | - $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
69 | + $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
70 | 70 | |
71 | - // Fire Initializing Event |
|
72 | - $mapper->fireEvent('initializing', $mapper); |
|
71 | + // Fire Initializing Event |
|
72 | + $mapper->fireEvent('initializing', $mapper); |
|
73 | 73 | |
74 | - // Proceed necessary parsing on the EntityMap object |
|
75 | - $entityMap->initialize(); |
|
74 | + // Proceed necessary parsing on the EntityMap object |
|
75 | + $entityMap->initialize(); |
|
76 | 76 | |
77 | - // Fire Initialized Event |
|
78 | - $mapper->fireEvent('initialized', $mapper); |
|
77 | + // Fire Initialized Event |
|
78 | + $mapper->fireEvent('initialized', $mapper); |
|
79 | 79 | |
80 | - return $mapper; |
|
81 | - } |
|
80 | + return $mapper; |
|
81 | + } |
|
82 | 82 | } |
@@ -11,194 +11,194 @@ |
||
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(array $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(array $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 | } |
@@ -152,13 +152,13 @@ |
||
152 | 152 | $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass); |
153 | 153 | |
154 | 154 | foreach ($embeddedAttributes as $key) { |
155 | - $prefix = snake_case(class_basename($valueClass)) . '_'; |
|
155 | + $prefix = snake_case(class_basename($valueClass)).'_'; |
|
156 | 156 | |
157 | 157 | $voWrapper = $this->factory->make($valueObject); |
158 | 158 | |
159 | - $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]); |
|
159 | + $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]); |
|
160 | 160 | |
161 | - unset($attributes[$prefix . $key]); |
|
161 | + unset($attributes[$prefix.$key]); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | $attributes[$localKey] = $valueObject; |