Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 11, 2024
1 parent dd6d2be commit 784e9ed
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 239 deletions.
26 changes: 0 additions & 26 deletions database/factories/TranslationValueFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public function up(): void
$table->id();
$table->morphs('translatable');
$table->string('language', 8);
$table->json('values')->nullable();
$table->timestamps();

$table->unique(['translatable_id', 'translatable_type', 'language']);
$table->unique(['translatable_id', 'translatable_type', 'language'], 'root_translatable_language');
});
}

Expand Down

This file was deleted.

7 changes: 6 additions & 1 deletion src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,12 @@ public function withoutOldValue(): mixed
*/
public function getValue(Model $model): mixed
{
return $model->getAttribute($this->getModelAttribute());
$attribute = $this->getModelAttribute();

return match (true) {
str_contains($attribute, '->') => data_get($model, str_replace('->', '.', $attribute)),
default => $model->getAttribute($this->getModelAttribute()),
};
}

/**
Expand Down
63 changes: 3 additions & 60 deletions src/Fields/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Models\TranslationValue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
Expand Down Expand Up @@ -81,63 +79,6 @@ public function resolveDisplay(Model $related): ?string
return parent::resolveDisplay($related);
}

/**
* {@inheritdoc}
*/
public function persist(Request $request, Model $model, mixed $value): void
{
$this->resolveHydrate($request, $model, $value);

$model->saved(static function (Model $model): void {
$model->values->each(function (TranslationValue $value) use ($model): void {
$value->translation()->associate($model)->save();
});
});
}

/**
* {@inheritdoc}
*/
public function resolveHydrate(Request $request, Model $model, mixed $value): void
{
if (is_null($this->hydrateResolver)) {
$this->hydrateResolver = function (Request $request, Model $model, array $value): void {
foreach ($value as $key => $attr) {
if ($key === 'language') {
$model->setAttribute($key, $attr);
} else {
$related = $model->values->firstWhere('key', $key) ?: $model->values()->make();

$cast = $model->related->getCasts()[$key] ?? 'string';

$related->mergeCasts(['value' => $cast])->fill(['key' => $key, 'value' => $attr]);

$model->values->when(
! $related->exists,
fn (Collection $collection): Collection => $collection->push($related)
);
}
}
};
}

parent::resolveHydrate($request, $model, $value);
}

/**
* {@inheritdoc}
*/
public function getValueForHydrate(Request $request): mixed
{
return $this->resolveFields($request)
->mapWithKeys(static function (Field $field) use ($request): mixed {
return [
$field->getModelAttribute() => $field->getValueForHydrate($request),
];
})
->all();
}

/**
* {@inheritdoc}
*/
Expand All @@ -153,9 +94,11 @@ public function fields(Request $request): array
$model->related->translations->pluck('language')->all()
);

return is_null($model->language)
$options = is_null($model->language)
? $options
: array_unique(array_merge([$model->language], $options));

return array_combine($options, $options);
})
->required()
->rules(['required', 'string', Rule::in($this->getLanguages())]),
Expand Down
7 changes: 6 additions & 1 deletion src/Interfaces/Models/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace Cone\Root\Interfaces\Models;

use Illuminate\Database\Eloquent\Relations\MorphTo;

interface Translation
{
//
/**
* Get the translatable model for the translation.
*/
public function translatable(): MorphTo;
}
8 changes: 0 additions & 8 deletions src/Interfaces/Models/TranslationValue.php

This file was deleted.

18 changes: 7 additions & 11 deletions src/Models/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Cone\Root\Traits\InteractsWithProxy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Translation extends Model implements Contract
Expand All @@ -21,14 +20,19 @@ class Translation extends Model implements Contract
*
* @var array<string, string>
*/
protected $casts = [];
protected $casts = [
'values' => 'json',
];

/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [];
protected $fillable = [
'language',
'values',
];

/**
* The table associated with the model.
Expand Down Expand Up @@ -60,12 +64,4 @@ public function translatable(): MorphTo
{
return $this->morphTo();
}

/**
* Get the translation values for the translation.
*/
public function values(): HasMany
{
return $this->hasMany(TranslationValue::getProxiedClass());
}
}
65 changes: 0 additions & 65 deletions src/Models/TranslationValue.php

This file was deleted.

18 changes: 5 additions & 13 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,11 @@ public function resolveTranslationsField(Request $request): ?Translations
return $this->resolveFields($request)
->translatable()
->map(static function (Field $field): Field {
$field = clone $field;

$field->translatable(false);

return $field->value(static function (Request $request, Model $model) use ($field): mixed {
$key = $field->getModelAttribute();

$cast = $model->related->getCasts()[$key] ?? 'string';

return $model->values->firstWhere('key', $key)
?->mergeCasts(['value' => $cast])
?->value;
});
return (clone $field)
->translatable(false)
->setModelAttribute($key = 'values->'.$field->getModelAttribute())
->name($key)
->id($key);
})
->all();
})
Expand Down
1 change: 0 additions & 1 deletion src/RootServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class RootServiceProvider extends ServiceProvider
Interfaces\Models\Notification::class => Models\Notification::class,
Interfaces\Models\Setting::class => Models\Setting::class,
Interfaces\Models\Translation::class => Models\Translation::class,
Interfaces\Models\TranslationValue::class => Models\TranslationValue::class,
Interfaces\Models\User::class => Models\User::class,
Interfaces\Navigation\Registry::class => Navigation\Registry::class,
Interfaces\Settings\Registry::class => Settings\Registry::class,
Expand Down
22 changes: 2 additions & 20 deletions src/Traits/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Cone\Root\Traits;

use Cone\Root\Models\Translation;
use Cone\Root\Models\TranslationValue;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Facades\App;

Expand All @@ -18,31 +16,15 @@ public function translations(): MorphMany
return $this->morphMany(Translation::getProxiedClass(), 'translatable');
}

/**
* Get the translation values for the model.
*/
public function translationValues(): HasManyThrough
{
return $this->hasManyThrough(TranslationValue::getProxiedClass(), Translation::getProxiedClass(), 'translatable_id')
->where('root_translations.translatable_type', static::class)
->select(['*', 'root_translations.language as language']);
}

/**
* Translate the value of the given key.
*/
public function translate(string $key, ?string $language = null): mixed
{
$language ??= App::getLocale();

$value = $this->translationValues->first(function (TranslationValue $value) use ($key, $language): bool {
return $value->key === $key && $value->language === $language;
});

if (is_null($value)) {
return $this->getAttribute($key);
}
$translation = $this->translations->firstWhere('language', $language);

return $value->mergeCasts(['key' => $this->getCasts()[$key] ?? 'string'])->value;
return $translation?->values[$key] ?? null;
}
}

0 comments on commit 784e9ed

Please sign in to comment.