|
1 | | -// Builds the template context for resource object files (src/Objects/{Name}.php). |
2 | | -// Mirrors the nextlove generate-resource-object-class.ts serialization: the |
3 | | -// from_json body lines and the constructor parameter lines, each already |
4 | | -// sorted with the nextlove property comparator. |
5 | | - |
6 | | -import { getPhpType } from '../map-php-type.js' |
7 | | -import type { ExtractedResourceObjectSchema ,PropertySchemaWithReferenceObject } from '../openapi/deep-extract-resource-object-schemas.js' |
| 1 | +// Builds the template context for resource object files (src/Objects/{Name}.php): |
| 2 | +// the from_json body lines and the constructor parameter lines. |
| 3 | +// |
| 4 | +// The blueprint does not track which resource properties are required, so |
| 5 | +// every property is optional: from_json falls back to null for missing values |
| 6 | +// and the constructor parameters are nullable. |
| 7 | + |
| 8 | +import type { |
| 9 | + ResourceObjectProperty, |
| 10 | + ResourceObjectSchema, |
| 11 | +} from '../resource-model.js' |
8 | 12 |
|
9 | 13 | export interface ObjectLayoutContext { |
10 | 14 | className: string |
11 | 15 | fromJsonProps: string[] |
12 | 16 | constructorParams: string[] |
13 | 17 | } |
14 | 18 |
|
15 | | -const compareProperties = ( |
16 | | - [propA, schemaA]: [string, PropertySchemaWithReferenceObject], |
17 | | - [propB, schemaB]: [string, PropertySchemaWithReferenceObject], |
18 | | - requiredPropertyNames: string[], |
19 | | -): number => { |
20 | | - const aRequired = requiredPropertyNames.includes(propA) |
21 | | - const bRequired = requiredPropertyNames.includes(propB) |
22 | | - const aNullable = 'nullable' in schemaA && schemaA.nullable === true |
23 | | - const bNullable = 'nullable' in schemaB && schemaB.nullable === true |
24 | | - |
25 | | - if (!aNullable !== !bNullable) return !aNullable ? -1 : 1 |
26 | | - if (aRequired !== bRequired) return aRequired ? -1 : 1 |
27 | | - return propA.localeCompare(propB) |
28 | | -} |
29 | | - |
30 | | -const isObjectReferencingResource = ( |
31 | | - schema: PropertySchemaWithReferenceObject, |
32 | | -): boolean => |
33 | | - 'type' in schema && |
34 | | - schema.type === 'object' && |
35 | | - 'referenceObjectTypeName' in schema |
| 19 | +const generateFromJsonProp = (property: ResourceObjectProperty): string => { |
| 20 | + const { name } = property |
36 | 21 |
|
37 | | -const isArrayReferencingResource = ( |
38 | | - schema: PropertySchemaWithReferenceObject, |
39 | | -): boolean => |
40 | | - 'type' in schema && |
41 | | - schema.type === 'array' && |
42 | | - 'referenceObjectTypeName' in schema |
43 | | - |
44 | | -const isPropertyRequired = ( |
45 | | - propName: string, |
46 | | - schema: PropertySchemaWithReferenceObject, |
47 | | - requiredPropertyNames: string[], |
48 | | -): boolean => |
49 | | - 'nullable' in schema |
50 | | - ? !(schema.nullable ?? false) |
51 | | - : requiredPropertyNames.includes(propName) |
52 | | - |
53 | | -const generateFromJsonProp = ( |
54 | | - propName: string, |
55 | | - schema: PropertySchemaWithReferenceObject, |
56 | | - requiredPropertyNames: string[], |
57 | | -): string => { |
58 | | - const required = isPropertyRequired(propName, schema, requiredPropertyNames) |
59 | | - const refName = |
60 | | - 'referenceObjectTypeName' in schema ? schema.referenceObjectTypeName : '' |
| 22 | + switch (property.kind) { |
| 23 | + case 'objectReference': |
| 24 | + return `${name}: isset($json->${name}) ? ${property.referenceName}::from_json($json->${name}) : null,` |
61 | 25 |
|
62 | | - if (isObjectReferencingResource(schema)) { |
63 | | - return `${propName}: ${required ? '' : `isset($json->${propName}) ? `}${refName}::from_json($json->${propName})${required ? ',' : ' : null,'}` |
64 | | - } |
| 26 | + case 'listReference': |
| 27 | + return `${name}: array_map(fn ($${name[0]}) => ${property.referenceName}::from_json($${name[0]}), $json->${name} ?? []),` |
65 | 28 |
|
66 | | - if (isArrayReferencingResource(schema)) { |
67 | | - return `${propName}: array_map(fn ($${propName[0]}) => ${refName}::from_json($${propName[0]}), $json->${propName} ?? []),` |
| 29 | + case 'value': |
| 30 | + return `${name}: $json->${name} ?? null,` |
68 | 31 | } |
69 | | - |
70 | | - return `${propName}: $json->${propName}${required ? ',' : ' ?? null,'}` |
71 | 32 | } |
72 | 33 |
|
73 | 34 | const generateConstructorParam = ( |
74 | | - propName: string, |
75 | | - schema: PropertySchemaWithReferenceObject, |
76 | | - requiredPropertyNames: string[], |
| 35 | + property: ResourceObjectProperty, |
77 | 36 | ): string => { |
78 | | - const phpType = isObjectReferencingResource(schema) |
79 | | - ? ((schema as { referenceObjectTypeName?: string }) |
80 | | - .referenceObjectTypeName ?? 'mixed') |
81 | | - : getPhpType('type' in schema ? schema.type : 'mixed') |
82 | | - const required = isPropertyRequired(propName, schema, requiredPropertyNames) |
83 | | - const nullSuffix = phpType === 'mixed' ? '' : required ? '' : ' | null' |
84 | | - |
85 | | - return `public ${phpType}${nullSuffix} $${propName},` |
| 37 | + switch (property.kind) { |
| 38 | + case 'objectReference': |
| 39 | + return `public ${property.referenceName}|null $${property.name},` |
| 40 | + |
| 41 | + case 'listReference': |
| 42 | + return `public array $${property.name},` |
| 43 | + |
| 44 | + case 'value': { |
| 45 | + const { phpType } = property |
| 46 | + const nullSuffix = phpType === 'mixed' ? '' : '|null' |
| 47 | + return `public ${phpType}${nullSuffix} $${property.name},` |
| 48 | + } |
| 49 | + } |
86 | 50 | } |
87 | 51 |
|
88 | 52 | export const setObjectLayoutContext = ( |
89 | | - schema: ExtractedResourceObjectSchema, |
| 53 | + schema: ResourceObjectSchema, |
90 | 54 | ): ObjectLayoutContext => { |
91 | | - const { name, requiredPropertyNames } = schema |
92 | | - const properties = { ...schema.properties } |
93 | | - |
94 | | - // TODO: Remove this created_at injection once seam-connect is patched and |
95 | | - // generated output is allowed to change. The nextlove generator added a |
96 | | - // created_at string property to every Errors and Warnings resource class. |
97 | | - if (name.endsWith('Errors') || name.endsWith('Warnings')) { |
98 | | - properties['created_at'] = { type: 'string' } |
99 | | - } |
100 | | - |
101 | | - const sorted = Object.entries(properties).sort((a, b) => |
102 | | - compareProperties(a, b, requiredPropertyNames), |
| 55 | + const sorted = [...schema.properties].sort((a, b) => |
| 56 | + a.name.localeCompare(b.name), |
103 | 57 | ) |
104 | 58 |
|
105 | 59 | return { |
106 | | - className: name, |
107 | | - fromJsonProps: sorted.map(([propName, propSchema]) => |
108 | | - generateFromJsonProp(propName, propSchema, requiredPropertyNames), |
109 | | - ), |
110 | | - constructorParams: sorted.map(([propName, propSchema]) => |
111 | | - generateConstructorParam(propName, propSchema, requiredPropertyNames), |
112 | | - ), |
| 60 | + className: schema.name, |
| 61 | + fromJsonProps: sorted.map(generateFromJsonProp), |
| 62 | + constructorParams: sorted.map(generateConstructorParam), |
113 | 63 | } |
114 | 64 | } |
0 commit comments