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/eleo.komma.nl/vendor/komma/kms/src/Core/AbstractTranslatableModel.php
<?php
namespace Komma\KMS\Core;

use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\App;
use Komma\KMS\Globalization\Languages\Models\Language;

/**
 * Represents a eloquent model that has a translation method to retrieve translations
 *
 * Interface TranslatableModelInterface
 * @package Komma\KMS\Core
 *
 * @see AbstractTranslationModel
 */
abstract class AbstractTranslatableModel extends Model implements HasThumbnailInterface
{
    use HasThumbnailTrait;

    /**
     * Gets the translation models for this model
     *
     * @return HasMany that resolves to AbstractTranslationModel instances
     */
    abstract public function translations(): HasMany;

    /**
     * Looks at the app language and searches the matching translation model for it.
     *
     * @throws \RuntimeException
     */
    public function translation(): HasOne
    {
        $languageId = App::getLanguage()->id;
        $translationsModel = get_class($this->translations()->getRelated());
        return $this->hasOne($translationsModel)->where('language_id', '=', $languageId);
    }

    /**
     * @param AbstractTranslatableModel $model
     * @param Language $language
     * @return AbstractTranslationModel|null The translation
     */
    public function getTranslationModelByLanguage(AbstractTranslatableModel &$model, Language $language): ?AbstractTranslationModel{
        $translation = $this->translations->filter(function(AbstractTranslationModel $translation) use ($language) {
            return $translation->language_id == $language->id;
        })->first();

        return $translation;
    }
}