-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNestHydration.php
330 lines (280 loc) · 10.4 KB
/
NestHydration.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
namespace NestHydration;
class NestHydration
{
const SPL_OBJECT = 'object';
const ASSOCIATIVE_ARRAY = 'associative';
const ARRAY_ACCESS_OBJECT = 'array_access_object';
/**
* Creates a data structure containing nested objects and/or arrays from
* tabular data based on a structure definition provided by
* structPropToColumnMap. If structPropToColumnMap is not provided but
* the data has column names that follow a particular convention then nested
* nested structures can also be created.
*
* @param array $data must be either an associtative array or a list of
* assoicative arrays. The columns in the table MUST be strings and not
* numeric.
* @param SPL_OBJECT|ASSOCIATIVE_ARRAY $resultType
* @param array|null|true $propertyMapping
* @return array returns a nested data structure that in accordance to
* that specified by the $propertyMapping param and populated with data
* from the $data parameter. Support output of nested associative arrays
* and lists or nested spl objects and lists.
*/
public static function nest($data, $resultType = NestHydration::ASSOCIATIVE_ARRAY, $structPropToColumnMap = null) {
// VALIDATE PARAMS AND BASIC INITIALIZATION
$listOnEmpty = false;
$columnList = null;
if ($data === null) {
return null;
}
if (!in_array($resultType, array(NestHydration::SPL_OBJECT, NestHydration::ASSOCIATIVE_ARRAY, NestHydration::ARRAY_ACCESS_OBJECT))) {
// invalid result type specified, use default
$resultType = NestHydration::ASSOCIATIVE_ARRAY;
}
if (!is_array($structPropToColumnMap) && $structPropToColumnMap !== null && $structPropToColumnMap !== true) {
throw new \Exception('nest expects param propertyMapping to be an array, plain object, null, or true');
}
// propertyMapping can be set to true as a tie break between
// returning null (empty structure) or an empty list
if ($structPropToColumnMap === true) {
$listOnEmpty = true;
$structPropToColumnMap = null;
} elseif (is_array($structPropToColumnMap) && is_integer(key($structPropToColumnMap))) {
$listOnEmpty = true;
}
if (empty($data)) {
return $listOnEmpty ? array() : null;
}
if (is_array($data) && !is_integer(key($data))) {
// internal table should be a table format but an associative
// array could be passed as the first (and only) row of that table
$table = array($data);
} elseif (is_array($data) && is_integer(key($data))) {
$table = $data;
} else {
throw new \Exception('nest expects param data to form an plain object or an array of plain objects (forming a table)');
}
if ($structPropToColumnMap === null && !empty($table)) {
// property mapping not specified, determine it from column names
$structPropToColumnMap = self::structPropToColumnMapFromColumnHints(array_keys($table[0]));
}
if ($structPropToColumnMap === null) {
// properties is empty, can't form structure or determine content
// for a list. Assume a structure unless listOnEmpty
return $listOnEmpty ? array() : null;
} elseif (empty($table)) {
// table is empty, return the appropriate empty result based on input definition
return is_integer(key($structPropToColumnMap)) ? array() : null;
}
// COMPLETE VALIDATING PARAMS AND BASIC INITIALIZATION
$meta = self::buildMeta($structPropToColumnMap);
// BUILD FROM TABLE
// struct is populated inside the build function
$struct = array('base' => null);
foreach ($table as $row) {
foreach ($meta->primeIdColumnList as $primeIdColumn) {
// for each prime id column (corresponding to a to many relation or
// the top level) attempted to build an object
self::_nest($row, $primeIdColumn, $struct, $meta, $resultType);
}
}
return $struct['base'];
}
// defines function that can be called recursively
protected static function _nest($row, $idColumn, &$struct, &$meta, $resultType) {
$value = $row[$idColumn];
if ($value === null) {
// nothing to build
return;
}
// only really concerned with the meta data for this identity column
$objMeta = $meta->idMap[$idColumn];
if (array_key_exists($value . '', $objMeta->cache)) {
// object already exists in cache
if ($objMeta->containingIdUsage === null) {
// at the top level, parent is root
return;
}
$containingId = $row[$objMeta->containingColumn];
if (array_key_exists($value . '', $objMeta->containingIdUsage)
&& array_key_exists($containingId . '', $objMeta->containingIdUsage[$value . ''])
) {
// already placed as to many relation in container, done
return;
}
// not already placed as to many relation in container
$obj = &$objMeta->cache[$value . ''];
} else {
// don't have an object defined for this yet, create it
// create new structure in the list
if ($resultType === NestHydration::ARRAY_ACCESS_OBJECT) {
$obj = new \NestHydration\ArrayAccess();
} elseif ($resultType === NestHydration::SPL_OBJECT) {
$obj = new \StdClass;
} else { // ASSOCIATIVE_ARRAY
$obj = array();
}
$objMeta->cache[$value . ''] = &$obj;
// copy in properties from table data
foreach ($objMeta->valueList as $frag) {
if ($resultType === NestHydration::SPL_OBJECT) {
$prop = $frag->prop;
$obj->$prop = $row[$frag->column];
} else { // ASSOCIATIVE_ARRAY, ARRAY_ACCESS_OBJECT
$obj[$frag->prop] = $row[$frag->column];
}
}
// initialize empty to many relations, they will be populated when
// those objects build themselve and find this containing object
foreach ($objMeta->toManyPropList as $prop) {
if ($resultType === NestHydration::SPL_OBJECT) {
$obj->$prop = array();
} else { // ASSOCIATIVE_ARRAY, ARRAY_ACCESS_OBJECT
$obj[$prop] = array();
}
}
// intialize null to one relations and then recursively build them
foreach ($objMeta->toOneList as $frag) {
if ($resultType === NestHydration::SPL_OBJECT) {
$prop = $frag->prop;
$obj->$prop = null;
} else { // ASSOCIATIVE_ARRAY, ARRAY_ACCESS_OBJECT
$obj[$frag->prop] = null;
}
self::_nest($row, $frag->column, $struct, $meta, $resultType);
}
}
// link from the parent
if ($objMeta->containingColumn === null) {
// parent is the top level
if ($objMeta->isOneOfMany) {
// it is an array
if ($struct === null) {
$struct = array();
}
$struct['base'][] = &$obj;
} else {
// it is this object
$struct['base'] = &$obj;
}
} else {
$containingId = $row[$objMeta->containingColumn];
$container = &$meta->idMap[$objMeta->containingColumn]->cache[$containingId . ''];
if ($objMeta->isOneOfMany) {
// it is an array
if ($resultType === NestHydration::SPL_OBJECT || $resultType === NestHydration::ARRAY_ACCESS_OBJECT) {
$prop = $objMeta->ownProp;
$propObj = &$container->$prop;
$propObj[] = &$obj;
} else { // ASSOCIATIVE_ARRAY
$container[$objMeta->ownProp][] = &$obj;
}
} else {
// it is this object
if ($resultType === NestHydration::SPL_OBJECT || $resultType === NestHydration::ARRAY_ACCESS_OBJECT) {
$prop = $objMeta->ownProp;
$container->$prop = &$obj;
} else { // ASSOCIATIVE_ARRAY
$container[$objMeta->ownProp] = &$obj;
}
}
// record the containing id
if (!array_key_exists($value . '', $objMeta->containingIdUsage)) {
$objMeta->containingIdUsage[$value . ''] = array();
}
$objMeta->containingIdUsage[$value . ''][$containingId . ''] = true;
}
}
/* Create a data structure that contains lookups and cache spaces for quick
* reference and action for the workings of the nest method.
*/
protected static function buildMeta($structPropToColumnMap) {
// this data structure is populated by the _buildMeta function
$meta = (object) array(
'primeIdColumnList' => array(),
'idMap' => array(),
);
if (empty($structPropToColumnMap)) {
throw new \Exception('invalid structPropToColumnMap format');
}
if (is_integer(key($structPropToColumnMap))) {
// call with first object, but inform _buidMeta it is an array
self::_buildMeta($structPropToColumnMap[0], true, null, null, $meta);
} else {
// register first column as prime id column
$meta->primeIdColumnList[] = key($structPropToColumnMap);
// construct the rest
self::_buildMeta($structPropToColumnMap, false, null, null, $meta);
}
return $meta;
}
// recursive internal function
protected static function _buildMeta($structPropToColumnMap, $isOneOfMany, $containingColumn, $ownProp, &$meta) {
if (empty($structPropToColumnMap)) {
throw new \Exception('invalid structPropToColumnMap format');
}
$idColumn = $structPropToColumnMap[key($structPropToColumnMap)];
if ($isOneOfMany) {
$meta->primeIdColumnList[] = $idColumn;
}
$objMeta = (object) array(
'valueList' => array(),
'toOneList' => array(),
'toManyPropList' => array(),
'containingColumn' => $containingColumn,
'ownProp' => $ownProp,
'isOneOfMany' => $isOneOfMany === true,
'cache' => array(),
'containingIdUsage' => $containingColumn === null ? null : array(),
);
foreach ($structPropToColumnMap as $prop => $column) {
if (is_string($column)) {
// value property
$objMeta->valueList[] = (object) array(
'prop' => $prop,
'column' => $column,
);
} elseif (is_array($column) && is_integer(key($column))) {
// list of objects / to many relation
$objMeta->toManyPropList[] = $prop;
self::_buildMeta($column[0], true, $idColumn, $prop, $meta);
} else {
// object / to one relation
$subIdColumn = current($column);
$objMeta->toOneList[] = (object) array(
'prop' => $prop,
'column' => $subIdColumn,
);
self::_buildMeta($column, false, $idColumn, $prop, $meta);
}
}
$meta->idMap[$idColumn] = $objMeta;
}
/**
* Returns a property mapping data structure based on the names of columns
* in columnList. Used internally by nest when its propertyMapping param
* is not specified.
*/
public static function structPropToColumnMapFromColumnHints($columnList)
{
$propertyMapping = array();
foreach ($columnList as $column) {
$pointer =& $propertyMapping;
$navList = explode('_', $column);
$leaf = array_pop($navList);
foreach ($navList as $nav) {
if ($nav === '') {
$nav = 0;
}
if (!array_key_exists($nav, $pointer)) {
$pointer[$nav] = array();
}
$pointer =& $pointer[$nav];
}
$pointer[$leaf] = $column;
}
return $propertyMapping;
}
}