pass the locale along the item data

This commit is contained in:
Rev 2021-10-11 17:26:19 +09:00
parent 9266f96414
commit 5b8f54b138

View File

@ -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] ?? ''
];
}
}