Skip to content

Commit

Permalink
Properties are camel-cased by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
mducharme committed Jul 31, 2019
1 parent 913e701 commit a7d0a2e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Charcoal/Model/AbstractMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ public function defaultData()
}

/**
* Set the properties.
* Set the properties. Ensure the property idents (keys) are camel-cased.
*
* @param array $properties One or more properties.
* @return self
*/
public function setProperties(array $properties)
{
$this->properties = $properties;
$this->properties = [];
foreach ($properties as $k => $v) {
$this->properties[$this->camelize($k)] = $v;
}
return $this;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Charcoal/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function __construct(array $data = null)
*
* @param array $data The entity data. Will call setters.
* @return self
* @see AbstractEntity::setData()
*/
public function setData(array $data)
{
Expand Down Expand Up @@ -211,7 +212,8 @@ public function setFlatData(array $flatData)
foreach ($fields as $k => $f) {
if (is_string($k)) {
$fid = $f->ident();
$key = str_replace($propertyIdent.'_', '', $fid);
$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]);
Expand Down Expand Up @@ -251,12 +253,14 @@ public function flatData()

/**
* Retrieve the value for the given property.
* Force camelcase on the parameter.
*
* @param string $propertyIdent The property identifier to fetch.
* @return mixed
*/
public function propertyValue($propertyIdent)
{
$propertyIdent = $this->camelize($propertyIdent);
return $this[$propertyIdent];
}

Expand Down

0 comments on commit a7d0a2e

Please sign in to comment.