@@ -142,6 +142,26 @@ public function setData(array $data)
142
142
return $ this ;
143
143
}
144
144
145
+ /**
146
+ * Sets the object data, from an associative array map (or any other Traversable).
147
+ *
148
+ * @param array $data The model property data.
149
+ * @return array Returns the remaining dataset.
150
+ */
151
+ public function setPropertyData (array $ data )
152
+ {
153
+ $ data = $ this ->setIdFromData ($ data );
154
+
155
+ foreach ($ data as $ key => $ value ) {
156
+ if ($ this ->hasProperty ($ key )) {
157
+ $ this [$ key ] = $ value ;
158
+ unset($ data [$ key ]);
159
+ }
160
+ }
161
+
162
+ return $ data ;
163
+ }
164
+
145
165
/**
146
166
* Merge data on the model.
147
167
*
@@ -196,46 +216,53 @@ public function defaultData()
196
216
/**
197
217
* Set the model data (from a flattened structure).
198
218
*
199
- * This method takes a 1-dimensional array and fills the object with its values.
200
- *
201
- * @param array $flatData The model data.
219
+ * @param array $flatData The model dataset.
202
220
* @return self
203
221
*/
204
222
public function setFlatData (array $ flatData )
223
+ {
224
+ $ flatData = $ this ->setPropertyDataFromFlatData ($ flatData );
225
+
226
+ // Set remaining (non-property) data.
227
+ if (!empty ($ flatData )) {
228
+ $ this ->setData ($ flatData );
229
+ }
230
+
231
+ return $ this ;
232
+ }
233
+
234
+ /**
235
+ * Set the model property data (from a flattened structure).
236
+ *
237
+ * This method takes a one-dimensional dataset and, depending on the property's
238
+ * {@see \Charcoal\Property\PropertyField::fieldNames() field structure},
239
+ * returns a {@see \Charcoal\Property\PropertyField::parseFromFlatData() complex datum}.
240
+ *
241
+ * @param array $flatData The model property data.
242
+ * @return array Returns the remaining dataset.
243
+ */
244
+ public function setPropertyDataFromFlatData (array $ flatData )
205
245
{
206
246
$ flatData = $ this ->setIdFromData ($ flatData );
207
247
208
- $ data = [];
248
+ $ propData = [];
209
249
$ properties = $ this ->properties ();
210
250
foreach ($ properties as $ propertyIdent => $ property ) {
211
- $ fields = $ property ->fields (null );
212
- foreach ($ fields as $ k => $ f ) {
213
- if (is_string ($ k )) {
214
- $ fid = $ f ->ident ();
215
- $ snake = strtolower (preg_replace ('/(?<!^)[A-Z]/ ' , '_$0 ' , $ propertyIdent ));
216
- $ key = str_replace ($ snake .'_ ' , '' , $ fid );
217
- if (isset ($ flatData [$ fid ])) {
218
- $ data [$ propertyIdent ][$ key ] = $ flatData [$ fid ];
219
- unset($ flatData [$ fid ]);
220
- }
221
- } else {
222
- $ fid = $ f ->ident ();
223
- if (isset ($ flatData [$ fid ])) {
224
- $ data [$ propertyIdent ] = $ flatData [$ fid ];
225
- unset($ flatData [$ fid ]);
226
- }
251
+ $ fieldValues = [];
252
+ $ fieldNames = $ property ->fieldNames ();
253
+ foreach ($ fieldNames as $ fieldName ) {
254
+ if (array_key_exists ($ fieldName , $ flatData )) {
255
+ $ fieldValues [$ fieldName ] = $ flatData [$ fieldName ];
256
+ unset($ flatData [$ fieldName ]);
227
257
}
228
258
}
229
- }
230
-
231
- $ this ->setData ($ data );
232
259
233
- // Set remaining (non-property) data.
234
- if (! empty ( $ flatData )) {
235
- $ this -> setData ( $ flatData );
260
+ if ( $ fieldValues ) {
261
+ $ this [ $ propertyIdent ] = $ property -> parseFromFlatData ( $ fieldValues );
262
+ }
236
263
}
237
264
238
- return $ this ;
265
+ return $ flatData ;
239
266
}
240
267
241
268
/**
@@ -358,7 +385,7 @@ public function loadFromL10n($key, $value, array $langs)
358
385
* Useful for setting the object ID before the rest of the object's data.
359
386
*
360
387
* @param array $data The object data.
361
- * @return array The object data without the pre-set ID .
388
+ * @return array Returns the remaining dataset .
362
389
*/
363
390
protected function setIdFromData (array $ data )
364
391
{
0 commit comments