Skip to content

Commit

Permalink
Merge pull request #1 from afosto/hotfix/before-get-attributes
Browse files Browse the repository at this point in the history
Before get attributes
  • Loading branch information
bakkerpeter authored Feb 16, 2017
2 parents 883a57b + c2bd5cf commit cf0006a
Showing 1 changed file with 82 additions and 9 deletions.
91 changes: 82 additions & 9 deletions src/Components/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ abstract class Model {
*/
protected $attributes = [];

/**
* Returns doc block formatting
*/
public static function getDocBlock() {
$model = new static();
foreach ($model->getRules() as $rule) {
Expand All @@ -19,6 +22,8 @@ public static function getDocBlock() {
}

/**
* Magic getter.
*
* @param $name
*
* @return mixed
Expand All @@ -27,9 +32,13 @@ public function __get($name) {
if (isset($this->attributes[$name])) {
return $this->attributes[$name]->value;
}

return null;
}

/**
* Magic setter.
*
* @param $name
* @param $value
*/
Expand All @@ -43,30 +52,68 @@ public function __set($name, $value) {
}
}

/**
* Model constructor.
*/
public function __construct() {
foreach ($this->getRules() as $rule) {
$attribute = new Attribute($this, $rule);
$this->attributes[$attribute->key] = $attribute;
}
}

/**
* Max length validator
*
* @param $length
* @param $key
*
* @throws ValidationException
*/
public function validateMaxLength($length, $key) {
if (strlen($this->$key) > $length) {
throw new ValidationException("{$key} is to long, maxLength is {$length}, " . strlen($this->$key) . " chars given");
}
}

/**
* Required validator
*
* @param $key
*
* @throws ValidationException
*/
public function validateRequired($key) {
if ($this->$key === null) {
throw new ValidationException("{$key} is required");
}
}

/**
* Validate doubles
*
* @param $key
*/
public function validateDouble($key) {
$this->$key = number_format($this->$key, 2, '.', '');
}

/**
* Validate bool
*
* @param $key
*
* @throws ValidationException
*/
public function validateBoolean($key) {
if (is_bool($this->$key)) {
throw new ValidationException("{$key} is no boolean: " . $this->$key . " given");
}
}

/**
* Set the model's attributes
*
* @param array $data
*/
public function setAttributes(array $data) {
Expand All @@ -75,8 +122,31 @@ public function setAttributes(array $data) {
}
}

/**
* Return the key, allow models to do mapping
*
* @param $key
*
* @return mixed
*/
protected function getFormattedKey($key) {
return $key;
}

/**
* Required, the model rules
*
* @return mixed
*/
abstract public function getRules();

/**
* Run preprocessing when required
*/
protected function beforeGetAttributes() {
return true;
}

/**
* Returns the params as array
*
Expand All @@ -85,16 +155,19 @@ abstract public function getRules();
public function getAttributes() {
$data = [];

foreach ($this->attributes as $attribute) {
if ($attribute->value instanceof \ArrayObject) {
foreach ($attribute->value as $item) {
$data[$this->getFormattedKey($attribute->key)][] = $item->getAttributes();
if ($this->beforeGetAttributes()) {

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 {
$attribute->validate();
$data[$this->getFormattedKey($attribute->key)] = $attribute->value;
}
} else if ($attribute->value instanceof Model) {
$data[$this->getFormattedKey($attribute->key)] = $attribute->value->getAttributes();
} else {
$attribute->validate();
$data[$this->getFormattedKey($attribute->key)] = $attribute->value;
}
}

Expand Down

0 comments on commit cf0006a

Please sign in to comment.