HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers54/csbinstallatietechniek.nl/app/Projects/Kms/ProjectModelService.php
<?php declare(strict_types=1);


namespace App\Projects\Kms;

use App\Projects\Models\Project;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\ModelService;
use Komma\KMS\Core\Tree\NestedSets\Nodes\AbstractTranslatableTreeModel;
use App\Pages\Models\Page;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class ProjectModelService extends ModelService
{
    public function __construct()
    {
        $this->setModelClassName(Project::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, Project::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->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;
    }


}