Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 4, 2026. It is now read-only.

Commit 16b0fba

Browse files
refactor(deserializer): small code cleanup
1 parent acc5ed8 commit 16b0fba

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/DeserializerGenerator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ private function generateInnerCodeForFieldType(
212212
return $this->generateCodeForArray($type, $arrayPath, $modelPropertyPath, $stack);
213213

214214
case $type instanceof PropertyTypeDateTime:
215-
// todo: remove use of deprecated method {@link \Liip\MetadataParser\Metadata\PropertyTypeDateTime::getDeserializeFormat}
216215
$formats = $type->getDeserializeFormats() ?: (\is_string($type->getFormat()) ? [$type->getFormat()] : $type->getFormat());
217216
if (null !== $formats) {
218217
return $this->templating->renderAssignDateTimeFromFormat($type->isImmutable(), (string) $modelPropertyPath, (string) $arrayPath, $formats, $type->getZone());

src/Template/Deserialization.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,18 @@ function {{functionName}}(array {{jsonPath}}): {{className}}
6262
EOT;
6363

6464
private const TMPL_ASSIGN_DATETIME_FROM_FORMAT = <<<'EOT'
65+
{{date}} = false;
6566
foreach([{{formats|join(', ')}}] as {{format}}) {
6667
if (({{date}} = \DateTime::createFromFormat({{format}}, {{jsonPath}}, {{timezone}}))) {
6768
{{modelPath}} = {{date}};
6869
break;
6970
}
7071
}
7172
72-
if (false === ({{date}} ?? null)) {
73-
throw new \Exception('Invalid datetime string '.({{jsonPath}}).' matches none of the deserialization formats '.({{formats|join('|')}}));
73+
if (false === {{date}}) {
74+
throw new \Exception('Invalid datetime string '.({{jsonPath}}).' matches none of the deserialization formats: '.({{formats|join('|')}}));
7475
}
76+
unset({{format}}, {{date}});
7577

7678
EOT;
7779

@@ -81,16 +83,18 @@ function {{functionName}}(array {{jsonPath}}): {{className}}
8183
EOT;
8284

8385
private const TMPL_ASSIGN_DATETIME_IMMUTABLE_FROM_FORMAT = <<<'EOT'
86+
{{date}} = false;
8487
foreach([{{formats|join(', ')}}] as {{format}}) {
8588
if (({{date}} = \DateTimeImmutable::createFromFormat({{format}}, {{jsonPath}}, {{timezone}}))) {
8689
{{modelPath}} = {{date}};
8790
break;
8891
}
8992
}
9093
91-
if (false === ({{date}} ?? null)) {
92-
throw new \Exception('Invalid datetime string '.({{jsonPath}}).' matches none of the deserialization formats '.({{formats|join('|')}}));
94+
if (false === {{date}}) {
95+
throw new \Exception('Invalid datetime string '.({{jsonPath}}).' matches none of the deserialization formats: '.({{formats|join('|')}}));
9396
}
97+
unset({{format}}, {{date}});
9498

9599
EOT;
96100

@@ -213,12 +217,17 @@ public function renderAssignDateTimeToField(bool $immutable, string $modelPath,
213217
*/
214218
public function renderAssignDateTimeFromFormat(bool $immutable, string $modelPath, string $jsonPath, array|string $formats, string $timezone = null): string
215219
{
220+
if (is_string($formats)) {
221+
@trigger_error('Passing a string for argument $formats is deprecated, please pass an array of strings instead', \E_USER_DEPRECATED);
222+
$formats = [$formats];
223+
}
224+
216225
$template = $immutable ? self::TMPL_ASSIGN_DATETIME_IMMUTABLE_FROM_FORMAT : self::TMPL_ASSIGN_DATETIME_FROM_FORMAT;
217226
$formats = array_map(
218227
static fn (string $f): string => var_export($f, true),
219-
\is_string($formats) ? [$formats] : $formats
228+
$formats
220229
);
221-
$formatVariable = preg_replace_callback(
230+
$dateVariable = preg_replace_callback(
222231
'/([^a-zA-Z]+|\d+)([a-zA-Z])/',
223232
static fn ($match): string => (ctype_digit($match[1]) ? $match[1] : null).mb_strtoupper($match[2]),
224233
$modelPath
@@ -228,8 +237,8 @@ public function renderAssignDateTimeFromFormat(bool $immutable, string $modelPat
228237
'modelPath' => $modelPath,
229238
'jsonPath' => $jsonPath,
230239
'formats' => $formats,
231-
'format' => '$'.lcfirst($formatVariable),
232-
'date' => '$'.lcfirst($formatVariable).'Date',
240+
'format' => '$'.lcfirst($dateVariable).'Format',
241+
'date' => '$'.lcfirst($dateVariable),
233242
'timezone' => $timezone ? 'new \DateTimeZone('.var_export($timezone, true).')' : 'null',
234243
]);
235244
}

0 commit comments

Comments
 (0)