22
33namespace Html \TemplateGenerator ;
44
5+ use Html \Helper \Helper ;
56use Html \Interface \HTMLElementDelegatorInterface ;
67use Html \Interface \TemplateGeneratorInterface ;
78use Html \Mapping \TemplateGenerator ;
@@ -101,7 +102,7 @@ public function renderComponentClass(HTMLElementDelegatorInterface $element): st
101102 ? $ ref ->getConstant ('QUALIFIED_NAME ' )
102103 : strtolower ($ ref ->getShortName ());
103104
104- $ componentName = ucfirst ($ elementName );
105+ $ componentName = $ this -> getComponentClassName ( ucfirst ($ elementName) );
105106
106107 // Determine the level (Block, Inline, or Void)
107108 $ level = $ this ->determineComponentLevel ($ ref ->getName ());
@@ -282,6 +283,9 @@ public function renderComponentClass(HTMLElementDelegatorInterface $element): st
282283 return "' {$ type }' " ;
283284 }, $ propData ['allowedTypes ' ]);
284285
286+ if (in_array ('null ' , $ propData ['allowedTypes ' ])) {
287+ $ php .= " \$resolver->setDefaults([' {$ propName }' => null]); \n" ;
288+ }
285289 $ php .= " \$resolver->setAllowedTypes(' {$ propName }', [ " . implode (
286290 ', ' ,
287291 $ allowedTypesFormatted
@@ -416,6 +420,7 @@ private function renderTemplate(HTMLElementDelegatorInterface $element): string
416420 }
417421 }
418422 $ name = ucfirst ($ elementName );
423+ $ componentName = $ this ->getComponentClassName (ucfirst ($ elementName ));
419424
420425 // Build Twig Component template
421426 $ twig = "{# \n" ;
@@ -424,7 +429,7 @@ private function renderTemplate(HTMLElementDelegatorInterface $element): string
424429 $ twig .= " Symfony UX Twig Component (Anonymous) \n" ;
425430 $ twig .= " @see https://symfony.com/bundles/ux-twig-component/current/index.html \n\n" ;
426431 $ twig .= " Usage: \n" ;
427- $ twig .= ' <twig: ' . ucfirst ( $ elementName ) ;
432+ $ twig .= ' <twig: ' . $ componentName ;
428433
429434 // Add example usage
430435 $ exampleProps = [];
@@ -447,7 +452,7 @@ private function renderTemplate(HTMLElementDelegatorInterface $element): string
447452 } else {
448453 $ twig .= "> \n" ;
449454 $ twig .= " Content goes here \n" ;
450- $ twig .= ' </twig: ' . ucfirst ( $ elementName ) . "> \n" ;
455+ $ twig .= ' </twig: ' . $ componentName . "> \n" ;
451456 }
452457
453458 $ twig .= "\n @author vardumper <info@erikpoehler.com> \n" ;
@@ -495,7 +500,7 @@ private function buildComposedTemplate(
495500 string $ description ,
496501 array $ parentOf
497502 ): string {
498- $ componentName = ucfirst ($ elementName );
503+ $ componentName = $ this -> getComponentClassName ( ucfirst ($ elementName) );
499504
500505 $ twig = "{# \n" ;
501506 $ twig .= " This file is auto-generated. \n\n" ;
@@ -550,7 +555,7 @@ private function collectChildrenForComposedTemplate(array $parentOf): array
550555 ? $ childRef ->getConstant ('QUALIFIED_NAME ' )
551556 : strtolower ($ childRef ->getShortName ());
552557
553- $ childComponentName = ucfirst ($ childElementName );
558+ $ childComponentName = $ this -> getComponentClassName ( ucfirst ($ childElementName) );
554559 $ isSelfClosing = $ childRef ->hasConstant ('SELF_CLOSING ' ) && $ childRef ->getConstant ('SELF_CLOSING ' );
555560
556561 $ twigCode = '' ;
@@ -606,4 +611,16 @@ private function determineComponentLevel(string $className): string
606611 }
607612 return 'block ' ;
608613 }
614+
615+ /**
616+ * Get safe component class name, avoiding PHP reserved words
617+ */
618+ private function getComponentClassName (string $ className ): string
619+ {
620+ $ reserved = Helper::getReservedWords ();
621+ if (in_array (strtolower ($ className ), $ reserved , true )) {
622+ return $ className . 'Element ' ;
623+ }
624+ return $ className ;
625+ }
609626}
0 commit comments