name = $name; $this->slug = null; return $this; } public function getName(): string { if ($this->name === null) { throw new WidgetException('Widget of class '.self::class.' does not have a name!'); } return $this->name; } public function setTarget(string $target): self { $this->target = $target; return $this; } public function getTarget(): string { if ($this->target === null) { throw new WidgetException("Widget {$this->getName()} does not have Target set"); } return $this->target; } public function setPriority(int $priority): self { $this->priority = $priority; return $this; } public function getPriority(): int { if ($this->priority === null) { throw new WidgetException("Widget {$this->getName()} does not have priority set"); } return $this->priority; } /** * Method to 'invoke' the widget. Simple wrapper around the 'run' method, * which can be overridden in a custom Widget or trait */ public function __invoke(array $params = []): ?string { return $this->run($params); } /** * Actual method that 'runs' the widget and returns the output. For reasons * of extensibility: Do not call directly, but call `$widget()` to invoke. */ protected function run(array $params = []): ?string { if (array_key_exists('template', $params)) { $this->setTemplate($params['template']); } if ($this instanceof TwigAware) { $output = $this->getTWig()->render($this->getTemplate(), $params); } else { $output = $this->getTemplate(); } return sprintf( '
%s
', $this->getSlug(), $this->getName(), $output ); } public function setTemplate(string $template): self { $this->template = $template; return $this; } public function getTemplate(): string { if ($this->template === null) { throw new WidgetException("Widget {$this->getName()} does not have template set"); } return $this->template; } public function setZone(string $zone): self { $this->zone = $zone; return $this; } public function getZone(): string { if ($this->zone === null) { throw new WidgetException("Widget {$this->getName()} does not have Zone set"); } return $this->zone; } public function getSlug(): string { if ($this->slug === null) { $this->slug = Slugify::create()->slugify($this->getName()); } return $this->slug; } public function getCacheDuration(): int { return $this->cacheDuration; } }