2023-01-24 23:30:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
use Illuminate\Support\Collection;
|
2023-07-15 14:44:42 +01:00
|
|
|
use Illuminate\Support\Arr;
|
2023-01-24 23:30:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Jikan behavior config
|
|
|
|
*/
|
|
|
|
final class JikanConfig
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, int> $perEndpointCacheTtl
|
|
|
|
*/
|
|
|
|
private array $perEndpointCacheTtl;
|
|
|
|
|
|
|
|
private int $defaultCacheExpire;
|
|
|
|
|
2023-02-02 23:37:37 +00:00
|
|
|
private bool $microCachingEnabled;
|
|
|
|
|
2023-06-26 18:42:51 +01:00
|
|
|
private Collection $config;
|
|
|
|
|
2023-07-09 10:50:18 +01:00
|
|
|
private int $textMatchBuckets;
|
|
|
|
|
|
|
|
private int $typoTokensThreshold;
|
|
|
|
|
|
|
|
private int $dropTokensThreshold;
|
|
|
|
|
|
|
|
private int $searchCutOffMs;
|
|
|
|
|
|
|
|
private string $exhaustiveSearch;
|
|
|
|
|
2023-01-24 23:30:47 +00:00
|
|
|
public function __construct(array $config)
|
|
|
|
{
|
2023-07-15 14:44:42 +01:00
|
|
|
$this->perEndpointCacheTtl = Arr::get($config, "per_endpoint_cache_ttl", []);
|
|
|
|
$this->defaultCacheExpire = Arr::get($config, "default_cache_expire", 0);
|
|
|
|
$this->microCachingEnabled = in_array(Arr::get($config, "micro_caching_enabled", false), [true, 1, "1", "true"]);
|
|
|
|
$this->textMatchBuckets = Arr::get($config,"typesense_options.text_match_buckets", 1);
|
|
|
|
$this->exhaustiveSearch = (string) Arr::get($config, "typesense_options.exhaustive_search", "false");
|
|
|
|
$this->config = collect($config);
|
|
|
|
$this->typoTokensThreshold = Arr::get($config, "typesense_options.typo_tokens_threshold") ?? $this->maxResultsPerPage();
|
|
|
|
$this->dropTokensThreshold = Arr::get($config, "typesense_options.drop_tokens_threshold") ?? $this->maxResultsPerPage();
|
|
|
|
$this->searchCutOffMs = Arr::get($config, "typesense_options.search_cutoff_ms", 450);
|
2023-01-24 23:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cacheTtlForEndpoint(string $endpoint): ?int
|
|
|
|
{
|
|
|
|
return collect($this->perEndpointCacheTtl)->get($endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function defaultCacheExpire(): int
|
|
|
|
{
|
|
|
|
return $this->defaultCacheExpire;
|
|
|
|
}
|
2023-02-02 23:37:37 +00:00
|
|
|
|
|
|
|
public function isMicroCachingEnabled(): bool
|
|
|
|
{
|
|
|
|
return $this->microCachingEnabled;
|
|
|
|
}
|
2023-06-26 18:42:51 +01:00
|
|
|
|
|
|
|
public function maxResultsPerPage(?int $defaultValue = null): int
|
|
|
|
{
|
2023-07-15 14:44:42 +01:00
|
|
|
return (int) $this->config->get("max_results_per_page", $defaultValue ?? 25);
|
2023-06-26 18:42:51 +01:00
|
|
|
}
|
2023-07-09 10:50:18 +01:00
|
|
|
|
|
|
|
public function textMatchBuckets(): int
|
|
|
|
{
|
|
|
|
return $this->textMatchBuckets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function typoTokensThreshold(): int
|
|
|
|
{
|
|
|
|
return $this->typoTokensThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dropTokensThreshold(): int
|
|
|
|
{
|
|
|
|
return $this->dropTokensThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function exhaustiveSearch(): string
|
|
|
|
{
|
|
|
|
$normalizedValue = strtolower($this->exhaustiveSearch);
|
|
|
|
return match($this->exhaustiveSearch) {
|
|
|
|
"0", "FALSE" => "false",
|
|
|
|
"1", "TRUE" => "true",
|
|
|
|
default => in_array($normalizedValue, ["true", "false"]) ? $normalizedValue : "false"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public function searchCutOffMs(): int
|
|
|
|
{
|
|
|
|
return $this->searchCutOffMs;
|
|
|
|
}
|
2023-01-24 23:30:47 +00:00
|
|
|
}
|