| 1 |
<?php |
| 2 |
|
| 3 |
namespace App\Billingo\Models; |
| 4 |
|
| 5 |
use App\Billingo\Contracts\SelfControlInterface; |
| 6 |
use App\Billingo\Contracts\SerializableInterface; |
| 7 |
use App\Billingo\Contracts\ValidableInterface; |
| 8 |
use App\Billingo\Enums\BillingoErrorEnum; |
| 9 |
use App\Billingo\Error\BillingoError; |
| 10 |
use App\Billingo\Exceptions\EmptyObjectException; |
| 11 |
use App\Billingo\Exceptions\ValidationException; |
| 12 |
use App\Billingo\Traits\WithSelfControl; |
| 13 |
|
| 14 |
abstract class BillingoModel implements SerializableInterface, SelfControlInterface |
| 15 |
{ |
| 16 |
use WithSelfControl; |
| 17 |
|
| 18 |
protected ValidableInterface $validator; |
| 19 |
protected array $cast = []; |
| 20 |
protected array $properties; |
| 21 |
|
| 22 |
public function __construct(array $data = [], ?ValidableInterface $validator = null) |
| 23 |
{ |
| 24 |
$this->validator = $validator ?? $this->getValidator(); |
| 25 |
|
| 26 |
$this->fromArray($data); |
| 27 |
} |
| 28 |
|
| 29 |
abstract protected function getValidator(): ValidableInterface; |
| 30 |
|
| 31 |
public function toArray(): array |
| 32 |
{ |
| 33 |
$toArray = $this->properties; |
| 34 |
|
| 35 |
foreach ($toArray as $key => $value) { |
| 36 |
$toArray[$key] = $this->serializeValue($value); |
| 37 |
} |
| 38 |
|
| 39 |
return $toArray; |
| 40 |
} |
| 41 |
|
| 42 |
public function fromArray(array $data): self |
| 43 |
{ |
| 44 |
if (empty($data)) { |
| 45 |
$this->properties = []; |
| 46 |
} |
| 47 |
|
| 48 |
$withClasses = $this->createClasses($data, $this->cast); |
| 49 |
|
| 50 |
$validated = []; |
| 51 |
|
| 52 |
try { |
| 53 |
|
| 54 |
$validated = $this->validator->validate($withClasses); |
| 55 |
} catch (EmptyObjectException $exception) { |
| 56 |
$this->validator->setErrors(BillingoErrorEnum::EMPTY_OBJECT); |
| 57 |
} catch (ValidationException $exception) { |
| 58 |
$this->validator->setErrors(BillingoErrorEnum::VALIDATION_FAILED, $exception->getErrors()); |
| 59 |
} |
| 60 |
|
| 61 |
$this->properties = $validated; |
| 62 |
|
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
private function serializeValue($value): mixed |
| 67 |
{ |
| 68 |
if (is_object($value)) { |
| 69 |
return $value instanceof SerializableInterface ? $value->toArray() : $value; |
| 70 |
} elseif (is_array($value)) { |
| 71 |
return array_map( |
| 72 |
fn($item) => $item instanceof SerializableInterface ? $item->toArray() : $item, |
| 73 |
$value |
| 74 |
); |
| 75 |
} |
| 76 |
return $value; |
| 77 |
} |
| 78 |
|
| 79 |
protected function createClasses(array $data, array $castingList): array |
| 80 |
{ |
| 81 |
$convertedData = []; |
| 82 |
|
| 83 |
foreach ($data as $key => $value) { |
| 84 |
$convertedData[$key] = $this->convertValue($key, $value, $castingList); |
| 85 |
} |
| 86 |
|
| 87 |
return $convertedData; |
| 88 |
} |
| 89 |
|
| 90 |
private function convertValue(string $key, mixed $value, array $castingList): mixed |
| 91 |
{ |
| 92 |
if (is_object($value)) { |
| 93 |
|
| 94 |
return $value; |
| 95 |
} elseif (isset($castingList[$key])) { |
| 96 |
if (is_array($castingList[$key])) { |
| 97 |
|
| 98 |
return array_map( |
| 99 |
fn($item) => is_object($item) ? $item : new $castingList[$key][0]($item), |
| 100 |
$value |
| 101 |
); |
| 102 |
} else { |
| 103 |
|
| 104 |
return $value ? new $castingList[$key]($value) : null; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return $value; |
| 109 |
} |
| 110 |
|
| 111 |
public function getErrors(): ?BillingoError |
| 112 |
{ |
| 113 |
return $this->validator->getErrors(); |
| 114 |
} |
| 115 |
|
| 116 |
public function __get(string $name): mixed |
| 117 |
{ |
| 118 |
return $this->properties[$name] ?? null; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
public function __set(string $name, mixed $value): void |
| 123 |
{ |
| 124 |
if (!$this->validator->validateProperty($name, $value)) { |
| 125 |
|
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if (isset($this->properties[$name])) { |
| 130 |
|
| 131 |
$this->properties[$name] = $value; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|