/** * @return \KodiCMS\Widgets\Contracts\Widget|null * @throws WidgetException */ public function toWidget() { if (!is_null($this->widget)) { return $this->widget; } if (array_key_exists($this->id, static::$cachedWidgets)) { $this->widget = static::$cachedWidgets[$this->id]; return $this->widget; } if (!is_null($this->widget = $this->manager->makeWidget($this->type, $this->name, $this->description, $this->settings))) { $this->widget->setId($this->id); if ($this->isRenderable()) { $this->widget->setFrontendTemplate($this->template); } $this->widget->setRalatedWidgets($this->related); } else { $this->widget = new TempWidget($this->manager, $this->name, $this->description); } static::$cachedWidgets[$this->id] = $this->widget; return $this->widget; }
/** * @param Widget $widget * * @return bool|Widget * @throws ValidationException */ public function create(Widget $widget) { if ($widget->isExists()) { return false; } $data = ['name' => $widget->getName(), 'description' => $widget->getDescription(), 'settings' => $widget->getSettings(), 'type' => $widget->getType()]; $creator = new WidgetCreator(); $validator = $creator->validator($data); if ($validator->fails()) { throw (new ValidationException())->setValidator($validator); } $widgetModel = $creator->create($data); $widget->setId($widgetModel->id); return $widget; }