@@ -45,6 +45,10 @@ class Component implements JsonSerializable
4545 * @var string
4646 */
4747 protected string $ prefixVarId = 'id_ ' ;
48+ /**
49+ * @var string|null
50+ */
51+ protected ?string $ suffixVarId = null ;
4852
4953 public function __construct (string |null $ id , bool $ asSelector = true , string $ endSymbols = '; ' . PHP_EOL )
5054 {
@@ -195,12 +199,35 @@ public function asUseVar(): static
195199 return $ this ;
196200 }
197201
202+ /**
203+ * @return $this
204+ */
205+ public function asNameVar (): static
206+ {
207+ $ this ->exportMode = 'name ' ;
208+ return $ this ;
209+ }
210+
211+ /**
212+ * @param string $prefixVarId
213+ * @return $this
214+ */
198215 public function setPrefixVarId (string $ prefixVarId ): static
199216 {
200217 $ this ->prefixVarId = $ prefixVarId ;
201218 return $ this ;
202219 }
203220
221+ /**
222+ * @param string $suffixVarId
223+ * @return $this
224+ */
225+ public function setSuffixVarId (string $ suffixVarId ): static
226+ {
227+ $ this ->suffixVarId = $ suffixVarId ;
228+ return $ this ;
229+ }
230+
204231 /**
205232 * @throws Exception
206233 */
@@ -211,7 +238,9 @@ public function __toString()
211238 $ data = $ this ->method ?? '( ' . Encode::json ($ this ) . ') ' ;
212239
213240 $ id = $ this ->id ;
214- $ prefixVarId = $ this ->prefixVarId ;
241+ $ cleanId = $ this ->cleanId ($ id );
242+ $ prefixVarId = $ this ->cleanId ($ this ->prefixVarId );
243+ $ suffixVarId = $ this ->cleanId ($ this ->suffixVarId ?: '_ ' . static ::COMPONENT_NAME );
215244 $ selector = '' ;
216245 $ end = '' ;
217246
@@ -237,15 +266,17 @@ public function __toString()
237266 $ outputData = static ::COMPONENT_NAME . $ data . $ components . $ end ;
238267 $ output = $ selector . $ outputData ;
239268
269+ if ($ this ->exportMode === 'name ' ) {
270+ return "{$ prefixVarId }{$ cleanId }{$ suffixVarId }" ;
271+ }
272+
240273 if ($ this ->exportMode === 'var ' ) {
241274 if ($ id === null || $ id === '' ) {
242275 $ warn = "console.warn('Cannot export as var: component id is not set'); " . PHP_EOL ;
243276 return $ warn . $ output ;
244277 }
245278
246- $ cleanId = $ this ->cleanId ($ id );
247-
248- return "var {$ prefixVarId }{$ cleanId } = {$ output }" ;
279+ return "var {$ prefixVarId }{$ cleanId }{$ suffixVarId } = {$ output }" ;
249280 }
250281
251282 if ($ this ->exportMode === 'use ' ) {
@@ -254,9 +285,7 @@ public function __toString()
254285 return $ warn . $ output ;
255286 }
256287
257- $ cleanId = $ this ->cleanId ($ id );
258-
259- return "{$ prefixVarId }{$ cleanId }. {$ outputData }" ;
288+ return "{$ prefixVarId }{$ cleanId }{$ suffixVarId }. {$ outputData }" ;
260289 }
261290
262291 return $ output ;
0 commit comments