Skip to content

Commit

Permalink
[!] Updated to latest charcoal-property
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mcaskill committed Nov 4, 2019
1 parent 19bc4f2 commit ca39455
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "0.5.x-dev"
"dev-master": "0.6.x-dev"
}
},
"require": {
Expand All @@ -32,7 +32,7 @@
"locomotivemtl/charcoal-cache": "~0.1",
"locomotivemtl/charcoal-config": "~0.9",
"locomotivemtl/charcoal-factory": "~0.4",
"locomotivemtl/charcoal-property": "~0.9",
"locomotivemtl/charcoal-property": "~0.10",
"locomotivemtl/charcoal-view": "~0.3"
},
"require-dev": {
Expand Down
83 changes: 55 additions & 28 deletions src/Charcoal/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ public function setData(array $data)
return $this;
}

/**
* Sets the object data, from an associative array map (or any other Traversable).
*
* @param array $data The model property data.
* @return array Returns the remaining dataset.
*/
public function setPropertyData(array $data)
{
$data = $this->setIdFromData($data);

foreach ($data as $key => $value) {
if ($this->hasProperty($key)) {
$this[$key] = $value;
unset($data[$key]);
}
}

return $data;
}

/**
* Merge data on the model.
*
Expand Down Expand Up @@ -196,46 +216,53 @@ public function defaultData()
/**
* Set the model data (from a flattened structure).
*
* This method takes a 1-dimensional array and fills the object with its values.
*
* @param array $flatData The model data.
* @param array $flatData The model dataset.
* @return self
*/
public function setFlatData(array $flatData)
{
$flatData = $this->setPropertyDataFromFlatData($flatData);

// Set remaining (non-property) data.
if (!empty($flatData)) {
$this->setData($flatData);
}

return $this;
}

/**
* Set the model property data (from a flattened structure).
*
* This method takes a one-dimensional dataset and, depending on the property's
* {@see \Charcoal\Property\PropertyField::fieldNames() field structure},
* returns a {@see \Charcoal\Property\PropertyField::parseFromFlatData() complex datum}.
*
* @param array $flatData The model property data.
* @return array Returns the remaining dataset.
*/
public function setPropertyDataFromFlatData(array $flatData)
{
$flatData = $this->setIdFromData($flatData);

$data = [];
$propData = [];
$properties = $this->properties();
foreach ($properties as $propertyIdent => $property) {
$fields = $property->fields(null);
foreach ($fields as $k => $f) {
if (is_string($k)) {
$fid = $f->ident();
$snake = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $propertyIdent));
$key = str_replace($snake.'_', '', $fid);
if (isset($flatData[$fid])) {
$data[$propertyIdent][$key] = $flatData[$fid];
unset($flatData[$fid]);
}
} else {
$fid = $f->ident();
if (isset($flatData[$fid])) {
$data[$propertyIdent] = $flatData[$fid];
unset($flatData[$fid]);
}
$fieldValues = [];
$fieldNames = $property->fieldNames();
foreach ($fieldNames as $fieldName) {
if (array_key_exists($fieldName, $flatData)) {
$fieldValues[$fieldName] = $flatData[$fieldName];
unset($flatData[$fieldName]);
}
}
}

$this->setData($data);

// Set remaining (non-property) data.
if (!empty($flatData)) {
$this->setData($flatData);
if ($fieldValues) {
$this[$propertyIdent] = $property->parseFromFlatData($fieldValues);
}
}

return $this;
return $flatData;
}

/**
Expand Down Expand Up @@ -358,7 +385,7 @@ public function loadFromL10n($key, $value, array $langs)
* Useful for setting the object ID before the rest of the object's data.
*
* @param array $data The object data.
* @return array The object data without the pre-set ID.
* @return array Returns the remaining dataset.
*/
protected function setIdFromData(array $data)
{
Expand Down
1 change: 1 addition & 0 deletions src/Charcoal/Source/AbstractSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public function addFilter($param, $value = null, array $options = null)
/**
* Process a query filter with the current model.
*
* @todo If property is L10N, turn filter into group of filters for each language.
* @param FilterInterface $filter The expression object.
* @return FilterInterface The parsed expression object.
*/
Expand Down

0 comments on commit ca39455

Please sign in to comment.