51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Data;
|
||
|
|
||
|
use Illuminate\Support\Collection;
|
||
|
use Illuminate\Support\Facades\Cache;
|
||
|
use Illuminate\Support\Str;
|
||
|
|
||
|
class ItemsCollection
|
||
|
{
|
||
|
protected Collection $data;
|
||
|
|
||
|
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);
|
||
|
} else {
|
||
|
$this->data = Cache::get('items');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $query
|
||
|
* @return array
|
||
|
*/
|
||
|
public function findItem(string $query)
|
||
|
{
|
||
|
return $this->data->filter(function ($val) use ($query) {
|
||
|
return Str::contains($val['_id'], $query)
|
||
|
|| Str::contains($val['_name'], $query)
|
||
|
|| Str::contains($val['_parent'], $query);
|
||
|
})->map(function ($item) {
|
||
|
return [
|
||
|
'_id' => $item['_id'],
|
||
|
'_name' => $item['_name'],
|
||
|
];
|
||
|
})->all();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $id
|
||
|
* @return array
|
||
|
*/
|
||
|
public function getItemById(string $id)
|
||
|
{
|
||
|
return $this->data[$id];
|
||
|
}
|
||
|
}
|