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.php71
1 files changed, 66 insertions, 5 deletions
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}/"