From d1fe4536cc039450b540104e382db0d01d5cf886 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Fri, 19 Jun 2020 02:16:51 +0100 Subject: Initial commit --- app/Rugby/Factory/DataAdapter.php | 8 +++ app/Rugby/Factory/Service.php | 71 +++++++++++++++++++ app/Rugby/Factory/SixnationsrugbyAdapter.php | 100 +++++++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 app/Rugby/Factory/DataAdapter.php create mode 100644 app/Rugby/Factory/Service.php create mode 100644 app/Rugby/Factory/SixnationsrugbyAdapter.php (limited to 'app/Rugby/Factory') diff --git a/app/Rugby/Factory/DataAdapter.php b/app/Rugby/Factory/DataAdapter.php new file mode 100644 index 0000000..b4971ce --- /dev/null +++ b/app/Rugby/Factory/DataAdapter.php @@ -0,0 +1,8 @@ +data_adapter = $data_adapter; + } + + public function save() + { + $tournament = Model\Tournament::where(['name' => $this->data_adapter->getTournamentName()])->first(); + + if (!$tournament) { + $tournament = Model\Tournament::create(['name' => $this->data_adapter->getTournamentName()]); + } + + foreach ($this->data_adapter->getData() as $match_data) { + $this->processMatch($match_data, $tournament); + } + } + + protected function processMatch(array $data, Model\Tournament $tournament) + { + $venue = Model\Venue::where('name', '=', $data['venue_name'])->first(); + + if (!$venue) { + $venue = Model\Venue::create( + [ + 'city' => $data['venue_city'], + 'name' => $data['venue_name'] + ] + ); + } + + $home_team = Model\Team::where('name', '=', $data['team_home'])->first(); + + if (!$home_team) { + $home_team = Model\Team::Create(['name' => $data['team_home']]); + } + + $away_team = Model\Team::where('name', '=', $data['team_away'])->first(); + + if (!$away_team) { + $away_team = Model\Team::Create(['name' => $data['team_away']]); + } + + $match = Model\Match::create( + [ + 'date' => (new \Carbon\Carbon($data['match_date']))->format('Y-m-d H:i:s'), + 'score' => $data['match_score'], + 'half_score' => $data['match_half_score'], + 'referee' => $data['match_referee'] ?? null, + ] + ); + + $match->venue_id = $venue->id; + $match->save(); + + $match->tournaments()->save($tournament); + $match->teams()->attach($home_team, ['is_home' => '1']); + $match->teams()->attach($away_team, ['is_home' => '0']); + } +} diff --git a/app/Rugby/Factory/SixnationsrugbyAdapter.php b/app/Rugby/Factory/SixnationsrugbyAdapter.php new file mode 100644 index 0000000..6b6edd7 --- /dev/null +++ b/app/Rugby/Factory/SixnationsrugbyAdapter.php @@ -0,0 +1,100 @@ +tournament_name = $tournament_name; + $this->data = $this->processData($this->cleanData($data)); + } + + public function getData() + { + return $this->data; + } + + + public function getTournamentName() + { + return $this->tournament_name; + } + + + protected function cleanData($data) + { + // string + $data = str_replace("Round 2", "", $data); + $data = str_replace("Round 3", "", $data); + $data = str_replace("Round 4", "", $data); + $data = str_replace("Round 5", "", $data); + $data = str_replace("BBC logo", "", $data); + $data = str_replace("ITV logo", "", $data); + $data = str_replace("S4C logo", "", $data); + $data = str_replace("Preview", "\na\nb\nc", $data); + $data = str_replace("Match Centre", "\na\nb\nc", $data); + + // array + $data = explode('Round 1', $data)[1]; + $data = explode('Store', $data)[0]; + $data = ltrim(str_replace("\n\n", "\n", $data), "\n"); + $data = explode("\n", $data); + + return $data; + } + + protected function processData($data) + { + $count = count($data) - 1; + + $processed_data = []; + + for ($i = 0; $i < $count; $i+= 10) { + $raw_match_data = array_slice($data, $i, 10); + + $match_data = [ + 'match_date' => $raw_match_data[0], + 'team_home' => $raw_match_data[1], + 'team_away' => $raw_match_data[4] + ]; + + $score = explode(' HT: ', $raw_match_data[3]); + + $match_data['match_score'] = ($score[0] ?? null); + $match_data['match_half_score'] = ($score[1] ?? null); + $match_info = explode(' Ref: ', $raw_match_data[6]); + + if (isset($match_info[0])) { + $venue_info = explode(', ', $match_info[0]); + + $match_data['venue_name'] = ($venue_info[0] ?? null); + $match_data['venue_city'] = ($venue_info[1] ?? null); + } + + $match_data['match_referee'] = ($match_info[1] ?? null); + + $processed_data[] = $match_data; + } + + return $processed_data; + } + + public function getRef($raw) + { + $ref = explode(' Ref:', $raw[6]); + + if (isset($ref[1])) { + return $ref[1]; + } + + return null; + } +} -- cgit v1.2.3