Add stock items, groups and movements at a moving average cost
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user