diff options
| author | Fbenas <philbeansburton@gmail.com> | 2020-06-21 17:58:23 +0100 | 
|---|---|---|
| committer | Fbenas <philbeansburton@gmail.com> | 2020-06-21 17:58:23 +0100 | 
| commit | 9cd0234665ea66dff172d94b9c1b4cb61b1d25b1 (patch) | |
| tree | 9cb5d13acbf3c5bb8c842620ceb8a104b41babb7 /app/Rugby/Concerns | |
| parent | 8d6e8b306d7836e4075a13ad98617bfe5afaa1a0 (diff) | |
Diffstat (limited to 'app/Rugby/Concerns')
| -rw-r--r-- | app/Rugby/Concerns/Matchable.php | 71 | 
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}/" | 
