From 5b8f54b1382ff9853e339a4b76b9e7b2acd6eb9d Mon Sep 17 00:00:00 2001 From: Rev Date: Mon, 11 Oct 2021 17:26:19 +0900 Subject: [PATCH] pass the locale along the item data --- app/Data/ItemsCollection.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/app/Data/ItemsCollection.php b/app/Data/ItemsCollection.php index d6916da..d339982 100644 --- a/app/Data/ItemsCollection.php +++ b/app/Data/ItemsCollection.php @@ -9,16 +9,24 @@ use Illuminate\Support\Str; class ItemsCollection { - protected Collection $data; + protected Collection $items; + protected Collection $locale; public function __construct() { if (!Cache::has('items')) { $file = storage_path('items.json'); - $this->data = collect(json_decode(file_get_contents($file), true)['data']); - Cache::put('items', $this->data); + $this->items = collect(json_decode(file_get_contents($file), true)['data']); + Cache::put('items', $this->items); } else { - $this->data = Cache::get('items'); + $this->items = Cache::get('items'); + } + if (!Cache::has('locale')) { + $file = storage_path('locale.json'); + $this->locale = collect(json_decode(file_get_contents($file), true)['templates']); + Cache::put('locale', $this->locale); + } else { + $this->locale = Cache::get('locale'); } } @@ -28,7 +36,7 @@ class ItemsCollection */ public function findItem(string $query): Collection { - return $this->data->filter(function ($val) use ($query) { + return $this->items->filter(function ($val) use ($query) { $query = Str::lower($query); return Str::contains($val['_id'], $query) || Str::contains($val['_name'], $query) @@ -48,6 +56,10 @@ class ItemsCollection */ public function getItemById(string $id): array { - return $this->data[$id] ?? throw new ItemNotFoundException('Item not found'); + $item = $this->items[$id] ?? throw new ItemNotFoundException('Item not found'); + return [ + 'item' => $item, + 'locale' => $this->locale[$id] ?? '' + ]; } }