@@ -4,48 +4,48 @@ |
||
4 | 4 | |
5 | 5 | interface DBAdapter |
6 | 6 | { |
7 | - /** |
|
8 | - * Return's Driver specific Query Implementation. |
|
9 | - * |
|
10 | - * @return \Illuminate\Database\Query\Builder |
|
11 | - */ |
|
12 | - public function getQuery(); |
|
7 | + /** |
|
8 | + * Return's Driver specific Query Implementation. |
|
9 | + * |
|
10 | + * @return \Illuminate\Database\Query\Builder |
|
11 | + */ |
|
12 | + public function getQuery(); |
|
13 | 13 | |
14 | - /** |
|
15 | - * Return the Date format used on this adapter. |
|
16 | - * |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - public function getDateFormat(): string; |
|
14 | + /** |
|
15 | + * Return the Date format used on this adapter. |
|
16 | + * |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + public function getDateFormat(): string; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Start a DB transaction on driver that supports it. |
|
23 | - * |
|
24 | - * @return void |
|
25 | - */ |
|
26 | - public function beginTransaction(); |
|
21 | + /** |
|
22 | + * Start a DB transaction on driver that supports it. |
|
23 | + * |
|
24 | + * @return void |
|
25 | + */ |
|
26 | + public function beginTransaction(); |
|
27 | 27 | |
28 | - /** |
|
29 | - * Commit a DB transaction on driver that supports it. |
|
30 | - * |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - public function commit(); |
|
28 | + /** |
|
29 | + * Commit a DB transaction on driver that supports it. |
|
30 | + * |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + public function commit(); |
|
34 | 34 | |
35 | - /** |
|
36 | - * Rollback a DB transaction on driver that supports it. |
|
37 | - * |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public function rollback(); |
|
35 | + /** |
|
36 | + * Rollback a DB transaction on driver that supports it. |
|
37 | + * |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public function rollback(); |
|
41 | 41 | |
42 | - /** |
|
43 | - * Parse result from database and return an array of results |
|
44 | - * casted to analogue compatible values. |
|
45 | - * |
|
46 | - * @param array $rows |
|
47 | - * |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - public function fromDatabase(array $rows): array; |
|
42 | + /** |
|
43 | + * Parse result from database and return an array of results |
|
44 | + * casted to analogue compatible values. |
|
45 | + * |
|
46 | + * @param array $rows |
|
47 | + * |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + public function fromDatabase(array $rows): array; |
|
51 | 51 | } |
@@ -4,38 +4,38 @@ |
||
4 | 4 | |
5 | 5 | class IlluminateDriver implements DriverInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The Illuminate Connection Provider. |
|
9 | - * |
|
10 | - * @var CapsuleConnectionProvider|IlluminateConnectionProvider |
|
11 | - */ |
|
12 | - protected $connectionProvider; |
|
7 | + /** |
|
8 | + * The Illuminate Connection Provider. |
|
9 | + * |
|
10 | + * @var CapsuleConnectionProvider|IlluminateConnectionProvider |
|
11 | + */ |
|
12 | + protected $connectionProvider; |
|
13 | 13 | |
14 | - /** |
|
15 | - * IlluminateDriver constructor. |
|
16 | - * |
|
17 | - * @param $connectionProvider |
|
18 | - */ |
|
19 | - public function __construct($connectionProvider) |
|
20 | - { |
|
21 | - $this->connectionProvider = $connectionProvider; |
|
22 | - } |
|
14 | + /** |
|
15 | + * IlluminateDriver constructor. |
|
16 | + * |
|
17 | + * @param $connectionProvider |
|
18 | + */ |
|
19 | + public function __construct($connectionProvider) |
|
20 | + { |
|
21 | + $this->connectionProvider = $connectionProvider; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - public function getName(): string |
|
28 | - { |
|
29 | - return 'illuminate'; |
|
30 | - } |
|
24 | + /** |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + public function getName(): string |
|
28 | + { |
|
29 | + return 'illuminate'; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * {@inheritdoc} |
|
34 | - */ |
|
35 | - public function getAdapter(string $connection = null): DBAdapter |
|
36 | - { |
|
37 | - $connection = $this->connectionProvider->connection($connection); |
|
32 | + /** |
|
33 | + * {@inheritdoc} |
|
34 | + */ |
|
35 | + public function getAdapter(string $connection = null): DBAdapter |
|
36 | + { |
|
37 | + $connection = $this->connectionProvider->connection($connection); |
|
38 | 38 | |
39 | - return new IlluminateDBAdapter($connection); |
|
40 | - } |
|
39 | + return new IlluminateDBAdapter($connection); |
|
40 | + } |
|
41 | 41 | } |
@@ -4,37 +4,37 @@ |
||
4 | 4 | |
5 | 5 | class Manager |
6 | 6 | { |
7 | - /** |
|
8 | - * Drivers. |
|
9 | - * |
|
10 | - * @var DriverInterface[] |
|
11 | - */ |
|
12 | - protected $drivers = []; |
|
7 | + /** |
|
8 | + * Drivers. |
|
9 | + * |
|
10 | + * @var DriverInterface[] |
|
11 | + */ |
|
12 | + protected $drivers = []; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Add a Mapping Driver. |
|
16 | - * |
|
17 | - * @param DriverInterface $driver |
|
18 | - * |
|
19 | - * @return void |
|
20 | - */ |
|
21 | - public function addDriver(DriverInterface $driver) |
|
22 | - { |
|
23 | - $this->drivers[$driver->getName()] = $driver; |
|
24 | - } |
|
14 | + /** |
|
15 | + * Add a Mapping Driver. |
|
16 | + * |
|
17 | + * @param DriverInterface $driver |
|
18 | + * |
|
19 | + * @return void |
|
20 | + */ |
|
21 | + public function addDriver(DriverInterface $driver) |
|
22 | + { |
|
23 | + $this->drivers[$driver->getName()] = $driver; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Get the DBAdapter. |
|
28 | - * |
|
29 | - * @param string $driver |
|
30 | - * @param string $connection connection name for drivers supporting multiple connection. |
|
31 | - * |
|
32 | - * @return DBAdapter |
|
33 | - */ |
|
34 | - public function getAdapter(string $driver, string $connection = null) |
|
35 | - { |
|
36 | - if (array_key_exists($driver, $this->drivers)) { |
|
37 | - return $this->drivers[$driver]->getAdapter($connection); |
|
38 | - } |
|
39 | - } |
|
26 | + /** |
|
27 | + * Get the DBAdapter. |
|
28 | + * |
|
29 | + * @param string $driver |
|
30 | + * @param string $connection connection name for drivers supporting multiple connection. |
|
31 | + * |
|
32 | + * @return DBAdapter |
|
33 | + */ |
|
34 | + public function getAdapter(string $driver, string $connection = null) |
|
35 | + { |
|
36 | + if (array_key_exists($driver, $this->drivers)) { |
|
37 | + return $this->drivers[$driver]->getAdapter($connection); |
|
38 | + } |
|
39 | + } |
|
40 | 40 | } |
@@ -6,162 +6,162 @@ |
||
6 | 6 | |
7 | 7 | class Pivot extends Entity |
8 | 8 | { |
9 | - /** |
|
10 | - * @var bool |
|
11 | - */ |
|
12 | - protected $exists; |
|
13 | - |
|
14 | - /** |
|
15 | - * Pivot's table. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $table; |
|
20 | - |
|
21 | - /** |
|
22 | - * The parent entity of the relationship. |
|
23 | - * |
|
24 | - * @var object |
|
25 | - */ |
|
26 | - protected $parent; |
|
27 | - |
|
28 | - /** |
|
29 | - * The parent entity of the relationship. |
|
30 | - * |
|
31 | - * @var \Analogue\ORM\EntityMap |
|
32 | - */ |
|
33 | - protected $parentMap; |
|
34 | - |
|
35 | - /** |
|
36 | - * We may define a pivot mapping to deal with |
|
37 | - * soft deletes, timestamps, etc. |
|
38 | - * |
|
39 | - * @var \Analogue\ORM\EntityMap |
|
40 | - */ |
|
41 | - protected $pivotMap; |
|
42 | - |
|
43 | - /** |
|
44 | - * The name of the foreign key column. |
|
45 | - * |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $foreignKey; |
|
49 | - |
|
50 | - /** |
|
51 | - * The name of the "other key" column. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - protected $otherKey; |
|
56 | - |
|
57 | - /** |
|
58 | - * The attributes that aren't mass assignable. |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - protected $guarded = []; |
|
63 | - |
|
64 | - /** |
|
65 | - * Pivot uses timestamps ? |
|
66 | - * |
|
67 | - * @var bool |
|
68 | - */ |
|
69 | - protected $timestamps; |
|
70 | - |
|
71 | - /** |
|
72 | - * Create a new pivot model instance. |
|
73 | - * |
|
74 | - * @param \Analogue\ORM\System\InternallyMappable $parent |
|
75 | - * @param \Analogue\ORM\EntityMap $parentMap |
|
76 | - * @param array $attributes |
|
77 | - * @param string $table |
|
78 | - * @param bool $exists |
|
79 | - */ |
|
80 | - public function __construct($parent, $parentMap, $attributes, $table, $exists = false) |
|
81 | - { |
|
82 | - // The pivot model is a "dynamic" model since we will set the tables dynamically |
|
83 | - // for the instance. This allows it work for any intermediate tables for the |
|
84 | - // many to many relationship that are defined by this developer's classes. |
|
85 | - $this->setEntityAttributes($attributes); |
|
86 | - |
|
87 | - $this->table = $table; |
|
88 | - |
|
89 | - // We store off the parent instance so we will access the timestamp column names |
|
90 | - // for the model, since the pivot model timestamps aren't easily configurable |
|
91 | - // from the developer's point of view. We can use the parents to get these. |
|
92 | - $this->parent = $parent; |
|
93 | - |
|
94 | - $this->parentMap = $parentMap; |
|
95 | - |
|
96 | - $this->exists = $exists; |
|
97 | - |
|
98 | - $this->timestamps = $this->hasTimestampAttributes(); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Get the foreign key column name. |
|
103 | - * |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function getForeignKey() |
|
107 | - { |
|
108 | - return $this->foreignKey; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Get the "other key" column name. |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function getOtherKey() |
|
117 | - { |
|
118 | - return $this->otherKey; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Set the key names for the pivot model instance. |
|
123 | - * |
|
124 | - * @param string $foreignKey |
|
125 | - * @param string $otherKey |
|
126 | - * |
|
127 | - * @return $this |
|
128 | - */ |
|
129 | - public function setPivotKeys($foreignKey, $otherKey) |
|
130 | - { |
|
131 | - $this->foreignKey = $foreignKey; |
|
132 | - |
|
133 | - $this->otherKey = $otherKey; |
|
134 | - |
|
135 | - return $this; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Determine if the pivot model has timestamp attributes. |
|
140 | - * |
|
141 | - * @return bool |
|
142 | - */ |
|
143 | - public function hasTimestampAttributes() |
|
144 | - { |
|
145 | - return array_key_exists($this->getCreatedAtColumn(), $this->attributes); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Get the name of the "created at" column. |
|
150 | - * |
|
151 | - * @return string |
|
152 | - */ |
|
153 | - public function getCreatedAtColumn() |
|
154 | - { |
|
155 | - return $this->parentMap->getCreatedAtColumn(); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Get the name of the "updated at" column. |
|
160 | - * |
|
161 | - * @return string |
|
162 | - */ |
|
163 | - public function getUpdatedAtColumn() |
|
164 | - { |
|
165 | - return $this->parentMap->getUpdatedAtColumn(); |
|
166 | - } |
|
9 | + /** |
|
10 | + * @var bool |
|
11 | + */ |
|
12 | + protected $exists; |
|
13 | + |
|
14 | + /** |
|
15 | + * Pivot's table. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $table; |
|
20 | + |
|
21 | + /** |
|
22 | + * The parent entity of the relationship. |
|
23 | + * |
|
24 | + * @var object |
|
25 | + */ |
|
26 | + protected $parent; |
|
27 | + |
|
28 | + /** |
|
29 | + * The parent entity of the relationship. |
|
30 | + * |
|
31 | + * @var \Analogue\ORM\EntityMap |
|
32 | + */ |
|
33 | + protected $parentMap; |
|
34 | + |
|
35 | + /** |
|
36 | + * We may define a pivot mapping to deal with |
|
37 | + * soft deletes, timestamps, etc. |
|
38 | + * |
|
39 | + * @var \Analogue\ORM\EntityMap |
|
40 | + */ |
|
41 | + protected $pivotMap; |
|
42 | + |
|
43 | + /** |
|
44 | + * The name of the foreign key column. |
|
45 | + * |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $foreignKey; |
|
49 | + |
|
50 | + /** |
|
51 | + * The name of the "other key" column. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + protected $otherKey; |
|
56 | + |
|
57 | + /** |
|
58 | + * The attributes that aren't mass assignable. |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + protected $guarded = []; |
|
63 | + |
|
64 | + /** |
|
65 | + * Pivot uses timestamps ? |
|
66 | + * |
|
67 | + * @var bool |
|
68 | + */ |
|
69 | + protected $timestamps; |
|
70 | + |
|
71 | + /** |
|
72 | + * Create a new pivot model instance. |
|
73 | + * |
|
74 | + * @param \Analogue\ORM\System\InternallyMappable $parent |
|
75 | + * @param \Analogue\ORM\EntityMap $parentMap |
|
76 | + * @param array $attributes |
|
77 | + * @param string $table |
|
78 | + * @param bool $exists |
|
79 | + */ |
|
80 | + public function __construct($parent, $parentMap, $attributes, $table, $exists = false) |
|
81 | + { |
|
82 | + // The pivot model is a "dynamic" model since we will set the tables dynamically |
|
83 | + // for the instance. This allows it work for any intermediate tables for the |
|
84 | + // many to many relationship that are defined by this developer's classes. |
|
85 | + $this->setEntityAttributes($attributes); |
|
86 | + |
|
87 | + $this->table = $table; |
|
88 | + |
|
89 | + // We store off the parent instance so we will access the timestamp column names |
|
90 | + // for the model, since the pivot model timestamps aren't easily configurable |
|
91 | + // from the developer's point of view. We can use the parents to get these. |
|
92 | + $this->parent = $parent; |
|
93 | + |
|
94 | + $this->parentMap = $parentMap; |
|
95 | + |
|
96 | + $this->exists = $exists; |
|
97 | + |
|
98 | + $this->timestamps = $this->hasTimestampAttributes(); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Get the foreign key column name. |
|
103 | + * |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function getForeignKey() |
|
107 | + { |
|
108 | + return $this->foreignKey; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Get the "other key" column name. |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function getOtherKey() |
|
117 | + { |
|
118 | + return $this->otherKey; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Set the key names for the pivot model instance. |
|
123 | + * |
|
124 | + * @param string $foreignKey |
|
125 | + * @param string $otherKey |
|
126 | + * |
|
127 | + * @return $this |
|
128 | + */ |
|
129 | + public function setPivotKeys($foreignKey, $otherKey) |
|
130 | + { |
|
131 | + $this->foreignKey = $foreignKey; |
|
132 | + |
|
133 | + $this->otherKey = $otherKey; |
|
134 | + |
|
135 | + return $this; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Determine if the pivot model has timestamp attributes. |
|
140 | + * |
|
141 | + * @return bool |
|
142 | + */ |
|
143 | + public function hasTimestampAttributes() |
|
144 | + { |
|
145 | + return array_key_exists($this->getCreatedAtColumn(), $this->attributes); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Get the name of the "created at" column. |
|
150 | + * |
|
151 | + * @return string |
|
152 | + */ |
|
153 | + public function getCreatedAtColumn() |
|
154 | + { |
|
155 | + return $this->parentMap->getCreatedAtColumn(); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Get the name of the "updated at" column. |
|
160 | + * |
|
161 | + * @return string |
|
162 | + */ |
|
163 | + public function getUpdatedAtColumn() |
|
164 | + { |
|
165 | + return $this->parentMap->getUpdatedAtColumn(); |
|
166 | + } |
|
167 | 167 | } |
@@ -4,32 +4,32 @@ |
||
4 | 4 | |
5 | 5 | class HasMany extends HasOneOrMany |
6 | 6 | { |
7 | - /** |
|
8 | - * Lazy-Load the results of the relationship. |
|
9 | - * |
|
10 | - * @param $relation |
|
11 | - * |
|
12 | - * @return mixed |
|
13 | - */ |
|
14 | - public function getResults($relation) |
|
15 | - { |
|
16 | - $results = $this->query->get(); |
|
7 | + /** |
|
8 | + * Lazy-Load the results of the relationship. |
|
9 | + * |
|
10 | + * @param $relation |
|
11 | + * |
|
12 | + * @return mixed |
|
13 | + */ |
|
14 | + public function getResults($relation) |
|
15 | + { |
|
16 | + $results = $this->query->get(); |
|
17 | 17 | |
18 | - $this->cacheRelation($results, $relation); |
|
18 | + $this->cacheRelation($results, $relation); |
|
19 | 19 | |
20 | - return $results; |
|
21 | - } |
|
20 | + return $results; |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Match the eagerly loaded results to their parents. |
|
25 | - * |
|
26 | - * @param array $results |
|
27 | - * @param string $relation |
|
28 | - * |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public function match(array $results, $relation) |
|
32 | - { |
|
33 | - return $this->matchMany($results, $relation); |
|
34 | - } |
|
23 | + /** |
|
24 | + * Match the eagerly loaded results to their parents. |
|
25 | + * |
|
26 | + * @param array $results |
|
27 | + * @param string $relation |
|
28 | + * |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public function match(array $results, $relation) |
|
32 | + { |
|
33 | + return $this->matchMany($results, $relation); |
|
34 | + } |
|
35 | 35 | } |
@@ -66,7 +66,7 @@ |
||
66 | 66 | */ |
67 | 67 | protected function buildEmbeddedCollection(array $rows): Collection |
68 | 68 | { |
69 | - return collect($rows)->map(function ($attributes) { |
|
69 | + return collect($rows)->map(function($attributes) { |
|
70 | 70 | return $this->buildEmbeddedObject($attributes); |
71 | 71 | }); |
72 | 72 | } |
@@ -7,124 +7,124 @@ |
||
7 | 7 | |
8 | 8 | class EmbedsMany extends EmbedsOne |
9 | 9 | { |
10 | - /** |
|
11 | - * Match a single database row's attributes to a single |
|
12 | - * object, and return the updated attributes. |
|
13 | - * |
|
14 | - * @param array $attributes |
|
15 | - * |
|
16 | - * @throws MappingException |
|
17 | - * |
|
18 | - * @return array |
|
19 | - */ |
|
20 | - public function matchSingleResult(array $attributes) : array |
|
21 | - { |
|
22 | - $column = $this->relation; |
|
23 | - |
|
24 | - if (!$this->asArray) { |
|
25 | - throw new MappingException("column '$column' should be of type array or json"); |
|
26 | - } |
|
27 | - |
|
28 | - return $this->matchAsArray($attributes); |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * Match array attribute from parent to an embedded object, |
|
33 | - * and return the updated attributes. |
|
34 | - * |
|
35 | - * @param array $attributes |
|
36 | - * |
|
37 | - * @throws MappingException |
|
38 | - * |
|
39 | - * @return array |
|
40 | - */ |
|
41 | - protected function matchAsArray(array $attributes) : array |
|
42 | - { |
|
43 | - // Extract the attributes with the key of the relation, |
|
44 | - // which should be an array. |
|
45 | - $key = $this->relation; |
|
46 | - |
|
47 | - if (!array_key_exists($key, $attributes)) { |
|
48 | - $attributes[$key] = $this->asJson ? json_encode([]) : []; |
|
49 | - } |
|
50 | - |
|
51 | - if ($this->asJson) { |
|
52 | - $attributes[$key] = json_decode($attributes[$key], true); |
|
53 | - } |
|
54 | - |
|
55 | - if (!is_array($attributes[$key])) { |
|
56 | - throw new MappingException("'$key' column should be an array, actual :".$attributes[$key]); |
|
57 | - } |
|
58 | - |
|
59 | - $attributes[$key] = $this->buildEmbeddedCollection($attributes[$key]); |
|
60 | - |
|
61 | - return $attributes; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Build an embedded collection and returns it. |
|
66 | - * |
|
67 | - * @param array $rows |
|
68 | - * |
|
69 | - * @return Collection |
|
70 | - */ |
|
71 | - protected function buildEmbeddedCollection(array $rows): Collection |
|
72 | - { |
|
73 | - return collect($rows)->map(function ($attributes) { |
|
74 | - return $this->buildEmbeddedObject($attributes); |
|
75 | - }); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Transform embedded object into db column(s). |
|
80 | - * |
|
81 | - * @param mixed $objects |
|
82 | - * |
|
83 | - * @throws MappingException |
|
84 | - * |
|
85 | - * @return array $columns |
|
86 | - */ |
|
87 | - public function normalize($objects) : array |
|
88 | - { |
|
89 | - if (!$this->asArray) { |
|
90 | - throw new MappingException('Cannot normalize an embedsMany relation as row columns'); |
|
91 | - } |
|
92 | - |
|
93 | - return $this->normalizeAsArray($objects); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Normalize object as array containing raw attributes. |
|
98 | - * |
|
99 | - * @param mixed $objects |
|
100 | - * |
|
101 | - * @throws MappingException |
|
102 | - * |
|
103 | - * @return array |
|
104 | - */ |
|
105 | - protected function normalizeAsArray($objects) : array |
|
106 | - { |
|
107 | - $key = $this->relation; |
|
108 | - |
|
109 | - if (!is_array($objects) && !$objects instanceof Collection) { |
|
110 | - throw new MappingException("column '$key' should be of type array or collection"); |
|
111 | - } |
|
112 | - |
|
113 | - if ($objects instanceof Collection) { |
|
114 | - $objects = $objects->all(); |
|
115 | - } |
|
116 | - |
|
117 | - $normalizedObjects = []; |
|
118 | - |
|
119 | - foreach ($objects as $object) { |
|
120 | - $wrapper = $this->factory->make($object); |
|
121 | - $normalizedObjects[] = $wrapper->getEntityAttributes(); |
|
122 | - } |
|
123 | - |
|
124 | - if ($this->asJson) { |
|
125 | - $normalizedObjects = json_encode($normalizedObjects); |
|
126 | - } |
|
127 | - |
|
128 | - return [$key => $normalizedObjects]; |
|
129 | - } |
|
10 | + /** |
|
11 | + * Match a single database row's attributes to a single |
|
12 | + * object, and return the updated attributes. |
|
13 | + * |
|
14 | + * @param array $attributes |
|
15 | + * |
|
16 | + * @throws MappingException |
|
17 | + * |
|
18 | + * @return array |
|
19 | + */ |
|
20 | + public function matchSingleResult(array $attributes) : array |
|
21 | + { |
|
22 | + $column = $this->relation; |
|
23 | + |
|
24 | + if (!$this->asArray) { |
|
25 | + throw new MappingException("column '$column' should be of type array or json"); |
|
26 | + } |
|
27 | + |
|
28 | + return $this->matchAsArray($attributes); |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * Match array attribute from parent to an embedded object, |
|
33 | + * and return the updated attributes. |
|
34 | + * |
|
35 | + * @param array $attributes |
|
36 | + * |
|
37 | + * @throws MappingException |
|
38 | + * |
|
39 | + * @return array |
|
40 | + */ |
|
41 | + protected function matchAsArray(array $attributes) : array |
|
42 | + { |
|
43 | + // Extract the attributes with the key of the relation, |
|
44 | + // which should be an array. |
|
45 | + $key = $this->relation; |
|
46 | + |
|
47 | + if (!array_key_exists($key, $attributes)) { |
|
48 | + $attributes[$key] = $this->asJson ? json_encode([]) : []; |
|
49 | + } |
|
50 | + |
|
51 | + if ($this->asJson) { |
|
52 | + $attributes[$key] = json_decode($attributes[$key], true); |
|
53 | + } |
|
54 | + |
|
55 | + if (!is_array($attributes[$key])) { |
|
56 | + throw new MappingException("'$key' column should be an array, actual :".$attributes[$key]); |
|
57 | + } |
|
58 | + |
|
59 | + $attributes[$key] = $this->buildEmbeddedCollection($attributes[$key]); |
|
60 | + |
|
61 | + return $attributes; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Build an embedded collection and returns it. |
|
66 | + * |
|
67 | + * @param array $rows |
|
68 | + * |
|
69 | + * @return Collection |
|
70 | + */ |
|
71 | + protected function buildEmbeddedCollection(array $rows): Collection |
|
72 | + { |
|
73 | + return collect($rows)->map(function ($attributes) { |
|
74 | + return $this->buildEmbeddedObject($attributes); |
|
75 | + }); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Transform embedded object into db column(s). |
|
80 | + * |
|
81 | + * @param mixed $objects |
|
82 | + * |
|
83 | + * @throws MappingException |
|
84 | + * |
|
85 | + * @return array $columns |
|
86 | + */ |
|
87 | + public function normalize($objects) : array |
|
88 | + { |
|
89 | + if (!$this->asArray) { |
|
90 | + throw new MappingException('Cannot normalize an embedsMany relation as row columns'); |
|
91 | + } |
|
92 | + |
|
93 | + return $this->normalizeAsArray($objects); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Normalize object as array containing raw attributes. |
|
98 | + * |
|
99 | + * @param mixed $objects |
|
100 | + * |
|
101 | + * @throws MappingException |
|
102 | + * |
|
103 | + * @return array |
|
104 | + */ |
|
105 | + protected function normalizeAsArray($objects) : array |
|
106 | + { |
|
107 | + $key = $this->relation; |
|
108 | + |
|
109 | + if (!is_array($objects) && !$objects instanceof Collection) { |
|
110 | + throw new MappingException("column '$key' should be of type array or collection"); |
|
111 | + } |
|
112 | + |
|
113 | + if ($objects instanceof Collection) { |
|
114 | + $objects = $objects->all(); |
|
115 | + } |
|
116 | + |
|
117 | + $normalizedObjects = []; |
|
118 | + |
|
119 | + foreach ($objects as $object) { |
|
120 | + $wrapper = $this->factory->make($object); |
|
121 | + $normalizedObjects[] = $wrapper->getEntityAttributes(); |
|
122 | + } |
|
123 | + |
|
124 | + if ($this->asJson) { |
|
125 | + $normalizedObjects = json_encode($normalizedObjects); |
|
126 | + } |
|
127 | + |
|
128 | + return [$key => $normalizedObjects]; |
|
129 | + } |
|
130 | 130 | } |
@@ -19,435 +19,435 @@ |
||
19 | 19 | */ |
20 | 20 | abstract class Relationship |
21 | 21 | { |
22 | - /** |
|
23 | - * The mapper instance for the related entity. |
|
24 | - * |
|
25 | - * @var Mapper |
|
26 | - */ |
|
27 | - protected $relatedMapper; |
|
28 | - |
|
29 | - /** |
|
30 | - * The Analogue Query Builder instance. |
|
31 | - * |
|
32 | - * @var Query |
|
33 | - */ |
|
34 | - protected $query; |
|
35 | - |
|
36 | - /** |
|
37 | - * The parent entity proxy instance. |
|
38 | - * |
|
39 | - * @var InternallyMappable |
|
40 | - */ |
|
41 | - protected $parent; |
|
42 | - |
|
43 | - /** |
|
44 | - * The parent entity map. |
|
45 | - * |
|
46 | - * @var \Analogue\ORM\EntityMap |
|
47 | - */ |
|
48 | - protected $parentMap; |
|
49 | - |
|
50 | - /** |
|
51 | - * The Parent Mapper instance. |
|
52 | - * |
|
53 | - * @var Mapper |
|
54 | - */ |
|
55 | - protected $parentMapper; |
|
56 | - |
|
57 | - /** |
|
58 | - * The related entity instance. |
|
59 | - * |
|
60 | - * @var object |
|
61 | - */ |
|
62 | - protected $related; |
|
63 | - |
|
64 | - /** |
|
65 | - * The related entity Map. |
|
66 | - * |
|
67 | - * @var \Analogue\ORM\EntityMap |
|
68 | - */ |
|
69 | - protected $relatedMap; |
|
70 | - |
|
71 | - /** |
|
72 | - * Indicate if the relationships use a pivot table.*. |
|
73 | - * |
|
74 | - * @var bool |
|
75 | - */ |
|
76 | - protected static $hasPivot = false; |
|
77 | - |
|
78 | - /** |
|
79 | - * Indicates if the relation is adding constraints. |
|
80 | - * |
|
81 | - * @var bool |
|
82 | - */ |
|
83 | - protected static $constraints = true; |
|
84 | - |
|
85 | - /** |
|
86 | - * Wrapper factory. |
|
87 | - * |
|
88 | - * @var \Analogue\ORM\System\Wrappers\Factory |
|
89 | - */ |
|
90 | - protected $factory; |
|
91 | - |
|
92 | - /** |
|
93 | - * Create a new relation instance. |
|
94 | - * |
|
95 | - * @param Mapper $mapper |
|
96 | - * @param mixed $parent |
|
97 | - * |
|
98 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
99 | - */ |
|
100 | - public function __construct(Mapper $mapper, $parent) |
|
101 | - { |
|
102 | - $this->relatedMapper = $mapper; |
|
103 | - |
|
104 | - $this->query = $mapper->getQuery(); |
|
105 | - |
|
106 | - $this->factory = new Factory(); |
|
107 | - |
|
108 | - $this->parent = $this->factory->make($parent); |
|
109 | - |
|
110 | - $this->parentMapper = $mapper->getManager()->getMapper($parent); |
|
111 | - |
|
112 | - $this->parentMap = $this->parentMapper->getEntityMap(); |
|
113 | - |
|
114 | - $this->related = $mapper->newInstance(); |
|
115 | - |
|
116 | - $this->relatedMap = $mapper->getEntityMap(); |
|
117 | - |
|
118 | - $this->addConstraints(); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Indicate if the relationship uses a pivot table. |
|
123 | - * |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - public function hasPivot() |
|
127 | - { |
|
128 | - return static::$hasPivot; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Set the base constraints on the relation query. |
|
133 | - * |
|
134 | - * @return void |
|
135 | - */ |
|
136 | - abstract public function addConstraints(); |
|
137 | - |
|
138 | - /** |
|
139 | - * Set the constraints for an eager load of the relation. |
|
140 | - * |
|
141 | - * @param array $results |
|
142 | - * |
|
143 | - * @return void |
|
144 | - */ |
|
145 | - abstract public function addEagerConstraints(array $results); |
|
146 | - |
|
147 | - /** |
|
148 | - * Match the eagerly loaded results to their parents, then return |
|
149 | - * updated results. |
|
150 | - * |
|
151 | - * @param array $results |
|
152 | - * @param string $relation |
|
153 | - * |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - abstract public function match(array $results, $relation); |
|
157 | - |
|
158 | - /** |
|
159 | - * Get the results of the relationship. |
|
160 | - * |
|
161 | - * @param string $relation relation name in parent's entity map |
|
162 | - * |
|
163 | - * @return mixed |
|
164 | - */ |
|
165 | - abstract public function getResults($relation); |
|
166 | - |
|
167 | - /** |
|
168 | - * Get the relationship for eager loading. |
|
169 | - * |
|
170 | - * @return \Illuminate\Support\Collection |
|
171 | - */ |
|
172 | - public function getEager() |
|
173 | - { |
|
174 | - return $this->get(); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Add the constraints for a relationship count query. |
|
179 | - * |
|
180 | - * @param Query $query |
|
181 | - * @param Query $parent |
|
182 | - * |
|
183 | - * @return Query |
|
184 | - */ |
|
185 | - public function getRelationCountQuery(Query $query, Query $parent) |
|
186 | - { |
|
187 | - $query->select(new Expression('count(*)')); |
|
188 | - |
|
189 | - $key = $this->wrap($this->getQualifiedParentKeyName()); |
|
190 | - |
|
191 | - return $query->where($this->getHasCompareKey(), '=', new Expression($key)); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Run a callback with constraints disabled on the relation. |
|
196 | - * |
|
197 | - * @param Closure $callback |
|
198 | - * |
|
199 | - * @return mixed |
|
200 | - */ |
|
201 | - public static function noConstraints(Closure $callback) |
|
202 | - { |
|
203 | - static::$constraints = false; |
|
204 | - |
|
205 | - // When resetting the relation where clause, we want to shift the first element |
|
206 | - // off of the bindings, leaving only the constraints that the developers put |
|
207 | - // as "extra" on the relationships, and not original relation constraints. |
|
208 | - $results = call_user_func($callback); |
|
209 | - |
|
210 | - static::$constraints = true; |
|
211 | - |
|
212 | - return $results; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Get all of the primary keys for an array of entities. |
|
217 | - * |
|
218 | - * @param array $entities |
|
219 | - * @param string $key |
|
220 | - * |
|
221 | - * @return array |
|
222 | - */ |
|
223 | - protected function getKeys(array $entities, $key = null) |
|
224 | - { |
|
225 | - if (is_null($key)) { |
|
226 | - $key = $this->relatedMap->getKeyName(); |
|
227 | - } |
|
228 | - |
|
229 | - $host = $this; |
|
230 | - |
|
231 | - return array_unique(array_values(array_map(function ($value) use ($key, $host) { |
|
232 | - if (!$value instanceof InternallyMappable) { |
|
233 | - $value = $host->factory->make($value); |
|
234 | - } |
|
235 | - |
|
236 | - return $value->getEntityAttribute($key); |
|
237 | - }, $entities))); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Get all the keys from a result set. |
|
242 | - * |
|
243 | - * @param array $results |
|
244 | - * @param string $key |
|
245 | - * |
|
246 | - * @return array |
|
247 | - */ |
|
248 | - protected function getKeysFromResults(array $results, $key = null) |
|
249 | - { |
|
250 | - if (is_null($key)) { |
|
251 | - $key = $this->parentMap->getKeyName(); |
|
252 | - } |
|
253 | - |
|
254 | - return array_unique(array_values(array_map(function ($value) use ($key) { |
|
255 | - return $value[$key]; |
|
256 | - }, $results))); |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Get the underlying query for the relation. |
|
261 | - * |
|
262 | - * @return Query |
|
263 | - */ |
|
264 | - public function getQuery() |
|
265 | - { |
|
266 | - return $this->query; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Get the base query builder. |
|
271 | - * |
|
272 | - * @return \Illuminate\Database\Query\Builder |
|
273 | - */ |
|
274 | - public function getBaseQuery() |
|
275 | - { |
|
276 | - return $this->query->getQuery(); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Get the parent model of the relation. |
|
281 | - * |
|
282 | - * @return InternallyMappable |
|
283 | - */ |
|
284 | - public function getParent() |
|
285 | - { |
|
286 | - return $this->parent; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Set the parent model of the relation. |
|
291 | - * |
|
292 | - * @param InternallyMappable $parent |
|
293 | - * |
|
294 | - * @return void |
|
295 | - */ |
|
296 | - public function setParent(InternallyMappable $parent) |
|
297 | - { |
|
298 | - $this->parent = $parent; |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Get the fully qualified parent key name. |
|
303 | - * |
|
304 | - * @return string |
|
305 | - */ |
|
306 | - protected function getQualifiedParentKeyName() |
|
307 | - { |
|
308 | - return $this->parent->getQualifiedKeyName(); |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Get the related entity of the relation. |
|
313 | - * |
|
314 | - * @return \Analogue\ORM\Entity |
|
315 | - */ |
|
316 | - public function getRelated() |
|
317 | - { |
|
318 | - return $this->related; |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * Get the related mapper for the relation. |
|
323 | - * |
|
324 | - * @return Mapper |
|
325 | - */ |
|
326 | - public function getRelatedMapper() |
|
327 | - { |
|
328 | - return $this->relatedMapper; |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * Get the name of the "created at" column. |
|
333 | - * |
|
334 | - * @return string |
|
335 | - */ |
|
336 | - public function createdAt() |
|
337 | - { |
|
338 | - return $this->parentMap->getCreatedAtColumn(); |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Get the name of the "updated at" column. |
|
343 | - * |
|
344 | - * @return string |
|
345 | - */ |
|
346 | - public function updatedAt() |
|
347 | - { |
|
348 | - return $this->parentMap->getUpdatedAtColumn(); |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Get the name of the related model's "updated at" column. |
|
353 | - * |
|
354 | - * @return string |
|
355 | - */ |
|
356 | - public function relatedUpdatedAt() |
|
357 | - { |
|
358 | - return $this->related->getUpdatedAtColumn(); |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Wrap the given value with the parent query's grammar. |
|
363 | - * |
|
364 | - * @param string $value |
|
365 | - * |
|
366 | - * @return string |
|
367 | - */ |
|
368 | - public function wrap($value) |
|
369 | - { |
|
370 | - return $this->parentMapper->getQuery()->getQuery()->getGrammar()->wrap($value); |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Get a fresh timestamp. |
|
375 | - * |
|
376 | - * @return Carbon |
|
377 | - */ |
|
378 | - protected function freshTimestamp() |
|
379 | - { |
|
380 | - return new Carbon(); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Cache the link between parent and related |
|
385 | - * into the mapper's Entity Cache. |
|
386 | - * |
|
387 | - * @param EntityCollection|Mappable $results result of the relation query |
|
388 | - * @param string $relation name of the relation method on the parent entity |
|
389 | - * |
|
390 | - * @return void |
|
391 | - */ |
|
392 | - protected function cacheRelation($results, $relation) |
|
393 | - { |
|
394 | - $cache = $this->parentMapper->getEntityCache(); |
|
395 | - |
|
396 | - $cache->cacheLoadedRelationResult($this->parent->getEntityKeyName(), $relation, $results, $this); |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * Return Pivot attributes when available on a relationship. |
|
401 | - * |
|
402 | - * @return array |
|
403 | - */ |
|
404 | - public function getPivotAttributes() |
|
405 | - { |
|
406 | - return []; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Get a combo type.primaryKey. |
|
411 | - * |
|
412 | - * @param Mappable $entity |
|
413 | - * |
|
414 | - * @return string |
|
415 | - */ |
|
416 | - protected function getEntityHash(Mappable $entity) |
|
417 | - { |
|
418 | - $class = get_class($entity); |
|
419 | - |
|
420 | - $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName(); |
|
421 | - |
|
422 | - return $class.'.'.$entity->getEntityAttribute($keyName); |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * Run synchronization content if needed by the |
|
427 | - * relation type. |
|
428 | - * |
|
429 | - * @param array $actualContent |
|
430 | - * |
|
431 | - * @return void |
|
432 | - */ |
|
433 | - abstract public function sync(array $actualContent); |
|
434 | - |
|
435 | - /** |
|
436 | - * Handle dynamic method calls to the relationship. |
|
437 | - * |
|
438 | - * @param string $method |
|
439 | - * @param array $parameters |
|
440 | - * |
|
441 | - * @return mixed |
|
442 | - */ |
|
443 | - public function __call($method, $parameters) |
|
444 | - { |
|
445 | - $result = call_user_func_array([$this->query, $method], $parameters); |
|
446 | - |
|
447 | - if ($result === $this->query) { |
|
448 | - return $this; |
|
449 | - } |
|
450 | - |
|
451 | - return $result; |
|
452 | - } |
|
22 | + /** |
|
23 | + * The mapper instance for the related entity. |
|
24 | + * |
|
25 | + * @var Mapper |
|
26 | + */ |
|
27 | + protected $relatedMapper; |
|
28 | + |
|
29 | + /** |
|
30 | + * The Analogue Query Builder instance. |
|
31 | + * |
|
32 | + * @var Query |
|
33 | + */ |
|
34 | + protected $query; |
|
35 | + |
|
36 | + /** |
|
37 | + * The parent entity proxy instance. |
|
38 | + * |
|
39 | + * @var InternallyMappable |
|
40 | + */ |
|
41 | + protected $parent; |
|
42 | + |
|
43 | + /** |
|
44 | + * The parent entity map. |
|
45 | + * |
|
46 | + * @var \Analogue\ORM\EntityMap |
|
47 | + */ |
|
48 | + protected $parentMap; |
|
49 | + |
|
50 | + /** |
|
51 | + * The Parent Mapper instance. |
|
52 | + * |
|
53 | + * @var Mapper |
|
54 | + */ |
|
55 | + protected $parentMapper; |
|
56 | + |
|
57 | + /** |
|
58 | + * The related entity instance. |
|
59 | + * |
|
60 | + * @var object |
|
61 | + */ |
|
62 | + protected $related; |
|
63 | + |
|
64 | + /** |
|
65 | + * The related entity Map. |
|
66 | + * |
|
67 | + * @var \Analogue\ORM\EntityMap |
|
68 | + */ |
|
69 | + protected $relatedMap; |
|
70 | + |
|
71 | + /** |
|
72 | + * Indicate if the relationships use a pivot table.*. |
|
73 | + * |
|
74 | + * @var bool |
|
75 | + */ |
|
76 | + protected static $hasPivot = false; |
|
77 | + |
|
78 | + /** |
|
79 | + * Indicates if the relation is adding constraints. |
|
80 | + * |
|
81 | + * @var bool |
|
82 | + */ |
|
83 | + protected static $constraints = true; |
|
84 | + |
|
85 | + /** |
|
86 | + * Wrapper factory. |
|
87 | + * |
|
88 | + * @var \Analogue\ORM\System\Wrappers\Factory |
|
89 | + */ |
|
90 | + protected $factory; |
|
91 | + |
|
92 | + /** |
|
93 | + * Create a new relation instance. |
|
94 | + * |
|
95 | + * @param Mapper $mapper |
|
96 | + * @param mixed $parent |
|
97 | + * |
|
98 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
99 | + */ |
|
100 | + public function __construct(Mapper $mapper, $parent) |
|
101 | + { |
|
102 | + $this->relatedMapper = $mapper; |
|
103 | + |
|
104 | + $this->query = $mapper->getQuery(); |
|
105 | + |
|
106 | + $this->factory = new Factory(); |
|
107 | + |
|
108 | + $this->parent = $this->factory->make($parent); |
|
109 | + |
|
110 | + $this->parentMapper = $mapper->getManager()->getMapper($parent); |
|
111 | + |
|
112 | + $this->parentMap = $this->parentMapper->getEntityMap(); |
|
113 | + |
|
114 | + $this->related = $mapper->newInstance(); |
|
115 | + |
|
116 | + $this->relatedMap = $mapper->getEntityMap(); |
|
117 | + |
|
118 | + $this->addConstraints(); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Indicate if the relationship uses a pivot table. |
|
123 | + * |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + public function hasPivot() |
|
127 | + { |
|
128 | + return static::$hasPivot; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Set the base constraints on the relation query. |
|
133 | + * |
|
134 | + * @return void |
|
135 | + */ |
|
136 | + abstract public function addConstraints(); |
|
137 | + |
|
138 | + /** |
|
139 | + * Set the constraints for an eager load of the relation. |
|
140 | + * |
|
141 | + * @param array $results |
|
142 | + * |
|
143 | + * @return void |
|
144 | + */ |
|
145 | + abstract public function addEagerConstraints(array $results); |
|
146 | + |
|
147 | + /** |
|
148 | + * Match the eagerly loaded results to their parents, then return |
|
149 | + * updated results. |
|
150 | + * |
|
151 | + * @param array $results |
|
152 | + * @param string $relation |
|
153 | + * |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + abstract public function match(array $results, $relation); |
|
157 | + |
|
158 | + /** |
|
159 | + * Get the results of the relationship. |
|
160 | + * |
|
161 | + * @param string $relation relation name in parent's entity map |
|
162 | + * |
|
163 | + * @return mixed |
|
164 | + */ |
|
165 | + abstract public function getResults($relation); |
|
166 | + |
|
167 | + /** |
|
168 | + * Get the relationship for eager loading. |
|
169 | + * |
|
170 | + * @return \Illuminate\Support\Collection |
|
171 | + */ |
|
172 | + public function getEager() |
|
173 | + { |
|
174 | + return $this->get(); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Add the constraints for a relationship count query. |
|
179 | + * |
|
180 | + * @param Query $query |
|
181 | + * @param Query $parent |
|
182 | + * |
|
183 | + * @return Query |
|
184 | + */ |
|
185 | + public function getRelationCountQuery(Query $query, Query $parent) |
|
186 | + { |
|
187 | + $query->select(new Expression('count(*)')); |
|
188 | + |
|
189 | + $key = $this->wrap($this->getQualifiedParentKeyName()); |
|
190 | + |
|
191 | + return $query->where($this->getHasCompareKey(), '=', new Expression($key)); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Run a callback with constraints disabled on the relation. |
|
196 | + * |
|
197 | + * @param Closure $callback |
|
198 | + * |
|
199 | + * @return mixed |
|
200 | + */ |
|
201 | + public static function noConstraints(Closure $callback) |
|
202 | + { |
|
203 | + static::$constraints = false; |
|
204 | + |
|
205 | + // When resetting the relation where clause, we want to shift the first element |
|
206 | + // off of the bindings, leaving only the constraints that the developers put |
|
207 | + // as "extra" on the relationships, and not original relation constraints. |
|
208 | + $results = call_user_func($callback); |
|
209 | + |
|
210 | + static::$constraints = true; |
|
211 | + |
|
212 | + return $results; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Get all of the primary keys for an array of entities. |
|
217 | + * |
|
218 | + * @param array $entities |
|
219 | + * @param string $key |
|
220 | + * |
|
221 | + * @return array |
|
222 | + */ |
|
223 | + protected function getKeys(array $entities, $key = null) |
|
224 | + { |
|
225 | + if (is_null($key)) { |
|
226 | + $key = $this->relatedMap->getKeyName(); |
|
227 | + } |
|
228 | + |
|
229 | + $host = $this; |
|
230 | + |
|
231 | + return array_unique(array_values(array_map(function ($value) use ($key, $host) { |
|
232 | + if (!$value instanceof InternallyMappable) { |
|
233 | + $value = $host->factory->make($value); |
|
234 | + } |
|
235 | + |
|
236 | + return $value->getEntityAttribute($key); |
|
237 | + }, $entities))); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Get all the keys from a result set. |
|
242 | + * |
|
243 | + * @param array $results |
|
244 | + * @param string $key |
|
245 | + * |
|
246 | + * @return array |
|
247 | + */ |
|
248 | + protected function getKeysFromResults(array $results, $key = null) |
|
249 | + { |
|
250 | + if (is_null($key)) { |
|
251 | + $key = $this->parentMap->getKeyName(); |
|
252 | + } |
|
253 | + |
|
254 | + return array_unique(array_values(array_map(function ($value) use ($key) { |
|
255 | + return $value[$key]; |
|
256 | + }, $results))); |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Get the underlying query for the relation. |
|
261 | + * |
|
262 | + * @return Query |
|
263 | + */ |
|
264 | + public function getQuery() |
|
265 | + { |
|
266 | + return $this->query; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Get the base query builder. |
|
271 | + * |
|
272 | + * @return \Illuminate\Database\Query\Builder |
|
273 | + */ |
|
274 | + public function getBaseQuery() |
|
275 | + { |
|
276 | + return $this->query->getQuery(); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Get the parent model of the relation. |
|
281 | + * |
|
282 | + * @return InternallyMappable |
|
283 | + */ |
|
284 | + public function getParent() |
|
285 | + { |
|
286 | + return $this->parent; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Set the parent model of the relation. |
|
291 | + * |
|
292 | + * @param InternallyMappable $parent |
|
293 | + * |
|
294 | + * @return void |
|
295 | + */ |
|
296 | + public function setParent(InternallyMappable $parent) |
|
297 | + { |
|
298 | + $this->parent = $parent; |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Get the fully qualified parent key name. |
|
303 | + * |
|
304 | + * @return string |
|
305 | + */ |
|
306 | + protected function getQualifiedParentKeyName() |
|
307 | + { |
|
308 | + return $this->parent->getQualifiedKeyName(); |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Get the related entity of the relation. |
|
313 | + * |
|
314 | + * @return \Analogue\ORM\Entity |
|
315 | + */ |
|
316 | + public function getRelated() |
|
317 | + { |
|
318 | + return $this->related; |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * Get the related mapper for the relation. |
|
323 | + * |
|
324 | + * @return Mapper |
|
325 | + */ |
|
326 | + public function getRelatedMapper() |
|
327 | + { |
|
328 | + return $this->relatedMapper; |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * Get the name of the "created at" column. |
|
333 | + * |
|
334 | + * @return string |
|
335 | + */ |
|
336 | + public function createdAt() |
|
337 | + { |
|
338 | + return $this->parentMap->getCreatedAtColumn(); |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Get the name of the "updated at" column. |
|
343 | + * |
|
344 | + * @return string |
|
345 | + */ |
|
346 | + public function updatedAt() |
|
347 | + { |
|
348 | + return $this->parentMap->getUpdatedAtColumn(); |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Get the name of the related model's "updated at" column. |
|
353 | + * |
|
354 | + * @return string |
|
355 | + */ |
|
356 | + public function relatedUpdatedAt() |
|
357 | + { |
|
358 | + return $this->related->getUpdatedAtColumn(); |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Wrap the given value with the parent query's grammar. |
|
363 | + * |
|
364 | + * @param string $value |
|
365 | + * |
|
366 | + * @return string |
|
367 | + */ |
|
368 | + public function wrap($value) |
|
369 | + { |
|
370 | + return $this->parentMapper->getQuery()->getQuery()->getGrammar()->wrap($value); |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Get a fresh timestamp. |
|
375 | + * |
|
376 | + * @return Carbon |
|
377 | + */ |
|
378 | + protected function freshTimestamp() |
|
379 | + { |
|
380 | + return new Carbon(); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Cache the link between parent and related |
|
385 | + * into the mapper's Entity Cache. |
|
386 | + * |
|
387 | + * @param EntityCollection|Mappable $results result of the relation query |
|
388 | + * @param string $relation name of the relation method on the parent entity |
|
389 | + * |
|
390 | + * @return void |
|
391 | + */ |
|
392 | + protected function cacheRelation($results, $relation) |
|
393 | + { |
|
394 | + $cache = $this->parentMapper->getEntityCache(); |
|
395 | + |
|
396 | + $cache->cacheLoadedRelationResult($this->parent->getEntityKeyName(), $relation, $results, $this); |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * Return Pivot attributes when available on a relationship. |
|
401 | + * |
|
402 | + * @return array |
|
403 | + */ |
|
404 | + public function getPivotAttributes() |
|
405 | + { |
|
406 | + return []; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Get a combo type.primaryKey. |
|
411 | + * |
|
412 | + * @param Mappable $entity |
|
413 | + * |
|
414 | + * @return string |
|
415 | + */ |
|
416 | + protected function getEntityHash(Mappable $entity) |
|
417 | + { |
|
418 | + $class = get_class($entity); |
|
419 | + |
|
420 | + $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName(); |
|
421 | + |
|
422 | + return $class.'.'.$entity->getEntityAttribute($keyName); |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * Run synchronization content if needed by the |
|
427 | + * relation type. |
|
428 | + * |
|
429 | + * @param array $actualContent |
|
430 | + * |
|
431 | + * @return void |
|
432 | + */ |
|
433 | + abstract public function sync(array $actualContent); |
|
434 | + |
|
435 | + /** |
|
436 | + * Handle dynamic method calls to the relationship. |
|
437 | + * |
|
438 | + * @param string $method |
|
439 | + * @param array $parameters |
|
440 | + * |
|
441 | + * @return mixed |
|
442 | + */ |
|
443 | + public function __call($method, $parameters) |
|
444 | + { |
|
445 | + $result = call_user_func_array([$this->query, $method], $parameters); |
|
446 | + |
|
447 | + if ($result === $this->query) { |
|
448 | + return $this; |
|
449 | + } |
|
450 | + |
|
451 | + return $result; |
|
452 | + } |
|
453 | 453 | } |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | |
229 | 229 | $host = $this; |
230 | 230 | |
231 | - return array_unique(array_values(array_map(function ($value) use ($key, $host) { |
|
231 | + return array_unique(array_values(array_map(function($value) use ($key, $host) { |
|
232 | 232 | if (!$value instanceof InternallyMappable) { |
233 | 233 | $value = $host->factory->make($value); |
234 | 234 | } |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | $key = $this->parentMap->getKeyName(); |
252 | 252 | } |
253 | 253 | |
254 | - return array_unique(array_values(array_map(function ($value) use ($key) { |
|
254 | + return array_unique(array_values(array_map(function($value) use ($key) { |
|
255 | 255 | return $value[$key]; |
256 | 256 | }, $results))); |
257 | 257 | } |
@@ -13,80 +13,80 @@ |
||
13 | 13 | */ |
14 | 14 | class MapperFactory |
15 | 15 | { |
16 | - /** |
|
17 | - * Manager instance. |
|
18 | - * |
|
19 | - * @var \Analogue\ORM\System\Manager |
|
20 | - */ |
|
21 | - protected $manager; |
|
22 | - |
|
23 | - /** |
|
24 | - * DriverManager instance. |
|
25 | - * |
|
26 | - * @var \Analogue\ORM\Drivers\Manager |
|
27 | - */ |
|
28 | - protected $drivers; |
|
29 | - |
|
30 | - /** |
|
31 | - * Event dispatcher instance. |
|
32 | - * |
|
33 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
34 | - */ |
|
35 | - protected $dispatcher; |
|
36 | - |
|
37 | - /** |
|
38 | - * MapperFactory constructor. |
|
39 | - * |
|
40 | - * @param DriverManager $drivers |
|
41 | - * @param Dispatcher $dispatcher |
|
42 | - * @param Manager $manager |
|
43 | - */ |
|
44 | - public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
45 | - { |
|
46 | - $this->drivers = $drivers; |
|
47 | - |
|
48 | - $this->dispatcher = $dispatcher; |
|
49 | - |
|
50 | - $this->manager = $manager; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Return a new Mapper instance. |
|
55 | - * |
|
56 | - * @param string $entityClass |
|
57 | - * @param EntityMap $entityMap |
|
58 | - * |
|
59 | - * @return Mapper |
|
60 | - */ |
|
61 | - public function make($entityClass, EntityMap $entityMap) |
|
62 | - { |
|
63 | - $driver = $entityMap->getDriver(); |
|
64 | - |
|
65 | - $connection = $entityMap->getConnection(); |
|
66 | - |
|
67 | - $adapter = $this->drivers->getAdapter($driver, $connection); |
|
68 | - |
|
69 | - $entityMap->setDateFormat($adapter->getDateFormat()); |
|
70 | - |
|
71 | - $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
72 | - |
|
73 | - // Fire Initializing Event |
|
74 | - $mapper->fireEvent('initializing', $mapper); |
|
75 | - |
|
76 | - // Proceed necessary parsing on the EntityMap object |
|
77 | - if (!$entityMap->isBooted()) { |
|
78 | - $entityMap->initialize(); |
|
79 | - } |
|
80 | - |
|
81 | - // Apply Inheritance scope, if necessary |
|
82 | - if ($entityMap->getInheritanceType() == 'single_table') { |
|
83 | - $scope = new SingleTableInheritanceScope($entityMap); |
|
84 | - $mapper->addGlobalScope($scope); |
|
85 | - } |
|
86 | - |
|
87 | - // Fire Initialized Event |
|
88 | - $mapper->fireEvent('initialized', $mapper); |
|
89 | - |
|
90 | - return $mapper; |
|
91 | - } |
|
16 | + /** |
|
17 | + * Manager instance. |
|
18 | + * |
|
19 | + * @var \Analogue\ORM\System\Manager |
|
20 | + */ |
|
21 | + protected $manager; |
|
22 | + |
|
23 | + /** |
|
24 | + * DriverManager instance. |
|
25 | + * |
|
26 | + * @var \Analogue\ORM\Drivers\Manager |
|
27 | + */ |
|
28 | + protected $drivers; |
|
29 | + |
|
30 | + /** |
|
31 | + * Event dispatcher instance. |
|
32 | + * |
|
33 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
34 | + */ |
|
35 | + protected $dispatcher; |
|
36 | + |
|
37 | + /** |
|
38 | + * MapperFactory constructor. |
|
39 | + * |
|
40 | + * @param DriverManager $drivers |
|
41 | + * @param Dispatcher $dispatcher |
|
42 | + * @param Manager $manager |
|
43 | + */ |
|
44 | + public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
45 | + { |
|
46 | + $this->drivers = $drivers; |
|
47 | + |
|
48 | + $this->dispatcher = $dispatcher; |
|
49 | + |
|
50 | + $this->manager = $manager; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Return a new Mapper instance. |
|
55 | + * |
|
56 | + * @param string $entityClass |
|
57 | + * @param EntityMap $entityMap |
|
58 | + * |
|
59 | + * @return Mapper |
|
60 | + */ |
|
61 | + public function make($entityClass, EntityMap $entityMap) |
|
62 | + { |
|
63 | + $driver = $entityMap->getDriver(); |
|
64 | + |
|
65 | + $connection = $entityMap->getConnection(); |
|
66 | + |
|
67 | + $adapter = $this->drivers->getAdapter($driver, $connection); |
|
68 | + |
|
69 | + $entityMap->setDateFormat($adapter->getDateFormat()); |
|
70 | + |
|
71 | + $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
72 | + |
|
73 | + // Fire Initializing Event |
|
74 | + $mapper->fireEvent('initializing', $mapper); |
|
75 | + |
|
76 | + // Proceed necessary parsing on the EntityMap object |
|
77 | + if (!$entityMap->isBooted()) { |
|
78 | + $entityMap->initialize(); |
|
79 | + } |
|
80 | + |
|
81 | + // Apply Inheritance scope, if necessary |
|
82 | + if ($entityMap->getInheritanceType() == 'single_table') { |
|
83 | + $scope = new SingleTableInheritanceScope($entityMap); |
|
84 | + $mapper->addGlobalScope($scope); |
|
85 | + } |
|
86 | + |
|
87 | + // Fire Initialized Event |
|
88 | + $mapper->fireEvent('initialized', $mapper); |
|
89 | + |
|
90 | + return $mapper; |
|
91 | + } |
|
92 | 92 | } |
@@ -15,56 +15,56 @@ |
||
15 | 15 | */ |
16 | 16 | class ProxyFactory |
17 | 17 | { |
18 | - public function make($entity, $relation, $class) |
|
19 | - { |
|
20 | - $entityMap = Manager::getMapper($entity)->getEntityMap(); |
|
18 | + public function make($entity, $relation, $class) |
|
19 | + { |
|
20 | + $entityMap = Manager::getMapper($entity)->getEntityMap(); |
|
21 | 21 | |
22 | - $singleRelations = $entityMap->getSingleRelationships(); |
|
23 | - $manyRelations = $entityMap->getManyRelationships(); |
|
22 | + $singleRelations = $entityMap->getSingleRelationships(); |
|
23 | + $manyRelations = $entityMap->getManyRelationships(); |
|
24 | 24 | |
25 | - if (in_array($relation, $singleRelations)) { |
|
26 | - return $this->makeEntityProxy($entity, $relation, $class); |
|
27 | - } |
|
25 | + if (in_array($relation, $singleRelations)) { |
|
26 | + return $this->makeEntityProxy($entity, $relation, $class); |
|
27 | + } |
|
28 | 28 | |
29 | - if (in_array($relation, $manyRelations)) { |
|
30 | - return new CollectionProxy($entity, $relation); |
|
31 | - } |
|
29 | + if (in_array($relation, $manyRelations)) { |
|
30 | + return new CollectionProxy($entity, $relation); |
|
31 | + } |
|
32 | 32 | |
33 | - throw new MappingException("Could not identify relation '$relation'"); |
|
34 | - } |
|
33 | + throw new MappingException("Could not identify relation '$relation'"); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Create an instance of a proxy object, extending the actual |
|
38 | - * related class. |
|
39 | - * |
|
40 | - * @param mixed $entity parent object |
|
41 | - * @param string $relation the name of the relationship method |
|
42 | - * @param string $class the class name of the related object |
|
43 | - * |
|
44 | - * @return mixed |
|
45 | - */ |
|
46 | - protected function makeEntityProxy($entity, $relation, $class) |
|
47 | - { |
|
48 | - $proxyPath = Manager::getInstance()->getProxyPath(); |
|
36 | + /** |
|
37 | + * Create an instance of a proxy object, extending the actual |
|
38 | + * related class. |
|
39 | + * |
|
40 | + * @param mixed $entity parent object |
|
41 | + * @param string $relation the name of the relationship method |
|
42 | + * @param string $class the class name of the related object |
|
43 | + * |
|
44 | + * @return mixed |
|
45 | + */ |
|
46 | + protected function makeEntityProxy($entity, $relation, $class) |
|
47 | + { |
|
48 | + $proxyPath = Manager::getInstance()->getProxyPath(); |
|
49 | 49 | |
50 | - if ($proxyPath !== null) { |
|
51 | - $proxyConfig = new Configuration(); |
|
52 | - $proxyConfig->setProxiesTargetDir($proxyPath); |
|
50 | + if ($proxyPath !== null) { |
|
51 | + $proxyConfig = new Configuration(); |
|
52 | + $proxyConfig->setProxiesTargetDir($proxyPath); |
|
53 | 53 | |
54 | - $factory = new LazyLoadingValueHolderFactory($proxyConfig); |
|
55 | - } else { |
|
56 | - $factory = new LazyLoadingValueHolderFactory(); |
|
57 | - } |
|
54 | + $factory = new LazyLoadingValueHolderFactory($proxyConfig); |
|
55 | + } else { |
|
56 | + $factory = new LazyLoadingValueHolderFactory(); |
|
57 | + } |
|
58 | 58 | |
59 | - $initializer = function (&$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer) use ($entity, $relation) { |
|
60 | - $entityMap = Manager::getMapper($entity)->getEntityMap(); |
|
59 | + $initializer = function (&$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer) use ($entity, $relation) { |
|
60 | + $entityMap = Manager::getMapper($entity)->getEntityMap(); |
|
61 | 61 | |
62 | - $wrappedObject = $entityMap->$relation($entity)->getResults($relation); |
|
62 | + $wrappedObject = $entityMap->$relation($entity)->getResults($relation); |
|
63 | 63 | |
64 | - $initializer = null; // disable initialization |
|
65 | - return true; // confirm that initialization occurred correctly |
|
66 | - }; |
|
64 | + $initializer = null; // disable initialization |
|
65 | + return true; // confirm that initialization occurred correctly |
|
66 | + }; |
|
67 | 67 | |
68 | - return $factory->createProxy($class, $initializer); |
|
69 | - } |
|
68 | + return $factory->createProxy($class, $initializer); |
|
69 | + } |
|
70 | 70 | } |
@@ -56,7 +56,7 @@ |
||
56 | 56 | $factory = new LazyLoadingValueHolderFactory(); |
57 | 57 | } |
58 | 58 | |
59 | - $initializer = function (&$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer) use ($entity, $relation) { |
|
59 | + $initializer = function(&$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, &$initializer) use ($entity, $relation) { |
|
60 | 60 | $entityMap = Manager::getMapper($entity)->getEntityMap(); |
61 | 61 | |
62 | 62 | $wrappedObject = $entityMap->$relation($entity)->getResults($relation); |