-
Notifications
You must be signed in to change notification settings - Fork 4
/
Hydrator.php
143 lines (126 loc) · 5.36 KB
/
Hydrator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Pulsar;
use Pulsar\Relation\Relationship;
class Hydrator
{
/**
* @return Model[]
*/
public static function hydrate(array $result, string|Model $modelClass, array $eagerLoaded): array
{
$ids = [];
$eagerLoadedProperties = [];
foreach ($eagerLoaded as $k) {
$eagerLoadedProperties[$k] = $modelClass::definition()->get($k);
$ids[$k] = [];
}
// fetch the models matching the query
/** @var Model[] $models */
$models = [];
foreach ($result as $j => $row) {
// type-cast the values because they came from the database
foreach ($row as $k => &$v) {
if ($property = $modelClass::definition()->get($k)) {
$v = Type::cast($property, $v);
}
}
// create the model and cache the loaded values
$models[] = new $modelClass($row);
// capture any local ids for eager loading relationships
foreach ($eagerLoaded as $k) {
$localKey = $eagerLoadedProperties[$k]->local_key;
if (isset($row[$localKey])) {
$ids[$k][$j] = $row[$localKey];
}
}
}
// hydrate the eager loaded relationships
foreach ($eagerLoaded as $k) {
$property = $eagerLoadedProperties[$k];
$relationModelClass = $property->relation;
$type = $property->relation_type;
if (Relationship::BELONGS_TO == $type) {
$relationships = self::fetchRelationships($relationModelClass, $ids[$k], $property->foreign_key, false);
foreach ($ids[$k] as $j => $id) {
if (isset($relationships[$id])) {
if ($property->persisted) {
$models[$j]->setRelation($k, $relationships[$id]);
// older style properties do not support this type of hydration
} else {
$models[$j]->hydrateValue($k, $relationships[$id]);
}
}
}
} elseif (Relationship::HAS_ONE == $type) {
$relationships = self::fetchRelationships($relationModelClass, $ids[$k], $property->foreign_key, false);
foreach ($ids[$k] as $j => $id) {
if (isset($relationships[$id])) {
if ($property->persisted) {
$models[$j]->setRelation($k, $relationships[$id]);
// older style properties do not support this type of hydration
} else {
$models[$j]->hydrateValue($k, $relationships[$id]);
}
} else {
// when using has one eager loading we must
// explicitly mark the relationship as null
// for models not found during eager loading
// or else it will trigger another DB call
$models[$j]->clearRelation($k);
// older style properties do not support this type of hydration
if (!$property->persisted) {
$models[$j]->hydrateValue($k, null);
}
}
}
} elseif (Relationship::HAS_MANY == $type) {
$relationships = self::fetchRelationships($relationModelClass, $ids[$k], $property->foreign_key, true);
foreach ($ids[$k] as $j => $id) {
if (isset($relationships[$id])) {
$models[$j]->setRelationCollection($k, $relationships[$id]);
// older style properties do not support this type of hydration
if (!$property->persisted) {
$models[$j]->hydrateValue($k, $relationships[$id]);
}
} else {
$models[$j]->setRelationCollection($k, []);
// older style properties do not support this type of hydration
if (!$property->persisted) {
$models[$j]->hydrateValue($k, []);
}
}
}
}
}
return $models;
}
/**
* Hydrates the eager-loaded relationships for a given set of IDs.
*
* @param bool $multiple when true will condense
*
* @return Model[]
*/
private static function fetchRelationships(string $modelClass, array $ids, string $foreignKey, bool $multiple): array
{
$uniqueIds = array_unique($ids);
if (0 === count($uniqueIds)) {
return [];
}
$in = $foreignKey.' IN ('.implode(',', $uniqueIds).')';
$models = $modelClass::where($in)
->first(Query::MAX_LIMIT);
$result = [];
foreach ($models as $model) {
if ($multiple) {
if (!isset($result[$model->$foreignKey])) {
$result[$model->$foreignKey] = [];
}
$result[$model->$foreignKey][] = $model;
} else {
$result[$model->$foreignKey] = $model;
}
}
return $result;
}
}