Files
pricebuddy/app/Services/StrategyExtractor.php
Jeremy 0d685c1cc3 feat: AI self-healing and microdata fallback extraction
Improve meta extraction with AI self-healing and HTML microdata support.

- AI self-heal the existing-store and auto-create extraction paths when no price
  is found, returning the detected/updated store (AiConfigHealer::applyPreviewToStore)
- Expose scrape_settings and settings on StoreResource
- Microdata (itemprop / <data>) fallback extraction across SchemaOrgService,
  StrategyExtractor and AutoCreateStore, with JSON-LD precedence locked by a test
- Use filled() for the price heal-gate; parseSchemaOrg returns ?string for
  ImageObject arrays
2026-06-18 23:13:06 +10:00

55 lines
1.6 KiB
PHP

<?php
namespace App\Services;
use App\Dto\StandardStrategyDto;
use App\Enums\ScraperStrategyType;
use Jez500\WebScraperForLaravel\WebScraperInterface;
class StrategyExtractor
{
/**
* Apply a single scrape-strategy slot to an already-loaded scraper and
* return the extracted string (with prepend/append), or null when the core
* extraction yields nothing (null or empty string).
*
* May throw Jez500\WebScraperForLaravel\Exceptions\DomSelectorException on an
* invalid selector/xpath; callers decide whether to swallow or surface it.
*/
public static function extract(WebScraperInterface $scraper, StandardStrategyDto $slot, string $field): ?string
{
$type = $slot->type;
if ($type === ScraperStrategyType::SchemaOrg) {
return SchemaOrgService::parseSchemaOrg($scraper->getSchemaOrg(), $field)
?? SchemaOrgService::parseMicrodata($scraper->getBody(), $field);
}
$value = $slot->value;
if ($value === null) {
return null;
}
$method = ScrapeUrl::getMethodFromType($type->value);
$args = match ($type) {
ScraperStrategyType::Selector => ScrapeUrl::parseSelector($value),
ScraperStrategyType::Regex => [ScrapeUrl::ensureRegexDelimiters($value)],
default => [$value],
};
$extracted = call_user_func_array([$scraper, $method], $args)?->first();
if ($extracted === null || $extracted === '') {
return null;
}
return implode('', [
$slot->prepend ?? '',
$extracted,
$slot->append ?? '',
]);
}
}