*/ class ControllerViewHelper extends AbstractStimulusViewHelper { use CompileWithRenderStatic; protected $escapeOutput = false; public function initializeArguments() { parent::initializeArguments(); $this->registerArgument('controller', 'mixed', 'Controller name(s)', true); $this->registerArgument('values', 'array', 'Controller values', false, []); } /** * @param array $arguments * @param \Closure $renderChildrenClosure * @param RenderingContextInterface $renderingContext * @return string */ public static function renderStatic( array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext ) { $dataOrControllerName = $arguments['controller']; $controllerValues = $arguments['values']; if (\is_string($dataOrControllerName)) { $data = [$dataOrControllerName => $controllerValues]; } else { if ($controllerValues) { throw new \InvalidArgumentException('You cannot pass an array to the first and second argument of stimulus_controller(): check the documentation.'); } $data = $dataOrControllerName; if (!$data) { return ''; } } $controllers = []; $values = []; foreach ($data as $controllerName => $controllerValues) { $controllerName = static::normalizeControllerName($controllerName); $controllers[] = $controllerName; foreach ($controllerValues as $key => $value) { if (!is_scalar($value)) { $value = json_encode($value); } if (\is_bool($value)) { $value = $value ? 'true' : 'false'; } $key = static::normalizeKeyName($key); $value = htmlspecialchars($value, ENT_QUOTES); $values[] = 'data-'.$controllerName.'-'.$key.'-value="'.$value.'"'; } } return rtrim('data-controller="'.implode(' ', $controllers).'" '.implode(' ', $values)); } }