1515use Symfony \Component \Form \AbstractTypeExtension ;
1616use Symfony \Component \Form \FormInterface ;
1717use Symfony \Component \Form \FormView ;
18+ use Symfony \Component \PropertyAccess \PropertyAccess ;
19+ use Symfony \Component \PropertyAccess \PropertyAccessor ;
1820
1921/**
2022 * Tree Aware Extension
@@ -56,6 +58,11 @@ abstract class TreeAwareExtension extends AbstractTypeExtension
5658 */
5759 protected $ defaultTranslationDomain ;
5860
61+ /**
62+ * @var PropertyAccessor
63+ */
64+ protected $ propertyAccessor ;
65+
5966 /**
6067 * Enable or disable automatic generation of missing labels
6168 *
@@ -113,7 +120,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
113120 {
114121 if ($ this ->treeBuilder && $ this ->keyBuilder ) {
115122 foreach ($ this ->keys as $ key => $ value ) {
116- if (isset ($ options[ $ key ]) && $ options [ $ key] === true ) {
123+ if ($ this -> optionEquals ($ options, $ key, true ) ) {
117124 $ this ->generateKey ($ view , $ key , $ value );
118125 }
119126 }
@@ -133,6 +140,33 @@ protected function generateKey(FormView &$view, $key, $value)
133140 $ view ->vars ['tree ' ] = $ this ->treeBuilder ->getTree ($ view );
134141 }
135142
136- $ view ->vars [$ key ] = $ this ->keyBuilder ->buildKeyFromTree ($ view ->vars ['tree ' ], $ value );
143+ $ this ->setVar ($ view ->vars , $ key , $ this ->keyBuilder ->buildKeyFromTree ($ view ->vars ['tree ' ], $ value ));
144+ }
145+
146+ protected function setVar (array &$ vars , string $ key , $ value ): void
147+ {
148+ if ($ this ->getPropertyAccessor ()->isWritable ($ vars , $ key )) {
149+ $ this ->getPropertyAccessor ()->setValue ($ vars , $ key , $ value );
150+ } else {
151+ $ vars [$ key ] = $ value ;
152+ }
153+ }
154+
155+ protected function optionEquals (array $ options , string $ key , $ value ): bool
156+ {
157+ if ($ this ->getPropertyAccessor ()->isReadable ($ options , $ key )) {
158+ return $ this ->getPropertyAccessor ()->getValue ($ options , $ key ) === $ value ;
159+ }
160+
161+ return isset ($ options [$ key ]) ? $ options [$ key ] === $ value : false ;
162+ }
163+
164+ protected function getPropertyAccessor (): PropertyAccessor
165+ {
166+ if (!$ this ->propertyAccessor ) {
167+ $ this ->propertyAccessor = PropertyAccess::createPropertyAccessor ();
168+ }
169+
170+ return $ this ->propertyAccessor ;
137171 }
138172}
0 commit comments