summaryrefslogtreecommitdiff
path: root/app/Rugby/Concerns/Matchable.php
blob: 2d2f02b9e7e0dfb603d44afd687818492a000d4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

namespace App\Rugby\Concerns;

trait Matchable
{
    public function matchableFilters()
    {
        dd("You must implement 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;
    }

    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;
    }
}