-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuildModelData.php
More file actions
135 lines (107 loc) · 4.61 KB
/
BuildModelData.php
File metadata and controls
135 lines (107 loc) · 4.61 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
<?php
declare(strict_types=1);
namespace Phenix\Database\Models\Concerns;
use Phenix\Database\Exceptions\ModelException;
use Phenix\Database\Models\Attributes\BelongsTo as BelongsToAttribute;
use Phenix\Database\Models\Attributes\BelongsToMany as BelongsToManyAttribute;
use Phenix\Database\Models\Attributes\Column;
use Phenix\Database\Models\Attributes\DateTime;
use Phenix\Database\Models\Attributes\HasMany as HasManyAttribute;
use Phenix\Database\Models\Attributes\ModelAttribute;
use Phenix\Database\Models\Properties\BelongsToManyProperty;
use Phenix\Database\Models\Properties\BelongsToProperty;
use Phenix\Database\Models\Properties\HasManyProperty;
use Phenix\Database\Models\Properties\ModelProperty;
use Phenix\Database\Models\Relationships\BelongsTo;
use Phenix\Database\Models\Relationships\BelongsToMany;
use Phenix\Database\Models\Relationships\HasMany;
use Phenix\Util\Arr;
use Phenix\Util\Date;
use ReflectionAttribute;
use ReflectionObject;
use ReflectionProperty;
use function array_filter;
use function array_map;
use function array_shift;
trait BuildModelData
{
protected function buildPropertyBindings(): array
{
$reflection = new ReflectionObject($this);
$bindings = [];
foreach ($reflection->getProperties() as $property) {
$attributes = array_map(function (ReflectionAttribute $attr): object {
return $attr->newInstance();
}, $property->getAttributes());
/** @var array<int, ModelAttribute&Column> $attributes */
$attributes = array_filter($attributes, fn (object $attr) => $attr instanceof ModelAttribute);
if (empty($attributes)) {
continue;
}
$attribute = array_shift($attributes);
$columnName = $attribute->getColumnName() ?? $property->getName();
$bindings[$columnName] = $this->buildModelProperty($attribute, $property);
}
return $bindings;
}
protected function buildRelationshipBindings(): array
{
$relationships = [];
foreach ($this->getPropertyBindings() as $property) {
if ($property instanceof BelongsToProperty) {
$relationships[$property->getName()] = $this->buildBelongsToRelationship($property);
} elseif ($property instanceof HasManyProperty) {
$relationships[$property->getName()] = new HasMany($property);
} elseif ($property instanceof BelongsToManyProperty) {
$relationships[$property->getName()] = new BelongsToMany($property);
}
}
return $relationships;
}
protected function buildModelProperty(ModelAttribute&Column $attribute, ReflectionProperty $property): ModelProperty
{
$arguments = [
$property->getName(),
(string) $property->getType(),
class_exists(ltrim((string) $property->getType(), '?')),
$attribute,
$property->isInitialized($this) ? $property->getValue($this) : null,
];
return match($attribute::class) {
BelongsToAttribute::class => new BelongsToProperty(...$arguments),
HasManyAttribute::class => new HasManyProperty(...$arguments),
BelongsToManyAttribute::class => new BelongsToManyProperty(...$arguments),
default => new ModelProperty(...$arguments),
};
}
protected function buildBelongsToRelationship(BelongsToProperty $property): BelongsTo
{
$foreignKey = Arr::first($this->getPropertyBindings(), function (ModelProperty $modelProperty) use ($property): bool {
return $property->getAttribute()->foreignProperty === $modelProperty->getName();
});
if (! $foreignKey) {
throw new ModelException("Foreign key not found for {$property->getName()} relationship.");
}
return new BelongsTo($property, $foreignKey);
}
/**
* @return array<string, mixed>
*/
protected function buildSavingData(): array
{
$data = [];
foreach ($this->getPropertyBindings() as $property) {
$propertyName = $property->getName();
$attribute = $property->getAttribute();
if (isset($this->{$propertyName})) {
$data[$property->getColumnName()] = $this->{$propertyName};
}
if ($attribute instanceof DateTime && $attribute->autoInit && ! isset($this->{$propertyName})) {
$now = Date::now();
$data[$property->getColumnName()] = $now->format($attribute->format);
$this->{$propertyName} = $now;
}
}
return $data;
}
}