Skip to content

Commit

Permalink
Merge pull request #4 from afosto/hotfix/before-get-attributes
Browse files Browse the repository at this point in the history
breaking changes
  • Loading branch information
bakkerpeter authored Mar 7, 2017
2 parents 1fccb96 + e114242 commit 7da4235
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 31 deletions.
17 changes: 6 additions & 11 deletions src/Components/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Attribute {
/**
* @var Validator
*/
private $_validator;
public $validator;

/**
* Attribute constructor.
Expand All @@ -26,23 +26,18 @@ class Attribute {
* @param $rule
*/
public function __construct(Model $owner, $rule) {
$this->_validator = new Validator($owner, $rule);
$this->validator = new Validator($owner, $rule);
$this->key = current($rule);
$this->value = $this->_validator->getNewValue();
if ($this->validator->getRelationType() === Validator::TYPE_MANY) {
$this->value = new \ArrayObject();
}
}

/**
* Runs the validation for this attribute
*/
public function validate() {
$this->_validator->run();
}

/**
* @return Validator
*/
public function getValidator() {
return $this->_validator;
$this->validator->run();
}

}
64 changes: 55 additions & 9 deletions src/Components/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static function model() {
* @return mixed
*/
public function __get($name) {
if (isset($this->attributes[$name])) {
return $this->attributes[$name]->value;
if ($this->getAttribute($name, true)) {
return $this->getAttribute($name)->value;
}

return null;
Expand All @@ -50,11 +50,11 @@ public function __get($name) {
* @param $value
*/
public function __set($name, $value) {
if (isset($this->attributes[$name])) {
if ($this->attributes[$name] instanceof \ArrayObject) {
$this->attributes[$name]->value[] = $value;
if ($this->getAttribute($name)) {
if ($this->getAttribute($name) instanceof \ArrayObject) {
$this->getAttribute($name)->value[] = $value;
} else {
$this->attributes[$name]->value = $value;
$this->getAttribute($name)->value = $value;
}
}
}
Expand Down Expand Up @@ -128,10 +128,53 @@ public function setAttributes($data) {
$data = $data->getAttributes();
}
foreach ($data as $key => $value) {
$this->$key = $value;
if ($this->getAttributeRelation($key) === Validator::TYPE_MANY) {
foreach ($value as $subValue) {
$item = $this->getAttribute($key)->validator->getNewModel();
$item->setAttributes($subValue);
$this->{$key}[] = $item;
}
} else if ($this->getAttributeRelation($key) === Validator::TYPE_HAS_ONE) {
$newModel = $this->getAttribute($key)->validator->getNewModel();
$newModel->setAttributes($value);
$this->{$key} = $newModel;
} else {
$this->{$key} = $value;
}
}
}

/**
* @param $key
*
* @return integer
*/
protected function getAttributeRelation($key) {
if (isset($this->attributes[$this->getFormattedKey($key, true)])) {
$attribute = $this->attributes[$this->getFormattedKey($key, true)];

return $attribute->validator->getRelationType();
}

return Validator::TYPE_VALUE;
}

/**
* @param $key
*
* @return Attribute|bool
*/
protected function getAttribute($key) {
if (isset($this->attributes[$this->getFormattedKey($key, true)])) {
return $this->attributes[$this->getFormattedKey($key, true)];
}

return false;
}

/**
* @return array
*/
protected function getMap() {
return [];
}
Expand All @@ -140,11 +183,14 @@ protected function getMap() {
* Returns the mapped key
*
* @param $key
* @param $flip
*
* @return mixed
*/
protected function getFormattedKey($key) {
return array_key_exists($key, $this->getMap()) ? $this->getMap()[$key] : $key;
protected function getFormattedKey($key, $flip = false) {
$keys = (($flip) ? array_flip($this->getMap()) : $this->getMap());

return array_key_exists($key, $keys) ? $keys[$key] : $key;
}

/**
Expand Down
44 changes: 33 additions & 11 deletions src/Components/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Afosto\Bp\Components;

use Afosto\Bp\Exceptions\ValidationException;

class Validator {

/**
Expand Down Expand Up @@ -55,6 +57,12 @@ class Validator {
*/
private $_type;

/**
* Full namespaced path to the new model
* @var string
*/
private $_modelPath;

/**
* Validator constructor.
*
Expand All @@ -68,13 +76,27 @@ public function __construct(Model $owner, array $rule) {
list($key, $type, $required, $validation) = $rule;
$this->key = $key;
$this->_owner = $owner;
$this->_type = $type;

if (is_numeric($validation)) {
$this->_callable = [$this->_owner, 'validateMaxLength'];
$this->_callableParams = [$validation, $this->key];
} else if ($validation !== null && ($this->_getRelationType() == self::TYPE_VALUE || $this->_getRelationType() == self::TYPE_HAS_ONE)) {
} else if ($validation !== null && ($this->getRelationType() == self::TYPE_VALUE || $this->getRelationType() == self::TYPE_HAS_ONE)) {
$this->_callable = [$this->_owner, $validation];
}
$this->_type = $type;

if ($this->getRelationType() != self::TYPE_VALUE) {
$reflector = new \ReflectionClass($this->_owner);
if (substr($type, 0, 1) === '\\') {
$this->_modelPath = $this->_type;
} else {
if (substr($type, -2) === '[]') {
$this->_modelPath = $reflector->getNamespaceName() . '\\' . substr($this->_type, 0, -2);
} else {
$this->_modelPath = $reflector->getNamespaceName() . '\\' . $this->_type;
}
}
}
$this->_required = (bool)$required;
}

Expand All @@ -93,27 +115,27 @@ public function run() {
}

/**
* @return \ArrayObject|null
* @return Model
* @throws ValidationException
*/
public function getNewValue() {
if ($this->_getRelationType() == self::TYPE_MANY) {
return new \ArrayObject();
public function getNewModel() {
if (class_exists($this->_modelPath)) {
return new $this->_modelPath;
}

return null;
throw new ValidationException('Model ' . $this->_modelPath . ' not found');
}

/**
* @return int
* @return integer
*/
public function _getRelationType() {
if (in_array($this->_type, ['string', 'integer', 'float', 'boolean'])) {
public function getRelationType() {
if (in_array($this->_type, ['string', 'integer', 'float', 'boolean', '\DateTime'])) {
return self::TYPE_VALUE;
} else if (substr($this->_type, -2) == '[]') {
return self::TYPE_MANY;
} else {
return self::TYPE_HAS_ONE;
}
}

}

0 comments on commit 7da4235

Please sign in to comment.