@@ -62,16 +62,18 @@ function {{functionName}}(array {{jsonPath}}): {{className}}
6262EOT;
6363
6464 private const TMPL_ASSIGN_DATETIME_FROM_FORMAT = <<<'EOT'
65+ {{date}} = false;
6566foreach([{{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
7678EOT;
7779
@@ -81,16 +83,18 @@ function {{functionName}}(array {{jsonPath}}): {{className}}
8183EOT;
8284
8385 private const TMPL_ASSIGN_DATETIME_IMMUTABLE_FROM_FORMAT = <<<'EOT'
86+ {{date}} = false;
8487foreach([{{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
9599EOT;
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