matchableFilters(); if (is_array($filters)) { $filters = collect($filters); } foreach ($filters as $filter) { $match = $this->matchArray($filter[1], explode(' ', $search), $filter[0]); if (!$match) { return false; } } return true; } public function matchString(string $needle, string $type, string $hay): bool { if ($type == 'date') { if (!$this->isDate($needle)) { dd('Matchable `' . $hay . '` is not a date'); }; if (!$this->isDate($hay)) { return false; }; return $this->getYear($hay) == $this->getYear($needle); } return $hay == $needle; } public function matchArray(string $needle, array $haystack, string $type): bool { foreach ($haystack as $hay) { $result = $this->matchString($needle, $type, $hay); if ($result) { return true; } } return false; } public function isDate(string $value): bool { $patterns = [ "/\d{2}\-\d{2}\-\d{4}/", "/\d{2}\_\d{2}\_\d{4}/", "/\d{2}\/\d{2}\/\d{4}/", "/\d{4}/", ]; foreach ($patterns as $pattern) { if (preg_match($pattern, $value, $matches)) { return true; } } return false; } public function getYear(string $value): string { $patterns = [ "/\d{4}/" ]; foreach ($patterns as $pattern) { if (preg_match($pattern, $value, $matches)) { return $matches[0]; } } return false; } }