mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Dto\Insights;
|
|
|
|
use App\Enums\StockStatus;
|
|
|
|
final class AvailabilityData
|
|
{
|
|
/**
|
|
* @param array<int, array{available: bool, days: int}> $segments
|
|
*/
|
|
public function __construct(
|
|
public readonly string $storeName,
|
|
public readonly float $inStockPercent,
|
|
public readonly StockStatus $currentStatus,
|
|
public readonly array $segments,
|
|
) {}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'storeName' => $this->storeName,
|
|
'inStockPercent' => $this->inStockPercent,
|
|
'currentStatus' => $this->currentStatus->value,
|
|
'segments' => $this->segments,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public static function fromArray(array $data): self
|
|
{
|
|
$segments = array_map(static fn (array $segment): array => [
|
|
'available' => (bool) $segment['available'],
|
|
'days' => (int) $segment['days'],
|
|
], $data['segments']);
|
|
|
|
return new self(
|
|
storeName: (string) $data['storeName'],
|
|
inStockPercent: (float) $data['inStockPercent'],
|
|
currentStatus: StockStatus::from($data['currentStatus']),
|
|
segments: $segments,
|
|
);
|
|
}
|
|
}
|