-
Notifications
You must be signed in to change notification settings - Fork 4
/
DefinitionBuilder.php
227 lines (194 loc) · 8.13 KB
/
DefinitionBuilder.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
namespace Pulsar;
use ICanBoogie\Inflector;
use Pulsar\Relation\Relationship;
final class DefinitionBuilder
{
/** @var Definition[] */
private static array $definitions = [];
/**
* Gets the definition for a model. If needed the
* definition will be generated. It will only be
* generated once.
*/
public static function get(string $modelClass): Definition
{
/** @var Model $modelClass */
if (!isset(self::$definitions[$modelClass])) {
self::$definitions[$modelClass] = $modelClass::buildDefinition();
}
return self::$definitions[$modelClass];
}
/**
* Builds a model definition given certain parameters.
*
* @param string[] $ids
* @param Property[] $properties
*/
public static function build(array $ids, array $properties, string $modelClass): Definition
{
// add in the default ID property
if (!isset($properties[Model::DEFAULT_ID_NAME]) && $ids == [Model::DEFAULT_ID_NAME]) {
$properties[Model::DEFAULT_ID_NAME] = new Property(
type: Type::INTEGER,
mutable: Property::IMMUTABLE,
);
}
$result = [];
foreach ($properties as $k => $property) {
// convert to an array in order to fill in additional settings
$hasDefault = $property->hasDefault;
$property = $property->toArray();
if (!$hasDefault) {
unset($property['default']);
}
// handle relationship shortcuts
$relationType = $property['relation_type'] ?? null;
if (isset($property['relation']) && !$relationType) {
self::buildBelongsToLegacy($k, $property);
} elseif (isset($property['belongs_to']) || Relationship::BELONGS_TO == $relationType) {
self::buildBelongsTo($k, $property, $result);
} elseif (isset($property['has_one']) || Relationship::HAS_ONE == $relationType) {
self::buildHasOne($property, $modelClass);
} elseif (isset($property['belongs_to_many']) || Relationship::BELONGS_TO_MANY == $relationType) {
self::buildBelongsToMany($property, $modelClass);
} elseif (isset($property['has_many']) || Relationship::HAS_MANY == $relationType) {
self::buildHasMany($property, $modelClass);
} elseif (isset($property['morphs_to'])) {
self::buildPolymorphic($property, $k);
}
// install validation rule for encrypted properties
if (isset($property['encrypted']) && $property['encrypted'] && !isset($property['validate'])) {
$property['validate'] = 'encrypt';
}
$property['name'] = $k;
$result[$k] = new Property(...$property);
}
// order the properties array by name for consistency
// since it is constructed in a random order
ksort($result);
return new Definition($ids, $result);
}
//
// Relationship Shortcuts
//
/**
* This is added for BC with older versions of pulsar
* that only supported belongs to relationships.
*/
private static function buildBelongsToLegacy(string $name, array &$property): void
{
$property['relation_type'] = Relationship::BELONGS_TO;
// in the legacy configuration the default local key was the property name
$property['local_key'] = $property['local_key'] ?? $name;
// the default foreign key is `id`
if (!isset($property['foreign_key'])) {
$property['foreign_key'] = Model::DEFAULT_ID_NAME;
}
}
private static function buildBelongsTo(string $name, array &$property, array &$result): void
{
// the default local key would look like `user_id`
// for a property named `user`
if (!isset($property['local_key'])) {
$property['local_key'] = $name.'_id';
}
// the default foreign key is `id`
if (!isset($property['foreign_key'])) {
$property['foreign_key'] = Model::DEFAULT_ID_NAME;
}
// when a belongs_to relationship is used then we automatically add a
// new property for the ID field which gets persisted to the DB
if (!isset($result[$property['local_key']])) {
$result[$property['local_key']] = new Property(
name: $property['local_key'],
type: Type::INTEGER,
mutable: $property['mutable'] ?? Property::MUTABLE,
);
}
}
private static function buildBelongsToMany(array &$property, string $modelClass): void
{
/* @var Model $modelClass */
// the default local key would look like `user_id`
// for a model named User
if (!isset($property['local_key'])) {
$inflector = Inflector::get();
$property['local_key'] = strtolower($inflector->underscore($modelClass::modelName())).'_id';
}
if (!isset($property['foreign_key'])) {
$inflector = Inflector::get();
$property['foreign_key'] = strtolower($inflector->underscore($property['relation']::modelName())).'_id';
}
// the default pivot table name looks like
// RoleUser for models named Role and User.
// the tablename is built from the model names
// in alphabetic order.
if (!isset($property['pivot_tablename'])) {
$names = [
$modelClass::modelName(),
$property['relation']::modelName(),
];
sort($names);
$property['pivot_tablename'] = implode($names);
}
}
private static function buildHasOne(array &$property, string $modelClass): void
{
/* @var Model $modelClass */
// the default foreign key would look like `user_id`
// for a model named User
if (!isset($property['foreign_key'])) {
$inflector = Inflector::get();
$property['foreign_key'] = strtolower($inflector->underscore($modelClass::modelName())).'_id';
}
if (!isset($property['local_key'])) {
$property['local_key'] = Model::DEFAULT_ID_NAME;
}
}
private static function buildHasMany(array &$property, string $modelClass): void
{
/* @var Model $modelClass */
// the default foreign key would look like
// `user_id` for a model named User
if (!isset($property['foreign_key'])) {
$inflector = Inflector::get();
$property['foreign_key'] = strtolower($inflector->underscore($modelClass::modelName())).'_id';
}
// the default local key is `id`
if (!isset($property['local_key'])) {
$property['local_key'] = Model::DEFAULT_ID_NAME;
}
}
private static function buildPolymorphic(array &$property, string $name): void
{
/* @var Model $modelClass */
$property['relation_type'] = Relationship::POLYMORPHIC;
$property['persisted'] = false;
$property['in_array'] = false;
// the default foreign key is `id`
if (!isset($property['foreign_key'])) {
$property['foreign_key'] = Model::DEFAULT_ID_NAME;
}
// the default local key type is the property name
if (!isset($property['local_key'])) {
$property['local_key'] = $name;
}
// when a polymorhpic relationship is used then we automatically add a
// new property for the type and ID fields which gets persisted to the DB
if (!isset($result[$property['local_key'].'_type'])) {
$result[$property['local_key'].'_type'] = new Property(
name: $property['local_key'].'_type',
type: Type::STRING,
mutable: $property['mutable'] ?? Property::MUTABLE,
);
}
if (!isset($result[$property['local_key'].'_id'])) {
$result[$property['local_key'].'_id'] = new Property(
name: $property['local_key'].'_id',
type: Type::INTEGER,
mutable: $property['mutable'] ?? Property::MUTABLE,
);
}
}
}