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; } protected 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); } elseif ($type == 'number') { if (!$this->isNumeric($needle)) { dd('Matchable `' . $hay . '` is not numeric'); }; if (!$this->isNumeric($hay)) { return false; } return $this->getNumeric($hay) == $this->getNumeric($needle); } return $hay == $needle; } protected 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; } protected 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; } protected function isNumeric(string $value): bool { $value = strtolower($value); if (is_numeric($value)) { return true; } if (array_key_exists($value, $this->getNumberLookup())) { return true; } return false; } protected function getNumberLookup(): array { return [ 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5, 'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9, 'ten' => 10, ]; } protected function getNumeric(string $value): int { $value = strtolower($value); $numbers = $this->getNumberLookup(); if (is_numeric($value)) { return $value; } if (array_key_exists($value, $numbers)) { return $numbers[$value]; } return false; } protected function getYear(string $value): string { $patterns = [ "/\d{4}/" ]; foreach ($patterns as $pattern) { if (preg_match($pattern, $value, $matches)) { return $matches[0]; } } return false; } }