Last active
March 24, 2021 09:05
-
-
Save mass6/8fabc87f3033f64f399849122dea7ba7 to your computer and use it in GitHub Desktop.
Self validation DTO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Str; | |
use Illuminate\Validation\Validator; | |
use Illuminate\Validation\ValidationException; | |
use Illuminate\Support\Facades\Validator as ValidatorFactory; | |
/** | |
* Class CoreCTO | |
*/ | |
abstract class CoreDTO | |
{ | |
/** | |
* Whether debug mode. | |
*/ | |
const DEBUG = false; | |
/** | |
* Data passed into the DTO. | |
* | |
* @var array | |
*/ | |
protected $data; | |
/** | |
* @var array | |
*/ | |
protected $allowed = []; | |
/** | |
* Attributes that should be returned as Carbon date objects. | |
* | |
* @var array | |
*/ | |
protected $dates = []; | |
/** | |
* CoreDTO constructor. | |
* | |
* @param array $data | |
* @throws ValidationException | |
*/ | |
public function __construct(array $data) | |
{ | |
$this->setData($data); | |
$this->validate(); | |
} | |
/** | |
* @param array $data | |
*/ | |
protected function setData(array $data) | |
{ | |
collect($data)->each(function ($value, $key) { | |
if ($this->attributeIsAllowed($key)) { | |
$this->setKey($key, $value); | |
} | |
}); | |
} | |
/** | |
* @param $key | |
* | |
* @return bool | |
*/ | |
protected function attributeIsAllowed($key) | |
{ | |
return in_array($key, $this->allowed); | |
} | |
/** | |
* @throws ValidationException | |
*/ | |
protected function validate() | |
{ | |
$validator = ValidatorFactory::make($this->data, $this->getRules(), $this->getMessages()); | |
$this->getAfterValidationRules($validator); | |
if ($validator->fails()) { | |
if (static::DEBUG) { | |
dump($validator->errors()); | |
} | |
throw new ValidationException($validator); | |
} | |
} | |
/** | |
* @param Validator $validator | |
*/ | |
protected function getAfterValidationRules(Validator $validator) | |
{ | |
} | |
/** | |
* Return a rules array for validation. | |
* | |
* @return array | |
*/ | |
abstract protected function getRules(); | |
/** | |
* Get custom messages for validator errors. | |
* | |
* @return array | |
*/ | |
protected function getMessages() | |
{ | |
return []; | |
} | |
/** | |
* @param $key | |
* @param $value | |
*/ | |
protected function setKey($key, $value) | |
{ | |
$this->data[$key] = $value; | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| Getters | |
|-------------------------------------------------------------------------- | |
| | |
*/ | |
/** | |
* @return array | |
*/ | |
public function getData() | |
{ | |
return $this->data; | |
} | |
/** | |
* @param $name | |
* | |
* @return mixed | |
*/ | |
public function __get($name) | |
{ | |
if (array_key_exists($name, $this->data)) { | |
return $this->data[$name]; | |
} else if (array_key_exists(Str::snake($name), $this->data)) { | |
return $this->data[Str::snake($name)]; | |
} else if (array_key_exists(Str::camel($name), $this->data)) { | |
return $this->data[Str::camel($name)]; | |
} | |
} | |
/** | |
* @param $name | |
* @param $arguments | |
* @return mixed | |
*/ | |
public function __call($name, $arguments) | |
{ | |
if (property_exists($this, $name)) { | |
return $this->{$name}; | |
} else if (method_exists($this, $name)) { | |
return $this->{$name}(); | |
} else if (substr($name, 0, 3) === 'get') { | |
$property = Str::camel(substr($name, 3)); | |
return $this->{$property}; | |
} else { | |
return $this->{$name}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment