@omnifyjp/ts 2.1.2 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -276,6 +276,13 @@ function buildDocProperties(properties, expandedProperties, propertyOrder) {
|
|
|
276
276
|
const phpType = nullable ? 'int|null' : 'int';
|
|
277
277
|
lines.push(` * @property ${phpType} $${snakeName}`);
|
|
278
278
|
}
|
|
279
|
+
else if (relation === 'MorphTo') {
|
|
280
|
+
const morphName = prop['morphName'] || toSnakeCase(propName);
|
|
281
|
+
const nullable = prop['nullable'] ?? true;
|
|
282
|
+
const nullSuffix = nullable ? '|null' : '';
|
|
283
|
+
lines.push(` * @property string${nullSuffix} $${morphName}_type`);
|
|
284
|
+
lines.push(` * @property string${nullSuffix} $${morphName}_id`);
|
|
285
|
+
}
|
|
279
286
|
continue;
|
|
280
287
|
}
|
|
281
288
|
if (expandedProperties[propName]) {
|
|
@@ -329,6 +336,11 @@ function buildFillable(properties, expandedProperties, propertyOrder) {
|
|
|
329
336
|
if (relation === 'ManyToOne') {
|
|
330
337
|
fields.push(toSnakeCase(propName) + '_id');
|
|
331
338
|
}
|
|
339
|
+
else if (relation === 'MorphTo') {
|
|
340
|
+
const morphName = prop['morphName'] || toSnakeCase(propName);
|
|
341
|
+
fields.push(morphName + '_type');
|
|
342
|
+
fields.push(morphName + '_id');
|
|
343
|
+
}
|
|
332
344
|
continue;
|
|
333
345
|
}
|
|
334
346
|
if (expandedProperties[propName]) {
|
|
@@ -30,6 +30,12 @@ export function buildRelation(propName, property, modelNamespace, context) {
|
|
|
30
30
|
case 'MorphMany':
|
|
31
31
|
method = morphMany(propName, property, target, modelNamespace);
|
|
32
32
|
break;
|
|
33
|
+
case 'MorphToMany':
|
|
34
|
+
method = morphToMany(propName, property, target, modelNamespace, context);
|
|
35
|
+
break;
|
|
36
|
+
case 'MorphedByMany':
|
|
37
|
+
method = morphedByMany(propName, property, target, modelNamespace);
|
|
38
|
+
break;
|
|
33
39
|
}
|
|
34
40
|
if (method === null)
|
|
35
41
|
return null;
|
|
@@ -50,6 +56,8 @@ export function getReturnType(relation) {
|
|
|
50
56
|
case 'MorphTo': return 'MorphTo';
|
|
51
57
|
case 'MorphOne': return 'MorphOne';
|
|
52
58
|
case 'MorphMany': return 'MorphMany';
|
|
59
|
+
case 'MorphToMany': return 'MorphToMany';
|
|
60
|
+
case 'MorphedByMany': return 'MorphedByMany';
|
|
53
61
|
default: return 'BelongsTo';
|
|
54
62
|
}
|
|
55
63
|
}
|
|
@@ -153,3 +161,30 @@ function morphMany(propName, property, target, ns) {
|
|
|
153
161
|
return $this->morphMany(${fqcn}::class, '${morphName}');
|
|
154
162
|
}`;
|
|
155
163
|
}
|
|
164
|
+
function morphToMany(propName, property, target, ns, context) {
|
|
165
|
+
const morphName = property['morphName'] ?? toSnakeCase(propName);
|
|
166
|
+
const joinTable = property['joinTable'] ?? (morphName + 's');
|
|
167
|
+
const methodName = toCamelCase(propName);
|
|
168
|
+
const fqcn = `\\${ns}\\${target}`;
|
|
169
|
+
return ` /**
|
|
170
|
+
* The ${propName} that belong to this model.
|
|
171
|
+
*/
|
|
172
|
+
public function ${methodName}(): MorphToMany
|
|
173
|
+
{
|
|
174
|
+
return $this->morphToMany(${fqcn}::class, '${morphName}', '${joinTable}')
|
|
175
|
+
->withTimestamps();
|
|
176
|
+
}`;
|
|
177
|
+
}
|
|
178
|
+
function morphedByMany(propName, property, target, ns) {
|
|
179
|
+
const morphName = property['morphName'] ?? toSnakeCase(propName);
|
|
180
|
+
const methodName = toCamelCase(propName);
|
|
181
|
+
const fqcn = `\\${ns}\\${target}`;
|
|
182
|
+
return ` /**
|
|
183
|
+
* The ${propName} that belong to this model.
|
|
184
|
+
*/
|
|
185
|
+
public function ${methodName}(): MorphedByMany
|
|
186
|
+
{
|
|
187
|
+
return $this->morphedByMany(${fqcn}::class, '${morphName}')
|
|
188
|
+
->withTimestamps();
|
|
189
|
+
}`;
|
|
190
|
+
}
|
|
@@ -73,17 +73,34 @@ function generateBaseRequest(name, schema, reader, config, action) {
|
|
|
73
73
|
}
|
|
74
74
|
continue;
|
|
75
75
|
}
|
|
76
|
-
//
|
|
76
|
+
// Association handling
|
|
77
77
|
if (type === 'Association') {
|
|
78
78
|
const relation = prop['relation'] ?? '';
|
|
79
|
-
if (relation
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
if (relation === 'ManyToOne') {
|
|
80
|
+
const snakeName = toSnakeCase(propName) + '_id';
|
|
81
|
+
const rules = isUpdate
|
|
82
|
+
? toUpdateRules(prop, tableName, modelRouteParam)
|
|
83
|
+
: toStoreRules(prop, tableName);
|
|
84
|
+
rulesLines.push(` '${snakeName}' => ${formatRules(rules)},`);
|
|
85
|
+
attributeKeys.push(snakeName);
|
|
86
|
+
}
|
|
87
|
+
else if (relation === 'MorphTo') {
|
|
88
|
+
const morphName = prop['morphName'] || toSnakeCase(propName);
|
|
89
|
+
const nullable = prop['nullable'] ?? true;
|
|
90
|
+
const requiredRule = nullable ? 'nullable' : 'required';
|
|
91
|
+
const targets = prop['targets'] ?? [];
|
|
92
|
+
// {morphName}_type rules
|
|
93
|
+
const typeRules = [requiredRule, 'string'];
|
|
94
|
+
if (targets.length > 0) {
|
|
95
|
+
typeRules.push(`in:${targets.join(',')}`);
|
|
96
|
+
}
|
|
97
|
+
rulesLines.push(` '${morphName}_type' => [${typeRules.map(r => `'${r}'`).join(', ')}],`);
|
|
98
|
+
attributeKeys.push(morphName + '_type');
|
|
99
|
+
// {morphName}_id rules
|
|
100
|
+
const idRules = [requiredRule, 'string'];
|
|
101
|
+
rulesLines.push(` '${morphName}_id' => [${idRules.map(r => `'${r}'`).join(', ')}],`);
|
|
102
|
+
attributeKeys.push(morphName + '_id');
|
|
103
|
+
}
|
|
87
104
|
continue;
|
|
88
105
|
}
|
|
89
106
|
// File type: generate array item rules for multiple files
|