summaryrefslogtreecommitdiff
path: root/app/Rugby/Concerns/Matchable.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Rugby/Concerns/Matchable.php')
-rw-r--r--app/Rugby/Concerns/Matchable.php93
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;
+ }
+}