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/farmfun/reserveren.farmfun.be/app/Komma/Pages/Kms/PageTranslationService.php
<?php

declare(strict_types=1);

namespace App\Komma\Pages\Kms;

use App\Komma\Globalization\Languages\Models\Language;
use App\Komma\Kms\Core\AbstractTranslatableModel;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\TranslationService;
use App\Komma\Pages\Models\PageTranslation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class PageTranslationService extends TranslationService
{
    protected $modelClassName = PageTranslation::class;

    /**
     * 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 save(Model $model, Collection $attributes = null): Model
    {
        if ($attributes === null) {
            return $model;
        }

        if (! is_a($model, AbstractTranslatableModel::class)) {
            return $model;
        } //Only handle abstractTranslatable models. Since only those can have translations.
        /** @var $model AbstractTranslatableModel */
        $attributes->each(function (Attribute $attribute) use (&$model) {
            $valueFrom = $attribute->getsValueFrom();
            $valueReference = $attribute->getsValueFromReference();
            if ($valueFrom !== Attribute::ValueFromTranslationModel) {
                return $model;
            } //Continue to next iteration. We are not responsible for this attribute

            $value = $attribute->getValue();
            /** @var Language $language */
            $language = $attribute->getAssociatedLanguage();
            /** @var PageTranslation $translation */
            $translation = $this->getTranslationModelForModelByLanguage($model, $language);
            $translation->$valueReference = $value;
            if ($valueReference == 'name') {
                $translation->slug = Str::slug($value);
            }

            $this->saveModelTranslations($model);
        });

        return $model;
    }
}