summaryrefslogtreecommitdiff
path: root/app/Rugby/Factory/SixnationsrugbyAdapter.php
blob: 6b6edd761b281e0ed7db5516ff1e9d536b4b063d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace App\Rugby\Factory;

use App\Rugby\Factory\DataAdapter;
use Illuminate\Support\Facades\Storage;

class SixnationsrugbyAdapter implements DataAdapter
{
    protected $data;
    protected $filepath;
    protected $tournament_name;

    public function __construct(string $data, string $tournament_name)
    {
        $this->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;
    }
}