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 ++++++++++++++++++++++++++-- app/Rugby/Factory/DataAdapter.php | 1 - app/Rugby/Factory/Service.php | 21 ++++++++ app/Rugby/Factory/SixnationsrugbyAdapter.php | 1 - app/Rugby/Model/Match.php | 9 ++-- app/Rugby/Model/Tournament.php | 5 +- app/Rugby/Model/Video.php | 5 +- 7 files changed, 96 insertions(+), 17 deletions(-) (limited to 'app/Rugby') 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}/" diff --git a/app/Rugby/Factory/DataAdapter.php b/app/Rugby/Factory/DataAdapter.php index b4971ce..d0434c9 100644 --- a/app/Rugby/Factory/DataAdapter.php +++ b/app/Rugby/Factory/DataAdapter.php @@ -4,5 +4,4 @@ namespace App\Rugby\Factory; interface DataAdapter { - } diff --git a/app/Rugby/Factory/Service.php b/app/Rugby/Factory/Service.php index fa8028f..e966ce6 100644 --- a/app/Rugby/Factory/Service.php +++ b/app/Rugby/Factory/Service.php @@ -52,6 +52,27 @@ class Service $away_team = Model\Team::Create(['name' => $data['team_away']]); } + $existing_match = Model\Match::whereHas( + 'teams', + function ($query) use ($home_team) { + $query->where('name', '=', $home_team->name); + } + )->whereHas( + 'teams', + function ($query) use ($away_team) { + $query->where('name', '=', $away_team->name); + } + )->whereHas( + 'tournaments', + function ($query) use ($tournament) { + $query->where('name', '=', $tournament->name); + } + )->get(); + + if ($existing_match->first()) { + return; + } + $match = Model\Match::create( [ 'date' => (new \Carbon\Carbon($data['match_date']))->format('Y-m-d H:i:s'), diff --git a/app/Rugby/Factory/SixnationsrugbyAdapter.php b/app/Rugby/Factory/SixnationsrugbyAdapter.php index 6b6edd7..4a36602 100644 --- a/app/Rugby/Factory/SixnationsrugbyAdapter.php +++ b/app/Rugby/Factory/SixnationsrugbyAdapter.php @@ -28,7 +28,6 @@ class SixnationsrugbyAdapter implements DataAdapter return $this->tournament_name; } - protected function cleanData($data) { // string diff --git a/app/Rugby/Model/Match.php b/app/Rugby/Model/Match.php index 58a3dd9..38d58d8 100644 --- a/app/Rugby/Model/Match.php +++ b/app/Rugby/Model/Match.php @@ -47,14 +47,14 @@ class Match extends Model return $video->getUrl(); } - public function homeTeam() + public function getHomeTeam() { - return $this->teams()->wherePivot('is_home', '=', true); + return $this->teams()->wherePivot('is_home', '=', true)->first(); } - public function awayTeam() + public function getAwayTeam() { - return $this->teams()->wherePivot('is_home', '=', false); + return $this->teams()->wherePivot('is_home', '=', false)->first(); } public function tournaments() @@ -66,5 +66,4 @@ class Match extends Model { return $this->hasMany(Video::class, 'match_id'); } - } diff --git a/app/Rugby/Model/Tournament.php b/app/Rugby/Model/Tournament.php index 3ab6352..5c81278 100644 --- a/app/Rugby/Model/Tournament.php +++ b/app/Rugby/Model/Tournament.php @@ -12,7 +12,6 @@ class Tournament extends Model use Matchable; protected $table = 'tournaments'; - protected $fillable = ['name']; public function matchableFilters() @@ -25,6 +24,10 @@ class Tournament extends Model return ['date', $value]; } + if ($this->isNumeric($value)) { + return ['number', $value]; + } + return ['string', $value]; } ); diff --git a/app/Rugby/Model/Video.php b/app/Rugby/Model/Video.php index 2182b65..48da15e 100644 --- a/app/Rugby/Model/Video.php +++ b/app/Rugby/Model/Video.php @@ -10,7 +10,7 @@ class Video extends Model { protected $table = 'videos'; protected $casts = ['date' => 'datetime:Y-m-d']; - protected $fillable = ['path']; + protected $fillable = ['path', 'url']; public function match() { @@ -27,8 +27,5 @@ class Video extends Model public function getUrl(): string { return asset('storage/matches/' . $this->getFilename()); - // return Storage::disk('local')->url( - // 'matches/' . $this->getFilename() - // ); } } -- cgit v1.2.3