File: D:/HostingSpaces/SBogers10/komma.pro/app/KommaApp/DisplayNameTrait.php
<?php
namespace App\KommaApp;
/**
* Trait DisplayNameTrait
*
* Retrieves the name of a model that you can show to a user in the following way:
* 1. It wil have a look at the translationKeyNew variable. If it's key is set and exists, it wil return the
* appropriate translation.
* 2. If the previous trick failed it will pluralize the class name look for a translation file in the kms or shop
* translations folder that have that name in that order. If one of those translation files has a key called "new"
* in their array, it wil return the translation for that key. For pages this would resolve to the translation
* of key 'kms/page.new'.
* 3. If previous trick also fails it wil get translation of key "kms/global.new_plural" and suffix it the class name.
*
* that is the plural form of the class name in either
*
* @mixin \Eloquent
* @package App\KommaApp
*/
trait DisplayNameTrait
{
/** @var string $translationKeyNew The translation key that represents the display name when new */
protected static $translationKeyNew;
/**
* @return String
*/
public function getDisplayName(): String
{
if($this->exists && ($this->title || $this->name)) {
$displayName = $this->title ?: $this->name;
} else {
//Try to get the display name by using the translationKeyNew variable if set.
if(static::$translationKeyNew) {
$potentialTranslation = __(static::$translationKeyNew);
if($potentialTranslation !== static::$translationKeyNew) {
return $potentialTranslation;
}
}
//Translation could not be retrieved by the translationsKeyNew variable. Try to get the translation using the models class in plural form suffixed with new. For a page model this would be te key: 'kms/pages.new'
$className = static::FQCNToShortClassName();
$pluralClassname = str_plural($className);
$translationKeysToTry = [
'kms/'.$pluralClassname.'.new',
'shop/'.$pluralClassname.'.new'
];
foreach($translationKeysToTry as $translationKeyToTry)
{
$potentialTranslation = __($translationKeyToTry);
if($potentialTranslation !== $translationKeyToTry) {
return $potentialTranslation;
}
}
//Previous tricks did not work. Do a best effort by guessing it the translation
$new = ucfirst(__('kms/global.new_plural'));
return $new.' '.$className;
}
return $displayName;
}
/**
* Returns class name from the Fully Qualified Class Name of the class this trait is used on
*
* @return string
*/
private static function FQCNToShortClassName()
{
$FQCNSplit = explode('\\', static::class);
$shortModelClassName = $FQCNSplit[count($FQCNSplit)-1];
return snake_case($shortModelClassName);
}
}