-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractEntity.php
More file actions
171 lines (132 loc) · 4.43 KB
/
AbstractEntity.php
File metadata and controls
171 lines (132 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php declare(strict_types=1);
namespace Parable\Orm;
use Parable\Di\Container;
use Parable\Orm\PropertyTypes\PropertyTypeDeterminer;
abstract class AbstractEntity
{
/** @var string[] */
protected array $originalProperties = [];
public function getPrimaryKey(string $key)
{
$this->validatePrimaryKeyExistsOnEntity($key);
return $this->{$key};
}
public function setPrimaryKey(string $key, $value): void
{
$this->{$key} = $value;
}
public function hasBeenMarkedAsOriginal(): bool
{
return $this->originalProperties !== [];
}
public static function fromDatabaseItem(Container $container, string $primaryKey, array $values): self
{
$entity = $container->build(static::class);
$entity->validatePrimaryKeyExistsOnEntity($primaryKey);
if (!isset($values[$primaryKey])) {
throw new OrmException(sprintf(
"Could not set primary key '%s' on entity %s from database values",
$primaryKey,
static::class
));
}
$primaryKeyValue = PropertyTypeDeterminer::typeProperty($entity, $primaryKey, $values[$primaryKey] ?? null);
$entity->setPrimaryKey($primaryKey, $primaryKeyValue ?? null);
foreach ($values as $property => $value) {
if (!property_exists($entity, $property)) {
throw new OrmException(sprintf(
"Property '%s' does not exist on entity %s",
$property,
static::class
));
}
if ($property === $primaryKey || $value === null) {
continue;
}
$setter = $entity->getSetterForProperty($property);
$entity->{$setter}(PropertyTypeDeterminer::typeProperty($entity, $property, $value));
}
$entity->markAsOriginal();
return $entity;
}
public function toArray(): array
{
$array = (array)$this;
$filtered = [];
foreach ($array as $key => $value) {
$key = str_replace('*', '', $key);
$key = trim(str_replace(static::class, '', $key));
$value = PropertyTypeDeterminer::untypeProperty($this, $key, $value);
if ($value !== null && !is_scalar($value)) {
continue;
}
$filtered[$key] = $value;
}
return $filtered;
}
public function toArrayWithout(string ...$keys): array
{
$array = $this->toArray();
foreach ($keys as $key) {
unset($array[$key]);
}
return $array;
}
public function toArrayWithoutEmptyValues(): array
{
$array = $this->toArray();
foreach ($array as $key => $value) {
// We don't want values 0 or '0' to be seen as empty, so we ctype_digit it
if (empty($value) && !ctype_digit($value)) {
unset($array[$key]);
}
}
return $array;
}
public function toArrayWithOnlyChanges(): array
{
$array = $this->toArray();
foreach ($array as $key => $value) {
if (array_key_exists($key, $this->originalProperties)
&& $this->originalProperties[$key] === $value
) {
unset($array[$key]);
}
}
return $array;
}
public function markAsOriginal(): void
{
$this->originalProperties = $this->toArray();
}
protected function getSetterForProperty(string $property): string
{
if (str_contains($property, '_')) {
$setter = 'set';
$propertyParts = explode('_', $property);
foreach ($propertyParts as $propertyPart) {
$setter .= ucfirst($propertyPart);
}
} else {
$setter = 'set' . ucfirst($property);
}
if (!method_exists($this, $setter)) {
throw new OrmException(sprintf(
"Setter method '%s' not defined on entity %s",
$setter,
static::class
));
}
return $setter;
}
protected function validatePrimaryKeyExistsOnEntity(string $key): void
{
if (!property_exists($this, $key)) {
throw new OrmException(sprintf(
"Primary key property '%s' does not exist on entity %s",
$key,
static::class
));
}
}
}