mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
117 lines
3.1 KiB
PHP
117 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Helpers;
|
|
|
|
use App\Dto\Scraping\ScrapeSchemaDto;
|
|
use App\Enums\ScraperStrategyType;
|
|
use App\Exceptions\ScrapeSchemaValidationException;
|
|
use App\Services\ScrapeSchemaValidator;
|
|
|
|
class ScrapeStrategyHelper
|
|
{
|
|
/**
|
|
* Build a ScrapeSchemaDto from a raw strategy array.
|
|
*
|
|
* Excludes schema_org fields (handled separately by SchemaOrgService)
|
|
* and strips availability match config (incompatible with MatchDefinitionDto).
|
|
*/
|
|
public static function toSchema(array $strategy): ScrapeSchemaDto
|
|
{
|
|
$fields = self::normalizeForExtraction(
|
|
self::extractDtoCompatibleFields($strategy)
|
|
);
|
|
|
|
if ($fields === []) {
|
|
return new ScrapeSchemaDto;
|
|
}
|
|
|
|
try {
|
|
return (new ScrapeSchemaValidator)->validate($fields);
|
|
} catch (ScrapeSchemaValidationException) {
|
|
return new ScrapeSchemaDto;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Extract the availability match config from the raw strategy.
|
|
*
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public static function getAvailabilityMatch(array $strategy): ?array
|
|
{
|
|
$match = $strategy['availability']['match'] ?? null;
|
|
|
|
return is_array($match) ? $match : null;
|
|
}
|
|
|
|
/**
|
|
* Normalize field definitions for package consumption:
|
|
* map 'selector' to 'css' and strip 'match' keys.
|
|
*
|
|
* @param array<string, array<string, mixed>> $fields
|
|
* @return array<string, array<string, mixed>>
|
|
*/
|
|
public static function normalizeForExtraction(array $fields): array
|
|
{
|
|
return array_map(function (array $field) {
|
|
if (($field['type'] ?? '') === ScraperStrategyType::Selector->value) {
|
|
$field['type'] = 'css';
|
|
}
|
|
|
|
unset($field['match']);
|
|
|
|
return $field;
|
|
}, $fields);
|
|
}
|
|
|
|
/**
|
|
* Validate a raw strategy array by attempting DTO hydration.
|
|
*
|
|
* @return true|array<int, string>
|
|
*/
|
|
public static function validate(array $strategy): true|array
|
|
{
|
|
$fields = self::normalizeForExtraction(
|
|
self::extractDtoCompatibleFields($strategy)
|
|
);
|
|
|
|
if ($fields === []) {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
(new ScrapeSchemaValidator)->validate($fields);
|
|
|
|
return true;
|
|
} catch (ScrapeSchemaValidationException $e) {
|
|
return $e->errors();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Extract fields that are compatible with DTO hydration.
|
|
*
|
|
* Skips empty/non-array entries and schema_org fields (which bypass the DTO pipeline).
|
|
*
|
|
* @return array<string, array<string, mixed>>
|
|
*/
|
|
protected static function extractDtoCompatibleFields(array $strategy): array
|
|
{
|
|
$fields = [];
|
|
|
|
foreach ($strategy as $key => $definition) {
|
|
if (empty($definition) || ! is_array($definition)) {
|
|
continue;
|
|
}
|
|
|
|
if (($definition['type'] ?? '') === ScraperStrategyType::SchemaOrg->value) {
|
|
continue;
|
|
}
|
|
|
|
$fields[$key] = $definition;
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
}
|