Skip to content

Commit

Permalink
Basics
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkerpeter committed Dec 15, 2016
1 parent 493e107 commit 883a57b
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"Afosto\\Bp\\": "src"
}
}
}
}
48 changes: 48 additions & 0 deletions src/Components/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Afosto\Bp\Components;

class Attribute {

/**
* @var string
*/
public $key;

/**
* @var \ArrayObject|null|Model
*/
public $value;

/**
* @var Validator
*/
private $_validator;

/**
* Attribute constructor.
*
* @param Model $owner
* @param $rule
*/
public function __construct(Model $owner, $rule) {
$this->_validator = new Validator($owner, $rule);
$this->key = current($rule);
$this->value = $this->_validator->getNewValue();
}

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

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

}
104 changes: 104 additions & 0 deletions src/Components/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Afosto\Bp\Components;

use Afosto\Bp\Exceptions\ValidationException;

abstract class Model {

/**
* @var Attribute[]
*/
protected $attributes = [];

public static function getDocBlock() {
$model = new static();
foreach ($model->getRules() as $rule) {
echo "* @property {$rule[1]} \${$rule[0]}\n";
}
}

/**
* @param $name
*
* @return mixed
*/
public function __get($name) {
if (isset($this->attributes[$name])) {
return $this->attributes[$name]->value;
}
}

/**
* @param $name
* @param $value
*/
public function __set($name, $value) {
if (isset($this->attributes[$name])) {
if ($this->attributes[$name] instanceof \ArrayObject) {
$this->attributes[$name]->value[] = $value;
} else {
$this->attributes[$name]->value = $value;
}
}
}

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

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");
}
}

public function validateRequired($key) {
if ($this->$key === null) {
throw new ValidationException("{$key} is required");
}
}

public function validateDouble($key) {
$this->$key = number_format($this->$key, 2, '.', '');
}

/**
* @param array $data
*/
public function setAttributes(array $data) {
foreach ($data as $key => $value) {
$this->$key = $value;
}
}

abstract public function getRules();

/**
* Returns the params as array
*
* @return array
*/
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();
}
} else if ($attribute->value instanceof Model) {
$data[$this->getFormattedKey($attribute->key)] = $attribute->value->getAttributes();
} else {
$attribute->validate();
$data[$this->getFormattedKey($attribute->key)] = $attribute->value;
}
}

return $data;
}

}
119 changes: 119 additions & 0 deletions src/Components/Validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Afosto\Bp\Components;

class Validator {

/**
* Default type
*/
const TYPE_VALUE = 1;

/**
* Many relation type
*/
const TYPE_MANY = 2;

/**
* One relation type
*/
const TYPE_HAS_ONE = 3;

/**
* The attribute key
* @var string
*/
public $key;

/**
* Owner for this attribute
* @var Model
*/
private $_owner;

/**
* Used to call proper validation function
* @var array
*/
private $_callable;

/**
* Optional
* @var array
*/
private $_callableParams = [];

/**
* True for required attribute
* @var bool
*/
private $_required;

/**
* Integer, float, string, boolean
* @var string
*/
private $_type;

/**
* Validator constructor.
*
* @param Model $owner
* @param $rule
*/
public function __construct(Model $owner, array $rule) {
if (count($rule) == 3) {
$rule[] = null;
}
list($key, $type, $required, $validation) = $rule;
$this->key = $key;
$this->_owner = $owner;
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)) {
$this->_callable = [$this->_owner, $validation];
}
$this->_type = $type;
$this->_required = (bool)$required;
}

/**
* Run the validation
*/
public function run() {
if ($this->_required) {
$this->_owner->validateRequired($this->key);
}

//Call the validation rule
if ($this->_owner->{$this->key} !== null && $this->_callable !== null) {
call_user_func_array($this->_callable, $this->_callableParams);
}
}

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

return null;
}

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

}
7 changes: 7 additions & 0 deletions src/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Afosto\Bp\Exceptions;

class ValidationException extends \Exception {

}

0 comments on commit 883a57b

Please sign in to comment.