Skip to content

Commit

Permalink
Apply code review feedback
Browse files Browse the repository at this point in the history
* extract serializing properties to own variable to make
  code more readable.
* use ConstrainedDictionaryModel to detect additional properties
  • Loading branch information
markuspoerschke committed Aug 25, 2023
1 parent 048c9e1 commit f2616f9
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/generators/php/presets/JsonSerializablePreset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { PhpPreset } from '../PhpPreset';
import { PhpRenderer } from '../PhpRenderer';
import { ConstrainedMetaModel } from '../../../models';
import {
ConstrainedDictionaryModel,
ConstrainedMetaModel
} from '../../../models';

function renderSelf({
content
Expand All @@ -27,6 +30,19 @@ export const PHP_JSON_SERIALIZABLE_PRESET: PhpPreset = {
return renderSelf({ content, renderer });
},
additionalContent({ renderer, model, content }): string {
const serializedProperties = Object.values(model.properties).map(
(property) => {
if (
property.property instanceof ConstrainedDictionaryModel &&
property.property.serializationType === 'unwrap'
) {
return `...$this->${property.propertyName},`;
}

return `'${property.unconstrainedPropertyName}' => $this->${property.propertyName},`;
}
);

return (
content +
renderer.renderBlock([
Expand All @@ -35,17 +51,7 @@ export const PHP_JSON_SERIALIZABLE_PRESET: PhpPreset = {
renderer.indent(
renderer.renderBlock([
'return [',
renderer.indent(
renderer.renderBlock(
Object.values(model.properties).map((property) => {
if (property.propertyName === 'additionalProperties') {
return `...$this->${property.propertyName},`;
}

return `'${property.unconstrainedPropertyName}' => $this->${property.propertyName},`;
})
)
),
renderer.indent(renderer.renderBlock(serializedProperties)),
'];'
])
),
Expand All @@ -68,11 +74,11 @@ export const PHP_JSON_SERIALIZABLE_PRESET: PhpPreset = {
renderer.renderBlock([
'return match($this) {',
renderer.indent(
renderer.renderBlock([
...model.values.map(
renderer.renderBlock(
Object.values(model.values).map(
(value) => `self::${value.key} => ${value.value},`
)
])
)
),
'};'
])
Expand Down

0 comments on commit f2616f9

Please sign in to comment.