File: D:/HostingSpaces/debout/debout.nl/app/Pages/Kms/PageModelService.php
<?php declare(strict_types=1);
namespace App\Pages\Kms;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\ModelService;
use App\Pages\Models\Page;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class PageModelService extends ModelService
{
public function __construct()
{
$this->setModelClassName(Page::class);
parent::__construct();
}
/**
* Gets the values of an Eloquent model and passes them to a collection of attributes
*
* @param Model $model
* @param Collection $attributes
* @return mixed
*/
public function load(Model $model, Collection $attributes = null): Collection
{
if($attributes == null) return new Collection();
$this->checkContainsAttributes($attributes);
if(!is_a($model, Page::class)) return $attributes;
/** @var Page $model */
$attributes = $attributes->map(function(Attribute $attribute) use(&$model) {
$valueReference = $attribute->getsValueFromReference();
if($valueReference == "site_name") {
$site = $this->siteService->getCurrentSite();
$attribute->setValue($site->name);
}
return $attribute;
});
return parent::load($model, $attributes);
}
/**
* This method will create a new model instance.
*
* @return Model
*/
public function newModel(): Model
{
//Create a new TranslatableModelInterface
/** @var Page $model */
$model = new $this->modelClassName;
$model =
$model->site()->associate($this->siteService->getCurrentSite());
//Set the thumbnail
/** @var HasThumbnailInterface|Model $model */
if(!is_a($model, HasThumbnailInterface::class)) throw new \RuntimeException('The class "'.get_class($model).'" is expected to implement the "'.HasThumbnailInterface::class.'" but did not');
$model = $model->generateThumbnail();
return $model;
}
}