1
1
<?php
2
2
3
- declare (strict_types=1 );
4
-
5
3
/*
6
4
* This file is part of the Symfony package.
7
5
*
@@ -34,36 +32,64 @@ public static function getExtendedTypes(): iterable
34
32
public function buildView (FormView $ view , FormInterface $ form , array $ options ): void
35
33
{
36
34
if (
37
- null === $ options ['stimulus_controller ' ]
38
- && null === $ options ['stimulus_target ' ]
39
- && null === $ options ['stimulus_action ' ]
35
+ isset ( $ options ['stimulus_controller ' ])
36
+ || ! isset ( $ options ['stimulus_target ' ])
37
+ || ! isset ( $ options ['stimulus_action ' ])
40
38
) {
41
- return ;
42
- }
39
+ $ this ->stimulusAttributes = new StimulusAttributes (new Environment (new ArrayLoader ()));
43
40
44
- $ this ->stimulusAttributes = new StimulusAttributes (new Environment (new ArrayLoader ()));
41
+ if (isset ($ options ['stimulus_controller ' ])) {
42
+ $ this ->handleController ($ options ['stimulus_controller ' ]);
43
+ }
45
44
46
- if ( true === \array_key_exists ( ' stimulus_controller ' , $ options )) {
47
- $ this ->handleController ($ options ['stimulus_controller ' ]);
48
- }
45
+ if ( isset ( $ options[ ' stimulus_target ' ] )) {
46
+ $ this ->handleTarget ($ options ['stimulus_target ' ]);
47
+ }
49
48
50
- if ( true === \array_key_exists ( ' stimulus_target ' , $ options )) {
51
- $ this ->handleTarget ($ options ['stimulus_target ' ]);
52
- }
49
+ if ( isset ( $ options[ ' stimulus_action ' ] )) {
50
+ $ this ->handleAction ($ options ['stimulus_action ' ]);
51
+ }
53
52
54
- if ( true === \array_key_exists ( ' stimulus_action ' , $ options )) {
55
- $ this -> handleAction ( $ options [ ' stimulus_action ' ]) ;
53
+ $ attributes = \array_merge ( $ view -> vars [ ' attr ' ] , $ this -> stimulusAttributes -> toArray ());
54
+ $ view -> vars [ ' attr ' ] = $ attributes ;
56
55
}
57
56
58
- $ attributes = array_merge ($ view ->vars ['attr ' ], $ this ->stimulusAttributes ->toArray ());
57
+ foreach (['row_attr ' , 'choice_attr ' ] as $ index ) {
58
+ if (
59
+ isset ($ options [$ index ])
60
+ && (
61
+ isset ($ options [$ index ]['stimulus_controller ' ])
62
+ || isset ($ options [$ index ]['stimulus_target ' ])
63
+ || isset ($ options [$ index ]['stimulus_action ' ])
64
+ )
65
+ ) {
66
+ $ this ->stimulusAttributes = new StimulusAttributes (new Environment (new ArrayLoader ()));
67
+
68
+ if (isset ($ options [$ index ]['stimulus_controller ' ])) {
69
+ $ this ->handleController ($ options [$ index ]['stimulus_controller ' ]);
70
+ unset($ options [$ index ]['stimulus_controller ' ]);
71
+ }
72
+
73
+ if (isset ($ options [$ index ]['stimulus_target ' ])) {
74
+ $ this ->handleTarget ($ options [$ index ]['stimulus_target ' ]);
75
+ unset($ options [$ index ]['stimulus_target ' ]);
76
+ }
59
77
60
- $ view ->vars ['attr ' ] = $ attributes ;
78
+ if (isset ($ options [$ index ]['stimulus_action ' ])) {
79
+ $ this ->handleAction ($ options [$ index ]['stimulus_action ' ]);
80
+ unset($ options [$ index ]['stimulus_action ' ]);
81
+ }
82
+
83
+ $ attributes = \array_merge ($ options [$ index ], $ this ->stimulusAttributes ->toArray ());
84
+ $ view ->vars [$ index ] = $ attributes ;
85
+ }
86
+ }
61
87
}
62
88
63
89
private function handleController (string |array $ controllers ): void
64
90
{
65
91
if (\is_string ($ controllers )) {
66
- $ controllers = [$ controllcers ];
92
+ $ controllers = [$ controllers ];
67
93
}
68
94
69
95
foreach ($ controllers as $ controllerName => $ controller ) {
@@ -78,24 +104,24 @@ private function handleController(string|array $controllers): void
78
104
private function handleTarget (array $ targets ): void
79
105
{
80
106
foreach ($ targets as $ controllerName => $ target ) {
81
- $ this ->stimulusAttributes ->addTarget ($ controllerName , \is_array ($ target ) ? implode (' ' , $ target ) : $ target );
107
+ $ this ->stimulusAttributes ->addTarget ($ controllerName , \is_array ($ target ) ? \ implode (' ' , $ target ) : $ target );
82
108
}
83
109
}
84
110
85
111
private function handleAction (string |array $ actions ): void
86
112
{
87
113
// 'stimulus_action' => 'controllerName#actionName'
88
114
// 'stimulus_action' => 'eventName->controllerName#actionName'
89
- if (\is_string ($ actions ) && str_contains ($ actions , '# ' )) {
115
+ if (\is_string ($ actions ) && \ str_contains ($ actions , '# ' )) {
90
116
$ eventName = null ;
91
117
92
- if (str_contains ($ actions , '-> ' )) {
93
- [$ eventName , $ rest ] = explode ('-> ' , $ actions , 2 );
118
+ if (\ str_contains ($ actions , '-> ' )) {
119
+ [$ eventName , $ rest ] = \ explode ('-> ' , $ actions , 2 );
94
120
} else {
95
121
$ rest = $ actions ;
96
122
}
97
123
98
- [$ controllerName , $ actionName ] = explode ('# ' , $ rest , 2 );
124
+ [$ controllerName , $ actionName ] = \ explode ('# ' , $ rest , 2 );
99
125
100
126
$ this ->stimulusAttributes ->addAction ($ controllerName , $ actionName , $ eventName );
101
127
@@ -129,8 +155,8 @@ public function configureOptions(OptionsResolver $resolver): void
129
155
'stimulus_target ' => null ,
130
156
]);
131
157
132
- $ resolver ->setAllowedTypes ('stimulus_action ' , ['string ' , 'array ' , 'null ' ]);
133
- $ resolver ->setAllowedTypes ('stimulus_controller ' , ['string ' , 'array ' , 'null ' ]);
134
- $ resolver ->setAllowedTypes ('stimulus_target ' , ['string ' , 'array ' , 'null ' ]);
158
+ $ resolver ->setAllowedTypes ('stimulus_action ' , ['string ' , 'array ' , 'callable ' , ' null ' ]);
159
+ $ resolver ->setAllowedTypes ('stimulus_controller ' , ['string ' , 'array ' , 'callable ' , ' null ' ]);
160
+ $ resolver ->setAllowedTypes ('stimulus_target ' , ['string ' , 'array ' , 'callable ' , ' null ' ]);
135
161
}
136
162
}
0 commit comments