Skip to content

Commit ca39455

Browse files
committed
[!] Updated to latest charcoal-property
Reason: - Charcoal Property adds support for custom storable property fields - Decouples L10N property fields - Class `AbstractModel` is updated to cleanup flat-data assignment Requires: - charcoal-property v0.10.x Changed: - Bump branch-alias to 0.6 - Method `AbstractModel::setFlatData()` to decouple flat-data processing for model properties Added: - Method `setPropertyDataFromFlatData()` to process data from property field names - Method `setPropertyData()` to only assign where datum is a property and return the rest
1 parent 19bc4f2 commit ca39455

File tree

3 files changed

+58
-30
lines changed

3 files changed

+58
-30
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"prefer-stable": true,
2020
"extra": {
2121
"branch-alias": {
22-
"dev-master": "0.5.x-dev"
22+
"dev-master": "0.6.x-dev"
2323
}
2424
},
2525
"require": {
@@ -32,7 +32,7 @@
3232
"locomotivemtl/charcoal-cache": "~0.1",
3333
"locomotivemtl/charcoal-config": "~0.9",
3434
"locomotivemtl/charcoal-factory": "~0.4",
35-
"locomotivemtl/charcoal-property": "~0.9",
35+
"locomotivemtl/charcoal-property": "~0.10",
3636
"locomotivemtl/charcoal-view": "~0.3"
3737
},
3838
"require-dev": {

src/Charcoal/Model/AbstractModel.php

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,26 @@ public function setData(array $data)
142142
return $this;
143143
}
144144

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+
145165
/**
146166
* Merge data on the model.
147167
*
@@ -196,46 +216,53 @@ public function defaultData()
196216
/**
197217
* Set the model data (from a flattened structure).
198218
*
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.
202220
* @return self
203221
*/
204222
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)
205245
{
206246
$flatData = $this->setIdFromData($flatData);
207247

208-
$data = [];
248+
$propData = [];
209249
$properties = $this->properties();
210250
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]);
227257
}
228258
}
229-
}
230-
231-
$this->setData($data);
232259

233-
// Set remaining (non-property) data.
234-
if (!empty($flatData)) {
235-
$this->setData($flatData);
260+
if ($fieldValues) {
261+
$this[$propertyIdent] = $property->parseFromFlatData($fieldValues);
262+
}
236263
}
237264

238-
return $this;
265+
return $flatData;
239266
}
240267

241268
/**
@@ -358,7 +385,7 @@ public function loadFromL10n($key, $value, array $langs)
358385
* Useful for setting the object ID before the rest of the object's data.
359386
*
360387
* @param array $data The object data.
361-
* @return array The object data without the pre-set ID.
388+
* @return array Returns the remaining dataset.
362389
*/
363390
protected function setIdFromData(array $data)
364391
{

src/Charcoal/Source/AbstractSource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ public function addFilter($param, $value = null, array $options = null)
270270
/**
271271
* Process a query filter with the current model.
272272
*
273+
* @todo If property is L10N, turn filter into group of filters for each language.
273274
* @param FilterInterface $filter The expression object.
274275
* @return FilterInterface The parsed expression object.
275276
*/

0 commit comments

Comments
 (0)