| Total Complexity | 8 |
| Total Lines | 81 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 16 | abstract class Model |
||
| 17 | { |
||
| 18 | use HasValidator; |
||
| 19 | |||
| 20 | /** @var array */ |
||
| 21 | protected $rules = []; |
||
| 22 | /** @var array */ |
||
| 23 | protected $fields = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Valid current model & return data array. |
||
| 27 | * |
||
| 28 | * @return array |
||
| 29 | */ |
||
| 30 | public function getModel() |
||
| 31 | { |
||
| 32 | $this->valid($this->getData(), $this->getRules()); |
||
| 33 | |||
| 34 | return $this->getData(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get data of model. |
||
| 39 | * |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | public function getData(): array |
||
| 43 | { |
||
| 44 | return collect(\get_object_vars($this))->filter(function ($value, $name) { |
||
| 45 | return \in_array($name, $this->getFields()) && !empty($value); |
||
| 46 | })->map(function ($value, $name) { |
||
| 47 | $methodName = Str::camel('get_' . $name); |
||
| 48 | |||
| 49 | return \method_exists($this, $methodName) ? $this->$methodName() : $value; |
||
| 50 | })->toArray(); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get model fields. |
||
| 55 | * |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | public function getFields(): array |
||
| 59 | { |
||
| 60 | return $this->fields; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get list of rules array. |
||
| 65 | * |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | private function getRules(): array |
||
| 69 | { |
||
| 70 | return $this->rules; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * In valid function. |
||
| 75 | * |
||
| 76 | * @param array $errors |
||
| 77 | * |
||
| 78 | * @throws InvalidArgumentException |
||
| 79 | */ |
||
| 80 | public function InValidCallback(array $errors) |
||
| 81 | { |
||
| 82 | throw new InvalidArgumentException($errors[0]); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set Rules of current model. |
||
| 87 | * |
||
| 88 | * @param array $rules |
||
| 89 | * |
||
| 90 | * @return self |
||
| 91 | */ |
||
| 92 | public function setRules(array $rules): self |
||
| 97 | } |
||
| 98 | } |
||
| 99 |