diff options
| author | Fbenas <philbeansburton@gmail.com> | 2020-06-20 21:07:45 +0100 | 
|---|---|---|
| committer | Fbenas <philbeansburton@gmail.com> | 2020-06-20 21:07:45 +0100 | 
| commit | 8d6e8b306d7836e4075a13ad98617bfe5afaa1a0 (patch) | |
| tree | 99c44e28da231cac0d9e513aabc1ee0768972426 /app/Rugby/Concerns/Matchable.php | |
| parent | 897b68ac107f2fb0d4dc1d31ce96ce24c862eb27 (diff) | |
Add new scripts for youtube downloading and syncing with existing matches
Diffstat (limited to 'app/Rugby/Concerns/Matchable.php')
| -rw-r--r-- | app/Rugby/Concerns/Matchable.php | 93 | 
1 files changed, 93 insertions, 0 deletions
| 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 @@ +<?php + +namespace App\Rugby\Concerns; + +trait Matchable +{ +    public function matchableFilters() +    { + +    } + +    public function isMatch(string $search): bool +    { +        $filters = $this->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; +    } +} | 
