Files
2026-11-26 16:18:59 +02:00

34 lines
971 B
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Services;
/**
* Matches stock items to a search typed on a keyboard or touch screen, or
* read by a barcode scanner: every word of the search must be found in the
* name or the code, regardless of case and of the dots of å, ä and ö
* (spec 19.6).
*/
final class StockSearch
{
public static function matches(string $search, string $name, ?string $code): bool
{
$words = preg_split('/\s+/u', self::normalize($search), -1, PREG_SPLIT_NO_EMPTY) ?: [];
$haystack = self::normalize($name.' '.($code ?? ''));
foreach ($words as $word) {
if (! str_contains($haystack, $word)) {
return false;
}
}
return true;
}
public static function normalize(string $value): string
{
return strtr(mb_strtolower(trim($value)), ['å' => 'a', 'ä' => 'a', 'ö' => 'o', 'é' => 'e', 'ü' => 'u']);
}
}