1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Blackmine\Model; |
||
6 | |||
7 | use Blackmine\Mutator\ModelMutator; |
||
8 | use Blackmine\Mutator\MutableInterface; |
||
9 | use Carbon\CarbonImmutable; |
||
10 | use Error; |
||
11 | use JsonException; |
||
12 | use Blackmine\Collection\IdentityCollection; |
||
13 | |||
14 | /** |
||
15 | * @method mixed getId() |
||
16 | */ |
||
17 | abstract class AbstractModel implements ModelInterface |
||
18 | { |
||
19 | use ModelTrait; |
||
20 | |||
21 | public function fromArray(array $data): self |
||
22 | { |
||
23 | foreach ($data as $key => $value) { |
||
24 | $setter = $this->getSetter($key); |
||
25 | $this->$setter($value); |
||
26 | } |
||
27 | |||
28 | return $this; |
||
29 | } |
||
30 | |||
31 | public function toArray(): array |
||
32 | { |
||
33 | $clone = (array) $this; |
||
34 | $ret = []; |
||
35 | |||
36 | foreach ($clone as $key => $value) { |
||
37 | if ($value !== null) { |
||
38 | $aux = explode("\0", $key); |
||
39 | $new_key = $aux[count($aux) - 1]; |
||
40 | |||
41 | $getter = $this->getGetter($new_key); |
||
42 | $value = $this->$getter(); |
||
43 | |||
44 | if ($value instanceof Identity) { |
||
45 | $new_key .= "_id"; |
||
46 | $value = $value->getId(); |
||
47 | } |
||
48 | |||
49 | if ($value instanceof CarbonImmutable) { |
||
50 | $value = $value->format("Y-m-d"); |
||
51 | } |
||
52 | |||
53 | if ($value instanceof IdentityCollection) { |
||
54 | $value = $value->toArray(); |
||
55 | } |
||
56 | |||
57 | $ret[$new_key] = $value instanceof ModelInterface ? $value->toArray() : $value; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | return $ret; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @throws JsonException |
||
66 | */ |
||
67 | public function toJson(): string |
||
68 | { |
||
69 | return json_encode($this, JSON_THROW_ON_ERROR); |
||
70 | } |
||
71 | |||
72 | |||
73 | public function jsonSerialize(): ?array |
||
74 | { |
||
75 | return $this->toArray(); |
||
76 | } |
||
77 | |||
78 | public function getPayload(): array |
||
79 | { |
||
80 | $payload = $this->toArray(); |
||
81 | |||
82 | if ($this->isMutable()) { |
||
83 | /** @var MutableInterface $this */ |
||
84 | $mutator = new ModelMutator($this); |
||
85 | $payload = $mutator->mutate(); |
||
86 | } |
||
87 | |||
88 | return [$this->getEntityName() => $payload]; |
||
89 | } |
||
90 | |||
91 | public function getEntityName(): string |
||
92 | { |
||
93 | if (defined('static::ENTITY_NAME')) { |
||
94 | return static::ENTITY_NAME; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
95 | } |
||
96 | |||
97 | throw new Error('Mandatory constant ENTITY_NAME not defined in model class: ' . get_class($this)); |
||
98 | } |
||
99 | |||
100 | public function isPersisted(): bool |
||
101 | { |
||
102 | if (property_exists($this, "id")) { |
||
103 | return $this->id !== null; |
||
104 | } |
||
105 | |||
106 | return false; |
||
107 | } |
||
108 | |||
109 | public function isMutable(): bool |
||
110 | { |
||
111 | return in_array(MutableInterface::class, class_implements($this), true); |
||
112 | } |
||
113 | |||
114 | public function isCacheable(): bool |
||
115 | { |
||
116 | return property_exists($this, "id"); |
||
117 | } |
||
118 | } |
||
119 |