From ed8d8107c0817cdca0af13a76764538f478486fe Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 3 Oct 2024 16:49:16 +0200 Subject: [PATCH] up --- Actions/SaveTransAction.php | 5 +- Casts/LangField.php | 16 ++-- ...8_01_01_080016_create_lang_posts_table.php | 3 +- ...01_01_000003_create_translations_table.php | 3 +- Datas/TranslationData.php | 7 +- Http/Livewire/Lang/Change.php | 2 +- Models/Contracts/HasTranslationsContract.php | 2 +- Models/Post.php | 74 ++++++++++--------- Models/Traits/LinkedTrait.php | 6 +- Models/Translation.php | 44 +++++------ Providers/EventServiceProvider.php | 4 +- Providers/RouteServiceProvider.php | 9 ++- Services/TranslatorService.php | 10 +-- View/Components/Flag.php | 4 +- View/Composers/ThemeComposer.php | 18 +++-- 15 files changed, 105 insertions(+), 102 deletions(-) diff --git a/Actions/SaveTransAction.php b/Actions/SaveTransAction.php index bd3e189..5ced096 100755 --- a/Actions/SaveTransAction.php +++ b/Actions/SaveTransAction.php @@ -4,6 +4,7 @@ namespace Modules\Lang\Actions; +use Exception; use Illuminate\Support\Arr; use Illuminate\Support\Facades\File; use Modules\Xot\Actions\Array\SaveArrayAction; @@ -24,14 +25,14 @@ public function execute(string $key, int|string|array|null $data): void $cont = []; } $piece = implode('.', array_slice(explode('.', $key), 1)); - if ('' !== $piece) { + if ($piece !== '') { Arr::set($cont, $piece, $data); } else { $cont = $data; } if (! is_array($cont)) { - throw new \Exception('Error in SaveTransAction'); + throw new Exception('Error in SaveTransAction'); } app(SaveArrayAction::class)->execute(data: $cont, filename: $filename); diff --git a/Casts/LangField.php b/Casts/LangField.php index f121532..a231987 100755 --- a/Casts/LangField.php +++ b/Casts/LangField.php @@ -12,9 +12,9 @@ class LangField implements CastsAttributes /** * Cast the given value. * - * @param BaseModelLang $model - * @param string $key - * @param array $attributes + * @param BaseModelLang $model + * @param string $key + * @param array $attributes */ public function get($model, $key, $value, $attributes) { @@ -24,11 +24,11 @@ public function get($model, $key, $value, $attributes) /** * Prepare the given value for storage. * - * @param BaseModelLang $model - * @param string $key - * @param array $attributes - * @param string $key - * @param string $value + * @param BaseModelLang $model + * @param string $key + * @param array $attributes + * @param string $key + * @param string $value */ public function set($model, $key, $value, $attributes): array { diff --git a/Database/Migrations/2018_01_01_080016_create_lang_posts_table.php b/Database/Migrations/2018_01_01_080016_create_lang_posts_table.php index 2d0dc50..f046524 100755 --- a/Database/Migrations/2018_01_01_080016_create_lang_posts_table.php +++ b/Database/Migrations/2018_01_01_080016_create_lang_posts_table.php @@ -11,7 +11,8 @@ /* * Class CreateLangPostsTable. */ -return new class extends XotBaseMigration { +return new class extends XotBaseMigration +{ protected ?string $model_class = Post::class; /** diff --git a/Database/Migrations/2023_01_01_000003_create_translations_table.php b/Database/Migrations/2023_01_01_000003_create_translations_table.php index 4d746be..8e13ca7 100755 --- a/Database/Migrations/2023_01_01_000003_create_translations_table.php +++ b/Database/Migrations/2023_01_01_000003_create_translations_table.php @@ -10,7 +10,8 @@ /* * Class CreateTranslationsTable. */ -return new class extends XotBaseMigration { +return new class extends XotBaseMigration +{ // protected ?string $model_class = Post::class; /** * db up. diff --git a/Datas/TranslationData.php b/Datas/TranslationData.php index 1a71330..4d733c2 100755 --- a/Datas/TranslationData.php +++ b/Datas/TranslationData.php @@ -4,6 +4,7 @@ namespace Modules\Lang\Datas; +use Exception; use Illuminate\Support\Facades\File; use Spatie\LaravelData\Data; @@ -25,8 +26,8 @@ public function getFilename(): string { $hints = app('translator')->getLoader()->namespaces(); $path = collect($hints)->get($this->namespace); - if (null === $path) { - throw new \Exception('['.__LINE__.']['.class_basename($this).']'); + if ($path === null) { + throw new Exception('['.__LINE__.']['.class_basename($this).']'); } return app(\Modules\Xot\Actions\File\FixPathAction::class)->execute($path.'/'.$this->lang.'/'.$this->group.'.php'); @@ -40,7 +41,7 @@ public function getData(): array $data = File::getRequire($filename); } if (! is_array($data)) { - throw new \Exception('['.__LINE__.']['.class_basename($this).']'); + throw new Exception('['.__LINE__.']['.class_basename($this).']'); } return $data; diff --git a/Http/Livewire/Lang/Change.php b/Http/Livewire/Lang/Change.php index 98ef0f7..7e15a03 100755 --- a/Http/Livewire/Lang/Change.php +++ b/Http/Livewire/Lang/Change.php @@ -36,7 +36,7 @@ public function mount(): void $this->url = Request::getRequestUri(); $langs = Arr::map($langs, function (array $item, $key) { $url = LaravelLocalization::getLocalizedURL($key, $this->url, [], true); - if (false !== $url) { + if ($url !== false) { $url = Str::of($url)->replace(url(''), '')->toString(); } $item['url'] = $url; diff --git a/Models/Contracts/HasTranslationsContract.php b/Models/Contracts/HasTranslationsContract.php index 834d806..ff2b75b 100755 --- a/Models/Contracts/HasTranslationsContract.php +++ b/Models/Contracts/HasTranslationsContract.php @@ -9,7 +9,7 @@ interface HasTranslationsContract public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true): mixed; /** - * @param int|array|string|null $value + * @param int|array|string|null $value */ public function setTranslation(string $key, string $locale, $value): self; } diff --git a/Models/Post.php b/Models/Post.php index f45de1c..2ad7efa 100755 --- a/Models/Post.php +++ b/Models/Post.php @@ -18,41 +18,43 @@ use Spatie\Sluggable\HasSlug; use Spatie\Sluggable\SlugOptions; +use function is_string; + /** * Modules\Lang\Models\Post. * - * @property int $id - * @property int|null $user_id - * @property string|null $post_type - * @property int|null $post_id - * @property string|null $lang - * @property string|null $title - * @property string|null $subtitle - * @property string|null $guid - * @property string|null $txt - * @property string|null $image_src - * @property string|null $image_alt - * @property string|null $image_title - * @property string|null $meta_description - * @property string|null $meta_keywords - * @property int|null $author_id - * @property Carbon|null $created_at - * @property Carbon|null $updated_at - * @property int|null $category_id - * @property string|null $image - * @property string|null $content - * @property int|null $published - * @property string|null $created_by - * @property string|null $updated_by - * @property string|null $url - * @property array|null $url_lang - * @property array|null $image_resize_src - * @property string|null $linked_count - * @property string|null $related_count - * @property string|null $relatedrev_count - * @property string|null $linkable_type - * @property int|null $views_count - * @property Model|\Eloquent $linkable + * @property int $id + * @property int|null $user_id + * @property string|null $post_type + * @property int|null $post_id + * @property string|null $lang + * @property string|null $title + * @property string|null $subtitle + * @property string|null $guid + * @property string|null $txt + * @property string|null $image_src + * @property string|null $image_alt + * @property string|null $image_title + * @property string|null $meta_description + * @property string|null $meta_keywords + * @property int|null $author_id + * @property Carbon|null $created_at + * @property Carbon|null $updated_at + * @property int|null $category_id + * @property string|null $image + * @property string|null $content + * @property int|null $published + * @property string|null $created_by + * @property string|null $updated_by + * @property string|null $url + * @property array|null $url_lang + * @property array|null $image_resize_src + * @property string|null $linked_count + * @property string|null $related_count + * @property string|null $relatedrev_count + * @property string|null $linkable_type + * @property int|null $views_count + * @property Model|Eloquent $linkable * * @method static Builder|Post newModelQuery() * @method static Builder|Post newQuery() @@ -214,7 +216,7 @@ public function setTitleAttribute(string $value): void */ public function getTitleAttribute(?string $value): ?string { - if (null !== $value) { + if ($value !== null) { return $value; } @@ -236,14 +238,14 @@ public function getTitleAttribute(?string $value): ?string */ public function getGuidAttribute(?string $value): ?string { - if (\is_string($value) && '' !== $value && ! str_contains($value, ' ')) { + if (is_string($value) && $value !== '' && ! str_contains($value, ' ')) { return $value; } $value = $this->title; - if ('' === $value) { + if ($value === '') { $value = $this->attributes['post_type'].' '.$this->attributes['post_id']; } - if (null === $value) { + if ($value === null) { $value = 'u-'.random_int(1, 1000); } $value = Str::slug($value); diff --git a/Models/Traits/LinkedTrait.php b/Models/Traits/LinkedTrait.php index c6b6457..75e3d27 100755 --- a/Models/Traits/LinkedTrait.php +++ b/Models/Traits/LinkedTrait.php @@ -19,8 +19,6 @@ * Modules\Lang\Models\Traits\LinkedTrait. * * @property \Modules\Xot\Contracts\UserContract|null $user - * @property Post $post + * @property Post $post */ -trait LinkedTrait -{ -} +trait LinkedTrait {} diff --git a/Models/Translation.php b/Models/Translation.php index d11489d..37079d8 100755 --- a/Models/Translation.php +++ b/Models/Translation.php @@ -17,7 +17,7 @@ /** * Modules\Lang\Models\Translation. * - * @property int $id + * @property int $id * @property string|null $lang * @property string|null $key * @property string|null $value @@ -25,27 +25,27 @@ * @property string|null $updated_by * @property Carbon|null $created_at * @property Carbon|null $updated_at - * @property string $namespace - * @property string $group + * @property string $namespace + * @property string $group * @property string|null $item * - * @method static \Illuminate\Database\Eloquent\Builder|Translation newModelQuery() - * @method static \Illuminate\Database\Eloquent\Builder|Translation newQuery() - * @method static \Illuminate\Database\Eloquent\Builder|Translation ofTranslatedGroup(string $group) - * @method static \Illuminate\Database\Eloquent\Builder|Translation orderByGroupKeys(bool $ordered) - * @method static \Illuminate\Database\Eloquent\Builder|Translation query() - * @method static \Illuminate\Database\Eloquent\Builder|Translation selectDistinctGroup() - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereCreatedAt($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereCreatedBy($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereGroup($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereItem($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereKey($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereLang($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereNamespace($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereUpdatedAt($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereUpdatedBy($value) - * @method static \Illuminate\Database\Eloquent\Builder|Translation whereValue($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Translation newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Translation ofTranslatedGroup(string $group) + * @method static \Illuminate\Database\Eloquent\Builder|Translation orderByGroupKeys(bool $ordered) + * @method static \Illuminate\Database\Eloquent\Builder|Translation query() + * @method static \Illuminate\Database\Eloquent\Builder|Translation selectDistinctGroup() + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereCreatedBy($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereGroup($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereItem($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereKey($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereLang($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereNamespace($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereUpdatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereUpdatedBy($value) + * @method static \Illuminate\Database\Eloquent\Builder|Translation whereValue($value) * @method static \Modules\Lang\Database\Factories\TranslationFactory factory($count = null, $state = []) * * @property \Modules\Xot\Contracts\ProfileContract|null $creator @@ -83,12 +83,12 @@ public function scopeOrderByGroupKeys(EloquentBuilder $query, bool $ordered): El public function scopeSelectDistinctGroup(EloquentBuilder $query): EloquentBuilder|QueryBuilder { - $select = match (\DB::getDriverName()) { + $select = match (DB::getDriverName()) { 'mysql' => 'DISTINCT `group`', default => 'DISTINCT "group"', }; - return $query->select(\DB::raw($select)); + return $query->select(DB::raw($select)); } /* diff --git a/Providers/EventServiceProvider.php b/Providers/EventServiceProvider.php index a1034fb..408bf23 100755 --- a/Providers/EventServiceProvider.php +++ b/Providers/EventServiceProvider.php @@ -25,7 +25,5 @@ class EventServiceProvider extends BaseEventServiceProvider /** * Configure the proper event listeners for email verification. */ - protected function configureEmailVerification(): void - { - } + protected function configureEmailVerification(): void {} } diff --git a/Providers/RouteServiceProvider.php b/Providers/RouteServiceProvider.php index ad0d629..5b2f092 100755 --- a/Providers/RouteServiceProvider.php +++ b/Providers/RouteServiceProvider.php @@ -6,6 +6,9 @@ use Modules\Xot\Providers\XotBaseRouteServiceProvider; +use function in_array; +use function is_array; + class RouteServiceProvider extends XotBaseRouteServiceProvider { /** @@ -30,7 +33,7 @@ public function registerCallback(): void public function registerLang(): void { $locales = config('laravellocalization.supportedLocales'); - if (! \is_array($locales)) { + if (! is_array($locales)) { $locales = ['it' => 'it', 'en' => 'en']; } $langs = array_keys($locales); @@ -46,9 +49,9 @@ public function registerLang(): void $n = 3; } - if (\in_array(request()->segment($n), $langs, false)) { + if (in_array(request()->segment($n), $langs, false)) { $lang = request()->segment($n); - if (null !== $lang) { + if ($lang !== null) { app()->setLocale($lang); } } diff --git a/Services/TranslatorService.php b/Services/TranslatorService.php index 06d5359..aa0182c 100755 --- a/Services/TranslatorService.php +++ b/Services/TranslatorService.php @@ -20,10 +20,9 @@ class TranslatorService extends LaravelTranslator /** * Get the translation for the given key. * - * @param string $key - * @param string|null $locale - * @param bool $fallback - * + * @param string $key + * @param string|null $locale + * @param bool $fallback * @return string|array */ public function get($key, array $replace = [], $locale = null, $fallback = true) @@ -49,8 +48,7 @@ public function setTranslationManager(Manager $manager) /** * Undocumented function. * - * @param string $key - * + * @param string $key * @return void */ protected function notifyMissingKey($key) diff --git a/View/Components/Flag.php b/View/Components/Flag.php index 03eb2db..20cda7a 100755 --- a/View/Components/Flag.php +++ b/View/Components/Flag.php @@ -13,9 +13,7 @@ */ class Flag extends Component { - public function __construct(public string $name) - { - } + public function __construct(public string $name) {} public function render(): Renderable { diff --git a/View/Composers/ThemeComposer.php b/View/Composers/ThemeComposer.php index 0341316..9fdef46 100755 --- a/View/Composers/ThemeComposer.php +++ b/View/Composers/ThemeComposer.php @@ -4,8 +4,10 @@ namespace Modules\Lang\View\Composers; +use Exception; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; +use InvalidArgumentException; use Modules\Lang\Datas\LangData; use Spatie\LaravelData\DataCollection; @@ -14,27 +16,27 @@ class ThemeComposer /** * Get all supported languages as a DataCollection. * - * @throws \Exception if supportedLocales config is not an array - * * @return DataCollection + * + * @throws Exception if supportedLocales config is not an array */ public function languages(): DataCollection { $langs = config('laravellocalization.supportedLocales'); if (! is_array($langs)) { - throw new \Exception(sprintf('Invalid config for supportedLocales on line %d in %s', __LINE__, class_basename($this))); + throw new Exception(sprintf('Invalid config for supportedLocales on line %d in %s', __LINE__, class_basename($this))); } $languages = collect($langs)->map(function (mixed $item, string $locale): array { // Ensure $item is an array as expected, otherwise handle error. if (! is_array($item) || ! isset($item['regional'], $item['name'])) { - throw new \InvalidArgumentException(sprintf('Expected array with "regional" and "name" keys at locale %s', $locale)); + throw new InvalidArgumentException(sprintf('Expected array with "regional" and "name" keys at locale %s', $locale)); } // Extract regional code and handle 'en' to 'gb' mapping. $regionalCode = explode('_', (string) $item['regional'])[0] ?? 'en'; - if ('en' === $regionalCode) { + if ($regionalCode === 'en') { $regionalCode = 'gb'; } @@ -67,7 +69,7 @@ public function otherLanguages(): DataCollection ->filter(function (mixed $item) use ($currentLocale): bool { // Ensure the item is an instance of LangData if (! $item instanceof LangData) { - throw new \Exception(sprintf('Expected instance of LangData, got %s on line %d in %s', is_object($item) ? get_class($item) : gettype($item), __LINE__, class_basename($this))); + throw new Exception(sprintf('Expected instance of LangData, got %s on line %d in %s', is_object($item) ? get_class($item) : gettype($item), __LINE__, class_basename($this))); } // Filter out the current locale @@ -78,7 +80,7 @@ public function otherLanguages(): DataCollection /** * Get a specific field of the current language. * - * @throws \Exception if the current language is not found + * @throws Exception if the current language is not found */ public function currentLang(string $field): string { @@ -90,7 +92,7 @@ public function currentLang(string $field): string ->firstWhere('id', $currentLocale); if (! $lang instanceof LangData) { - throw new \Exception(sprintf('Current language not found on line %d in %s', __LINE__, class_basename($this))); + throw new Exception(sprintf('Current language not found on line %d in %s', __LINE__, class_basename($this))); } return (string) $lang->{$field};