*/ private const COPY_FIELDS = [ 'memo', 'invoice', 'category', 'feeMemo', 'dAcct', 'dSub', 'cAcct', 'cSub', 'basis', ]; /** @var list */ private const REQUIRED_FIELDS = ['category', 'dAcct', 'cAcct', 'basis']; public function __construct( private readonly AccountMappingStore $store, ) { } /** * @return array{ * auto_fill:bool, * reason:string, * matched_count:int, * sources:list>, * values:array * } */ public function suggest(string $platform, string $item): array { return $this->suggestFromRules($this->store->all(), $platform, $item); } /** * @param array> $rules * @return array{ * auto_fill:bool, * reason:string, * matched_count:int, * sources:list>, * values:array * } */ public function suggestFromRules(array $rules, string $platform, string $item): array { $platform = trim($platform); $item = trim($item); $itemKey = $this->normalizeItem($item); if ($itemKey === '') { return $this->emptyResult('invalid_item'); } $matches = []; foreach ($rules as $rule) { if (! is_array($rule) || $this->normalizeItem((string) ($rule['item'] ?? '')) !== $itemKey) { continue; } $matches[] = $rule; } if ($matches === []) { return $this->emptyResult('no_same_item'); } usort($matches, static fn (array $left, array $right): int => ( ((string) ($left['platform'] ?? '') === $platform ? 0 : 1) <=> ((string) ($right['platform'] ?? '') === $platform ? 0 : 1) ) ?: (((int) ($left['id'] ?? 0)) <=> ((int) ($right['id'] ?? 0)))); $templates = []; foreach ($matches as $rule) { $templates[$this->templateSignature($rule)] = true; } $sources = array_map( static fn (array $rule): array => [ 'id' => (int) ($rule['id'] ?? 0), 'platform' => trim((string) ($rule['platform'] ?? '')), 'item' => trim((string) ($rule['item'] ?? '')), 'memo' => trim((string) ($rule['memo'] ?? '')), ], array_slice($matches, 0, 8), ); $base = [ 'matched_count' => count($matches), 'sources' => $sources, 'values' => [], ]; if (count($templates) > 1) { return ['auto_fill' => false, 'reason' => 'ambiguous_templates'] + $base; } $template = $matches[0]; foreach (self::REQUIRED_FIELDS as $field) { if (trim((string) ($template[$field] ?? '')) === '') { return ['auto_fill' => false, 'reason' => 'incomplete_template'] + $base; } } $values = []; foreach (self::COPY_FIELDS as $field) { $values[$field] = trim((string) ($template[$field] ?? '')); } return [ 'auto_fill' => true, 'reason' => 'same_item', ] + array_replace($base, ['values' => $values]); } public function normalizeItem(string $item): string { $item = mb_strtolower(trim($item)); $item = preg_replace('/^wx/u', '', $item) ?? $item; return preg_replace('/[\s\x{3000}\p{P}\p{S}]+/u', '', $item) ?? $item; } /** @param array $rule */ private function templateSignature(array $rule): string { $values = []; foreach (self::COPY_FIELDS as $field) { $values[$field] = trim((string) ($rule[$field] ?? '')); } return json_encode($values, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR); } /** * @return array{ * auto_fill:bool, * reason:string, * matched_count:int, * sources:list>, * values:array * } */ private function emptyResult(string $reason): array { return [ 'auto_fill' => false, 'reason' => $reason, 'matched_count' => 0, 'sources' => [], 'values' => [], ]; } }