/** * @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 * @throws ValidationException */ public function update(Widget $widget) { if (!$widget->isExists()) { return false; } $data = ['name' => $widget->getName(), 'description' => $widget->getDescription(), 'settings' => $widget->getSettings()]; $updater = new WidgetUpdator(); $validator = $updater->validator($data); if ($validator->fails()) { throw (new ValidationException())->setValidator($validator); } $updater->update($widget->getId(), $data); return true; }