'date', 'valuta' => 'date', 'importo' => 'decimal:2', 'da_confermare' => 'boolean', 'match_data' => 'array', ]; public function getDescrizioneEstesaPulitaAttribute(): ?string { $raw = $this->descrizione_estesa ?? $this->descrizione; if (! is_string($raw)) { return null; } $text = trim($raw); if ($text === '') { return null; } if (is_array($this->match_data ?? null) && ! empty($this->match_data['descrizione_base'])) { $base = trim((string) $this->match_data['descrizione_base']); if ($base !== '') { $text = $base . ' ' . $text; } } $text = $this->applyCleaningRules($text); $text = preg_replace('/\s{2,}/', ' ', $text) ?? $text; $text = trim($text, " \t\n\r\0\x0B|-"); return $text !== '' ? $text : null; } protected function applyCleaningRules(string $text): string { $patterns = []; $config = self::loadCleaningConfig(); if (isset($config['base']['remove_patterns']) && is_array($config['base']['remove_patterns'])) { $patterns = array_merge($patterns, $config['base']['remove_patterns']); } $codice = $this->resolveCodiceBreve(); if ($codice && isset($config['codici'][$codice]['remove_patterns']) && is_array($config['codici'][$codice]['remove_patterns'])) { $patterns = array_merge($patterns, $config['codici'][$codice]['remove_patterns']); } foreach ($patterns as $pattern) { if (! is_string($pattern) || $pattern === '') { continue; } $text = preg_replace('/' . $pattern . '/i', '', $text) ?? $text; } return $text; } protected function resolveCodiceBreve(): ?string { $raw = strtoupper((string) ($this->descrizione ?? '')); if (str_contains($raw, 'ACCREDITO_BEU_CON_CONTABILE')) { return 'ACCBEUCONCON'; } if (str_contains($raw, 'COMBONMYB')) { return 'COMBONMYB'; } return null; } protected static function loadCleaningConfig(): array { if (self::$cleaningConfigCache !== null) { return self::$cleaningConfigCache; } $path = base_path('resources/bank-cleaning.json'); if (! is_file($path)) { return self::$cleaningConfigCache = []; } $json = file_get_contents($path); $data = is_string($json) ? json_decode($json, true) : null; if (! is_array($data)) { return self::$cleaningConfigCache = []; } return self::$cleaningConfigCache = $data; } public function registrazione() { return $this->belongsTo(Registrazione::class, 'registrazione_id'); } public function conto() { return $this->belongsTo(DatiBancari::class, 'conto_id'); } public function gestioneContabile() { return $this->belongsTo(GestioneContabile::class, 'gestione_id'); } }