Skip to content

Commit

Permalink
Merge pull request #3 from afosto/hotfix/before-get-attributes
Browse files Browse the repository at this point in the history
fixed issues with flipping attribute keys
  • Loading branch information
bakkerpeter authored Feb 28, 2017
2 parents 42f3492 + 5a71984 commit 1fccb96
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions src/Components/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class Model {
public static function getDocBlock() {
$model = new static();
foreach ($model->getRules() as $rule) {
echo "* @property {$rule[1]} \${$rule[0]}\n";
echo "* @property {$rule[1]} \${$model->getFormattedKey($rule[0], true)}\n";
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ public function validateMaxLength($length, $key) {
*/
public function validateRequired($key) {
if ($this->$key === null) {
throw new ValidationException("{$key} is required");
throw new ValidationException("{$key} is required for " . get_called_class());
}
}

Expand Down Expand Up @@ -123,9 +123,11 @@ public function validateBoolean($key) {
*
* @param array $data
*/
public function setAttributes(array $data) {
public function setAttributes($data) {
if ($data instanceof Model) {
$data = $data->getAttributes();
}
foreach ($data as $key => $value) {
$key = $this->getFormattedKey($key);
$this->$key = $value;
}
}
Expand Down Expand Up @@ -190,23 +192,48 @@ public function validate() {
* @return array
*/
public function getAttributes() {
return $this->_toArray(false, false);
}

/**
* Shorthand to return the formatted model
* Use the mapper (getMap) to transform the keys and validate the model data based on the rule set
*
* @return array
*/
public function getModel() {
return $this->_toArray(true, true);
}

/**
* Map the object to array
*
* @param bool $validate
* @param bool $mapKeys
*
* @return array
*/
private function _toArray($validate = false, $mapKeys = false) {
$callee = (($validate === $mapKeys && $mapKeys === true) ? 'getModel' : 'getAttributes');
$data = [];
if ($validate === true) {
$this->validate();
}

if ($this->validate()) {
foreach ($this->attributes as $attribute) {
if ($attribute->value instanceof \ArrayObject) {
foreach ($attribute->value as $item) {
$data[$this->getFormattedKey($attribute->key)][] = $item->getAttributes();
}
} else if ($attribute->value instanceof Model) {
$data[$this->getFormattedKey($attribute->key)] = $attribute->value->getAttributes();
} else {
$data[$this->getFormattedKey($attribute->key)] = $attribute->value;
foreach ($this->attributes as $attribute) {
$key = ($mapKeys === true ? $this->getFormattedKey($attribute->key) : $attribute->key);

if ($attribute->value instanceof \ArrayObject) {
foreach ($attribute->value as $item) {
$data[$key][] = $item->$callee();
}
} else if ($attribute->value instanceof Model) {
$data[$key] = $attribute->value->$callee();
} else {
$data[$key] = $attribute->value;
}

return $data;
}
}

return $data;
}
}

0 comments on commit 1fccb96

Please sign in to comment.