spt-items-api/app/Data/ItemsCollection.php

107 lines
3.9 KiB
PHP
Raw Normal View History

2021-10-08 20:44:34 +09:00
<?php
namespace App\Data;
use App\Exceptions\ItemNotFoundException;
2021-10-08 20:44:34 +09:00
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use App\Config\GiteaConfig;
use Illuminate\Support\Facades\Log;
2021-10-08 20:44:34 +09:00
class ItemsCollection
{
2021-10-11 17:26:19 +09:00
protected Collection $items;
protected Collection $locales;
2021-10-08 20:44:34 +09:00
private string $items_cache_key = 'items';
private string $locales_cache_key = 'locales';
public function __construct() {
if (!Cache::has($this->items_cache_key)) {
$this->refreshItemsCache();
}
else $this->items = Cache::get($this->items_cache_key);
if (!Cache::has($this->locales_cache_key)) {
$this->refreshLocalesCache();
2021-10-11 17:26:19 +09:00
}
else $this->locales = Cache::get($this->locales_cache_key);
}
public function refreshLocalesCache(): void
{
$this->locales = collect();
$raw_locales_global_base_url = GiteaConfig::RAW_LOCALES_GLOBAL_BASE_URL;
// Getting all locales in project/assets/database/locales/global from the Server development branch repository
$locales_list = collect(json_decode(file_get_contents(GiteaConfig::LOCALES_GLOBAL_URL), true));
foreach($locales_list as $item) {
// Extract the json name for the locale
preg_match('/([a-z]{2}(-[a-z]{2})?).json/', $item['name'], $current_locale_name, PREG_OFFSET_CAPTURE);
// If the name is not supported for any reason, dont add it to the locales
if (empty($current_locale_name) || !$current_locale_name[1][0]) continue;
$trimmed_current_locale_name = trim($current_locale_name[1][0]);
$test = collect([$trimmed_current_locale_name => json_decode(file_get_contents("${raw_locales_global_base_url}/${trimmed_current_locale_name}.json"), true)['templates']]);
$this->locales = $this->locales->merge($test);
2021-10-08 20:44:34 +09:00
}
2021-10-11 19:47:34 -04:00
Cache::put($this->locales_cache_key, $this->locales);
}
public function refreshItemsCache(): void {
$this->items = collect(json_decode(file_get_contents(GiteaConfig::RAW_ITEMS_URL), true));
Cache::put($this->items_cache_key, $this->items);
}
2021-10-11 19:47:34 -04:00
public function getLocales(): Collection {
return $this->locales->keys();
}
public function refreshAllCache(): void {
$this->refreshItemsCache();
$this->refreshLocalesCache();
2021-10-08 20:44:34 +09:00
}
/**
* @param string $query the content of the query eg. 'AK'
* @param string $locale the chosen local. Default to 'en'
* @return Collection
2021-10-08 20:44:34 +09:00
*/
public function findItem(string $query, string $locale = 'en'): Collection
2021-10-08 20:44:34 +09:00
{
return $this->items->filter(function ($val, $key) use ($query, $locale) {
2021-10-10 13:08:50 +09:00
$query = Str::lower($query);
2021-10-08 20:44:34 +09:00
return Str::contains($val['_id'], $query)
|| Str::contains($val['_name'], $query)
|| Str::contains($val['_parent'], $query)
|| (($this->locales[$locale][$key] ?? false)
&& $this->locales[$locale][$key]['Name']
&& Str::contains(Str::lower($this->locale[$locale][$key]['Name']), $query)
&& Str::contains(Str::lower($this->locale[$locale][$key]['ShortName']), $query));
})->map(function ($item) use ($locale) {
2021-10-08 20:44:34 +09:00
return [
'_id' => $item['_id'],
'_name' => $item['_name'],
'locale' => $this->locales[$locale][$item['_id']] ?? ''
2021-10-08 20:44:34 +09:00
];
})->values();
2021-10-08 20:44:34 +09:00
}
/**
* @param string $id the item ID to look for
* @param string $locale the chosen local. Default to 'en'
2021-10-08 20:44:34 +09:00
* @return array
* @throws ItemNotFoundException
2021-10-08 20:44:34 +09:00
*/
public function getItemById(string $id, string $locale = 'en'): array
2021-10-08 20:44:34 +09:00
{
2021-10-11 17:26:19 +09:00
$item = $this->items[$id] ?? throw new ItemNotFoundException('Item not found');
return [
'item' => $item,
'locale' => $this->locales[$locale][$id] ?? ''
2021-10-11 17:26:19 +09:00
];
2021-10-08 20:44:34 +09:00
}
}