From 9cd0234665ea66dff172d94b9c1b4cb61b1d25b1 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 21 Jun 2020 17:58:23 +0100 Subject: Improve robustness of all scripts and add more to sync command --- app/Rugby/Concerns/Matchable.php | 71 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) (limited to 'app/Rugby/Concerns') diff --git a/app/Rugby/Concerns/Matchable.php b/app/Rugby/Concerns/Matchable.php index b0c512c..2d2f02b 100644 --- a/app/Rugby/Concerns/Matchable.php +++ b/app/Rugby/Concerns/Matchable.php @@ -6,7 +6,7 @@ trait Matchable { public function matchableFilters() { - + dd("You must implement matchableFilters!"); } public function isMatch(string $search): bool @@ -28,7 +28,7 @@ trait Matchable return true; } - public function matchString(string $needle, string $type, string $hay): bool + protected function matchString(string $needle, string $type, string $hay): bool { if ($type == 'date') { if (!$this->isDate($needle)) { @@ -40,12 +40,22 @@ trait Matchable }; 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; } - public function matchArray(string $needle, array $haystack, string $type): bool + protected function matchArray(string $needle, array $haystack, string $type): bool { foreach ($haystack as $hay) { $result = $this->matchString($needle, $type, $hay); @@ -58,7 +68,7 @@ trait Matchable return false; } - public function isDate(string $value): bool + protected function isDate(string $value): bool { $patterns = [ "/\d{2}\-\d{2}\-\d{4}/", @@ -76,7 +86,58 @@ trait Matchable return false; } - public function getYear(string $value): string + 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}/" -- cgit v1.2.3