mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
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
72 lines
2.0 KiB
PHP
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();
|
|
}
|
|
}
|