Skip to content

[PHP-SYMFONY] Debug Symfony version 7, enums ref, array of enums $ref and date assert #19008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"doctrine/annotations": "^2.0",
"symfony/validator": "^6.4|^7.0",
"jms/serializer-bundle": "^5.4",
"symfony/framework-bundle": "^6.4|^7.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
return $this->{{name}};
}

/**
* Sets {{name}}.
*
* @param {{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
*
* @return $this
*/
public function {{setter}}({{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{name}}{{^required}} = null{{/required}}): self
{
$this->{{name}} = ${{name}};

return $this;
}

{{#isEnumRef}}
/**
* Gets {{name}} for serialization.
Expand All @@ -40,6 +54,24 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
{
return $this->{{name}}?->value ? (string) $this->{{name}}->value : null;
}

/**
* Sets {{name}}.
*
* @param string|{{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
*
* @return $this
*/
public function setDeserialized{{nameInPascalCase}}(string|{{#vendorExtensions.x-comment-type}}{{vendorExtensions.x-comment-type}} {{/vendorExtensions.x-comment-type}}${{name}}{{^required}} = null{{/required}}): self
{
if (is_string(${{name}})) {
${{name}} = {{baseType}}::tryFrom(${{name}});
}

$this->{{name}} = ${{name}};

return $this;
}
{{/isEnumRef}}

{{#isContainer}}
Expand All @@ -53,26 +85,36 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
public function getSerialized{{nameInPascalCase}}(): array
{
return array_map(
static fn ($value) => (string) $value->value,
$this->test ?? []
static fn ($value) => $value?->value ? (string) $value->value : null,
$this->{{name}} ?? []
);
}
{{/isEnumRef}}
{{/items}}
{{/isContainer}}

/**
* Sets {{name}}.
*
* @param {{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
*
* @return $this
*/
public function {{setter}}({{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{name}}{{^required}} = null{{/required}}): self
* Sets {{name}}.
*
* @param {{^required}}?{{/required}}array ${{name}}{{#description}} {{{.}}}{{/description}}
*
* @return $this
*/
public function setDeserialized{{nameInPascalCase}}({{^required}}?{{/required}}array ${{name}}{{^required}} = []{{/required}}): self
{
$this->{{name}} = ${{name}};
$this->{{name}} = array_map(
static function ($value) {
if (is_string($value)) {
$value = {{baseType}}::tryFrom($value);
}

return $value;
},
${{name}} ?? []
);

return $this;
}
{{/isEnumRef}}
{{/items}}
{{/isContainer}}

{{/vars}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,7 @@
{{/description}}
* @var {{{vendorExtensions.x-comment-type}}}
* @SerializedName("{{baseName}}")
{{#required}}
* @Assert\NotNull()
{{^isPrimitiveType}}
* @Assert\Valid()
{{/isPrimitiveType}}
{{/required}}
{{#isEnum}}
{{#isContainer}}
* @Assert\All({
{{#items}}
* @Assert\Choice({ {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}} })
{{/items}}
* })
{{/isContainer}}
{{^isContainer}}
* @Assert\Choice({ {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}} })
{{/isContainer}}
{{/isEnum}}
{{#isContainer}}
* @Assert\All({
{{#items}}
* @Assert\Type("{{dataType}}")
{{/items}}
* })
{{#isMap}}
{{#items}}
* @Type("array<string, {{dataType}}>")
Expand All @@ -37,7 +14,7 @@
{{^isMap}}
{{#items}}
{{#isEnumRef}}
* @Accessor(getter="getSerialized{{nameInPascalCase}}")
* @Accessor(getter="getSerialized{{nameInPascalCase}}", setter="setDeserialized{{nameInPascalCase}}")
* @Type("array<string>")
{{/isEnumRef}}
{{^isEnumRef}}
Expand All @@ -48,66 +25,95 @@
{{/isContainer}}
{{^isContainer}}
{{#isDate}}
* @Assert\Type("\Date")
* @Type("DateTime<'Y-m-d'>")
{{/isDate}}
{{#isDateTime}}
* @Assert\Type("\DateTime"))
* @Type("DateTime")
{{/isDateTime}}
{{#isEnumRef}}
* @Accessor(getter="getSerialized{{nameInPascalCase}}")
* @Type("string")
* @Accessor(getter="getSerialized{{nameInPascalCase}}", setter="setDeserialized{{nameInPascalCase}}")
* @Type("string")
{{/isEnumRef}}
{{^isDate}}
{{^isDateTime}}
{{^isEnumRef}}
* @Assert\Type("{{dataType}}")
* @Type("{{dataType}}")
{{/isEnumRef}}
{{/isDateTime}}
{{/isDate}}
{{/isContainer}}
*/
{{#required}}
#[Assert\NotNull]
{{^isPrimitiveType}}
#[Assert\Valid]
{{/isPrimitiveType}}
{{/required}}
{{#isEnum}}
{{#isContainer}}
#[Assert\All([
{{#items}}
new Assert\Choice([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]),
{{/items}}
])]
{{/isContainer}}
{{^isContainer}}
#[Assert\Choice([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])]
{{/isContainer}}
{{/isEnum}}
{{#isContainer}}
#[Assert\All([
{{#items}}
new Assert\Type("{{dataType}}"),
{{/items}}
])]
{{/isContainer}}
{{^isContainer}}
{{#isDate}}
#[Assert\Type("\DateTime")]
{{/isDate}}
{{#isDateTime}}
#[Assert\Type("\DateTime")]
{{/isDateTime}}
{{^isDate}}
{{^isDateTime}}
{{^isEnumRef}}
#[Assert\Type("{{dataType}}")]
{{/isEnumRef}}
{{/isDateTime}}
{{/isDate}}
{{/isContainer}}
{{#hasValidation}}
{{#maxLength}}
* @Assert\Length(
* max = {{.}}
* )
#[Assert\Length(max: {{.}})]
{{/maxLength}}
{{#minLength}}
* @Assert\Length(
* min = {{.}}
* )
#[Assert\Length(min: {{.}})]
{{/minLength}}
{{#minimum}}
{{#exclusiveMinimum}}
* @Assert\GreaterThan({{minimum}})
#[Assert\GreaterThan({{minimum}})]
{{/exclusiveMinimum}}
{{^exclusiveMinimum}}
* @Assert\GreaterThanOrEqual({{minimum}})
#[Assert\GreaterThanOrEqual({{minimum}})]
{{/exclusiveMinimum}}
{{/minimum}}
{{#maximum}}
{{#exclusiveMaximum}}
* @Assert\LessThan({{maximum}})
#[Assert\LessThan({{maximum}})]
{{/exclusiveMaximum}}
{{^exclusiveMaximum}}
* @Assert\LessThanOrEqual({{maximum}})
#[Assert\LessThanOrEqual({{maximum}})]
{{/exclusiveMaximum}}
{{/maximum}}
{{#pattern}}
* @Assert\Regex("/{{.}}/")
#[Assert\Regex("/{{.}}/")]
{{/pattern}}
{{#maxItems}}
* @Assert\Count(
* max = {{.}}
* )
#[Assert\Count(max: {{.}})]
{{/maxItems}}
{{#minItems}}
* @Assert\Count(
* min = {{.}}
* )
#[Assert\Count(min: {{.}})]
{{/minItems}}
{{/hasValidation}}
*/
protected {{{vendorExtensions.x-parameter-type}}} ${{name}} = {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}};
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ class JmsSerializer implements SerializerInterface
case '\DateTime':
return is_null($data) ? null :new DateTime($data);
default:
if (is_null($data)) {
return null;
}

if (!class_exists($type)) {
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
}

$reflectionClass = new \ReflectionClass($type);
if (!$reflectionClass->implementsInterface('\BackedENum')) {
if (!$reflectionClass->implementsInterface('\BackedEnum')) {
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
}

Expand Down
Loading
Loading