Skip to content

Instantly share code, notes, and snippets.

@koyablue
Last active November 4, 2021 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koyablue/1150419297fcfac1fdc7c5d5c1728fed to your computer and use it in GitHub Desktop.
Save koyablue/1150419297fcfac1fdc7c5d5c1728fed to your computer and use it in GitHub Desktop.
<?php
namespace App\Packages\FrontPage\Shared\Presentation;
use App\Packages\FrontPage\Shared\UseCase\LocaleSessionInterface;
use App\Packages\FrontPage\Shared\Domain\Models\Locale;
use App\Packages\FrontPage\Shared\Presentation\Dto\GetLocaleSessionResponse;
class LocaleSession implements LocaleSessionInterface
{
/**
* セッションキー
*
* @return string
*/
public function getLocaleSessionKey(): string
{
return 'locale';
}
/**
* locale変更用セッション保存
*
* @param Locale $locale
* @return void
*/
public function setLocaleSession(Locale $locale)
{
$sessionKey = $this->getLocaleSessionKey();
session([$sessionKey => $locale->getLocale()]);
}
/**
* 現在のlocaleをセッションから取得
* なければデフォルトlocale
*
* @return GetLocaleSessionResponse
*/
public function getLocaleSession(): GetLocaleSessionResponse
{
$sessionKey = $this->getLocaleSessionKey();
$locale = session()->has($sessionKey)
? session($sessionKey)
: config('app.locale');
return new GetLocaleSessionResponse($locale);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment