From 8d6e8b306d7836e4075a13ad98617bfe5afaa1a0 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sat, 20 Jun 2020 21:07:45 +0100 Subject: Add new scripts for youtube downloading and syncing with existing matches --- app/Rugby/Concerns/Matchable.php | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 app/Rugby/Concerns/Matchable.php (limited to 'app/Rugby/Concerns/Matchable.php') diff --git a/app/Rugby/Concerns/Matchable.php b/app/Rugby/Concerns/Matchable.php new file mode 100644 index 0000000..b0c512c --- /dev/null +++ b/app/Rugby/Concerns/Matchable.php @@ -0,0 +1,93 @@ +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; + } +} -- cgit v1.2.3