-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff33e87
commit 5465f4e
Showing
11 changed files
with
255 additions
and
184 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Models\Services; | ||
|
||
use Closure; | ||
|
||
class Registry | ||
{ | ||
public static array $values = []; | ||
|
||
public static function get(string $key, Closure $callback): mixed | ||
{ | ||
return static::$values[$key] ?? $callback(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Models\Services; | ||
|
||
use Illuminate\Database\Eloquent\Collection as DBCollection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
use LaravelLang\Config\Facades\Config; | ||
use LaravelLang\Locales\Data\LocaleData; | ||
use LaravelLang\Locales\Facades\Locales; | ||
use LaravelLang\Models\Eloquent\Translation; | ||
|
||
class Relation | ||
{ | ||
public static function initializeModel(Model $model): void | ||
{ | ||
if (blank($model->translations) && blank($model->load('translations')->translations)) { | ||
$model->setRelation('translations', new DBCollection()); | ||
} | ||
|
||
static::locales()->each(function (LocaleData $locale) use ($model) { | ||
if (! $model->translations?->has($locale->code)) { | ||
$model->translations->put($locale->code, static::initializeLocale($model, $locale->code)); | ||
} | ||
}); | ||
} | ||
|
||
public static function initializeLocale(Model $model, string $locale): Translation | ||
{ | ||
return (new (static::modelName($model))()) | ||
->setAttribute('item_id', $model->getKey()) | ||
->setAttribute('locale', $locale); | ||
} | ||
|
||
protected static function modelName(Model $model): string | ||
{ | ||
return get_class($model) . Config::shared()->models->suffix; | ||
} | ||
|
||
protected static function locales(): Collection | ||
{ | ||
return Locales::installed(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.