Skip to content

Commit e8ce49d

Browse files
committed
[+]: DataMapper :: Support for Object Data Type
1 parent 80e7b00 commit e8ce49d

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/DataMapper.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,21 @@ private static function propertiesToFlat(array $properties, $parent = null): arr
137137
{
138138
$result = [];
139139

140+
$innerTypes = [
141+
MappingFieldType::NESTED,
142+
MappingFieldType::OBJECT,
143+
];
144+
140145
if (! isset($parent)) {
141146
$result['root'] =
142147
[
143148
'map' => \array_flip(\array_keys(\array_filter(
144149
$properties,
145-
function ($item) {
146-
if (isset($item['type']) && $item['type'] === MappingFieldType::NESTED) {
147-
return false;
148-
}
149-
150-
return true;
150+
function ($item) use ($innerTypes) {
151+
return ! (
152+
isset($item['type'])
153+
|| (isset($item['properties']) && (\is_array($item['properties'])))
154+
);
151155
}
152156
))),
153157
];
@@ -157,16 +161,24 @@ function ($item) {
157161
$pKey = ! isset($parent) ? $key : ($parent . '.' . $key);
158162
$type = isset($value['type']) ? $value['type'] : null;
159163

164+
if (
165+
! isset($type)
166+
&& isset($value['properties'])
167+
&& (\is_array($value['properties']))
168+
) {
169+
$type = MappingFieldType::OBJECT;
170+
}
171+
160172
$result[$pKey] = [
161173
'key' => $key,
162174
'type' => $type,
163-
'map' => ($type === MappingFieldType::NESTED) ? \array_flip(\array_keys($value['properties'])) : [],
175+
'map' => \in_array($type, $innerTypes) ? \array_flip(\array_keys($value['properties'])) : [],
164176
];
165177

166-
if ($type === MappingFieldType::NESTED) {
178+
if (\in_array($type, $innerTypes)) {
167179
$result = \array_merge($result, self::propertiesToFlat($value['properties'], $pKey));
168180
}
169-
}
181+
}//end foreach
170182

171183
return $result;
172184
}
@@ -247,6 +259,7 @@ protected static function buildMap(array $data, array $mapping, $parent = null)
247259
}
248260
break;
249261

262+
case MappingFieldType::OBJECT:
250263
case MappingFieldType::NESTED:
251264
// Check if $value is associative.
252265
if (\is_array(\current($value))) {

0 commit comments

Comments
 (0)