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/SBogers10/bomacon.komma.pro/app/Posts/Kms/PostTranslationService.php
<?php
namespace App\Posts\Kms;


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


final class PostTranslationService extends TranslationService
{
    protected $modelClassName = PostTranslation::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;
    }
}