added ons
This commit is contained in:
@@ -54,6 +54,11 @@ class ZaarboxPriceService
|
||||
return $url !== '' ? $url : 'https://price.zaarbox.ir/api/free';
|
||||
}
|
||||
|
||||
public function hasApiUrlConfigured(): bool
|
||||
{
|
||||
return trim($this->config->getString('zaarbox_api_url', '')) !== '';
|
||||
}
|
||||
|
||||
/** کلید یا id ردیف در JSON تابلو (settings: zaarbox_price_key) */
|
||||
public function priceIdentifier(): string
|
||||
{
|
||||
@@ -190,6 +195,67 @@ class ZaarboxPriceService
|
||||
return $prices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $prices
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function findOunceFeedRow(array $prices): ?array
|
||||
{
|
||||
$row = collect($prices)
|
||||
->map(fn ($item) => is_array($item) ? $item : (array) $item)
|
||||
->filter(function (array $item) {
|
||||
$name = trim((string) ($item['name'] ?? ''));
|
||||
|
||||
return $name !== '' && mb_strpos($name, 'انس') !== false;
|
||||
})
|
||||
->sortBy([
|
||||
fn (array $item) => (bool) ($item['active'] ?? false) ? 0 : 1,
|
||||
fn (array $item) => str_contains(trim((string) ($item['name'] ?? '')), 'انس طلا') ? 0 : 1,
|
||||
fn (array $item) => mb_strlen(trim((string) ($item['name'] ?? ''))),
|
||||
])
|
||||
->first();
|
||||
|
||||
return is_array($row) ? $row : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{price: int, name: string, raw: array<string, mixed>|null}
|
||||
*/
|
||||
public function fetchOuncePrice(): array
|
||||
{
|
||||
$empty = ['price' => 0, 'name' => '', 'raw' => null];
|
||||
if (! $this->hasApiUrlConfigured()) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
try {
|
||||
$prices = $this->cachedPriceList(false) ?? $this->fetchPriceList();
|
||||
if (! is_array($prices)) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
$row = $this->findOunceFeedRow($prices);
|
||||
if ($row === null) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
$price = $this->resolveReferenceProviderTotal($row);
|
||||
if ($price <= 0) {
|
||||
return $empty;
|
||||
}
|
||||
|
||||
return [
|
||||
'price' => $price,
|
||||
'name' => trim((string) ($row['name'] ?? 'انس طلا')),
|
||||
'raw' => $row,
|
||||
];
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('Zaarbox ounce price fetch failed', ['message' => $e->getMessage()]);
|
||||
|
||||
return $empty;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{price: int, order_price: int, raw: array<string, mixed>|null}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user