Files
pricebuddy/app/Dto/StoreScraperStrategySetDto.php
Jeremy 1a857e1582 feat: meta extraction API and typed scrape-strategy DTOs
Add a meta-extraction API endpoint and refactor store scrape strategies onto
strongly-typed DTOs.

- POST /api/meta-extraction endpoint with SSRF protection on the target URL
- Typed scrape-strategy DTOs and enums (StandardStrategyDto, AvailabilityMatchDto,
  AvailabilityMatchType, AvailabilityStrategyDto, StoreScraperStrategySetDto)
  with a model cast for the stores.scrape_strategy column
- Convert ScrapeUrl, StrategyExtractor and availability resolution to consume the
  typed strategy slots; preserve legacy plain-string availability match entries
- Filament scrape_strategy form bridge plus DTO test coverage
2026-06-18 23:13:01 +10:00

72 lines
2.0 KiB
PHP

<?php
namespace App\Dto;
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;
/**
* The full set of scrape strategies stored on a Store's scrape_strategy column.
*
* @implements Arrayable<string, array<string, mixed>>
*/
class StoreScraperStrategySetDto implements Arrayable, JsonSerializable
{
public function __construct(
public ?StandardStrategyDto $title = null,
public ?StandardStrategyDto $price = null,
public ?StandardStrategyDto $image = null,
public ?StandardStrategyDto $description = null,
public ?AvailabilityStrategyDto $availability = null,
) {}
/**
* @param array<string, mixed>|null $data
*/
public static function fromArray(?array $data): self
{
$data ??= [];
return new self(
title: StandardStrategyDto::fromArray(self::slot($data, 'title')),
price: StandardStrategyDto::fromArray(self::slot($data, 'price')),
image: StandardStrategyDto::fromArray(self::slot($data, 'image')),
description: StandardStrategyDto::fromArray(self::slot($data, 'description')),
availability: AvailabilityStrategyDto::fromArray(self::slot($data, 'availability')),
);
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>|null
*/
protected static function slot(array $data, string $key): ?array
{
return is_array($data[$key] ?? null) ? $data[$key] : null;
}
/**
* @return array<string, array<string, mixed>>
*/
public function toArray(): array
{
$out = [];
foreach (['title', 'price', 'image', 'description', 'availability'] as $key) {
if ($this->{$key} !== null) {
$out[$key] = $this->{$key}->toArray();
}
}
return $out;
}
/**
* @return array<string, array<string, mixed>>
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}