1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFLScores\Utilities; |
4
|
|
|
|
5
|
|
|
use LaravelZero\Framework\Commands\Command; |
6
|
|
|
use NFLScores\Models\Game; |
7
|
|
|
use PHPCollections\Collections\GenericList; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Utility class for printing |
11
|
|
|
* elements into the console. |
12
|
|
|
*/ |
13
|
|
|
class Printer |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The command which will be execute. |
17
|
|
|
* |
18
|
|
|
* @var \LaravelZero\Framework\Commands\Command |
19
|
|
|
*/ |
20
|
|
|
private $command; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initialize values. |
24
|
|
|
* |
25
|
|
|
* @param \LaravelZero\Framework\Commands\Command |
26
|
|
|
*/ |
27
|
8 |
|
public function __construct(Command $command) |
28
|
|
|
{ |
29
|
8 |
|
$this->command = $command; |
30
|
8 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return an array with the name and |
34
|
|
|
* score of a specific team. |
35
|
|
|
* |
36
|
|
|
* @param string $abbr |
37
|
|
|
* @param array $score |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
6 |
|
private function buildScoreBoardRow(string $abbr, array $score): array |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
6 |
|
$abbr, |
45
|
6 |
|
$score['pointQ1'], |
46
|
6 |
|
$score['pointQ2'], |
47
|
6 |
|
$score['pointQ3'], |
48
|
6 |
|
$score['pointQ4'], |
49
|
6 |
|
$score['pointOT'], |
50
|
6 |
|
$score['pointTotal'], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Build and print a game' scoreboard |
56
|
|
|
* into the terminal. |
57
|
|
|
* |
58
|
|
|
* @param \NFLScores\Models\Game $game |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
6 |
|
private function printScoreBoard(Game $game): void |
63
|
|
|
{ |
64
|
6 |
|
$this->printScoreBoardHeader($game); |
65
|
|
|
|
66
|
6 |
|
$this->command->table( |
67
|
6 |
|
['', '1', '2', '3', '4', 'OT', 'T'], |
68
|
|
|
[ |
69
|
6 |
|
$this->buildScoreBoardRow($game->gameSchedule['homeTeamAbbr'], $game->score['homeTeamScore']), |
|
|
|
|
70
|
6 |
|
$this->buildScoreBoardRow($game->gameSchedule['visitorTeamAbbr'], $game->score['visitorTeamScore']), |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
|
74
|
6 |
|
$this->command->line("\n"); |
75
|
6 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Build and print the scoreboard header. |
79
|
|
|
* |
80
|
|
|
* @param \NFLScores\Models\Game $game |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
6 |
|
public function printScoreBoardHeader(Game $game) |
85
|
|
|
{ |
86
|
6 |
|
$this->command->line( |
87
|
6 |
|
sprintf( |
88
|
6 |
|
'%s VS %s @ %s', |
89
|
6 |
|
$game->gameSchedule['homeTeamAbbr'], |
|
|
|
|
90
|
6 |
|
$game->gameSchedule['visitorTeamAbbr'], |
91
|
6 |
|
$game->gameSchedule['site']['siteFullname'] |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
|
95
|
6 |
|
if (!$game->isFinished()) { |
96
|
3 |
|
$this->command->line(sprintf( |
97
|
3 |
|
'%s | %s | %s %s', |
98
|
3 |
|
$game->getCurrentQuarter(), |
99
|
3 |
|
$game->score['time'], |
|
|
|
|
100
|
3 |
|
$game->getPossesionTeam(), |
101
|
3 |
|
$game->getCurrentDown() |
102
|
|
|
)); |
103
|
|
|
} |
104
|
6 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Print a table with a list of games. |
108
|
|
|
* |
109
|
|
|
* @param \PHPCollections\Collections\GenericList $gameCollection |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
2 |
|
public function renderGamesList(?GenericList $gameCollection): void |
114
|
|
|
{ |
115
|
2 |
|
$data = []; |
116
|
|
|
|
117
|
|
|
$gameCollection->forEach(function ($game, $key) use (&$data) { |
|
|
|
|
118
|
2 |
|
array_push($data, [ |
119
|
2 |
|
$game->gameSchedule['homeTeamAbbr'], |
120
|
2 |
|
$game->gameSchedule['visitorTeamAbbr'], |
121
|
2 |
|
$game->gameSchedule['site']['siteFullname'], |
122
|
2 |
|
$game->gameSchedule['gameDate'], |
123
|
2 |
|
sprintf('%s ET', $game->gameSchedule['gameTimeEastern']), |
124
|
|
|
]); |
125
|
2 |
|
}); |
126
|
|
|
|
127
|
2 |
|
$this->command->table(['Home', 'Visitor', 'Stadium', 'Date', 'Hour'], $data); |
128
|
2 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Print a collection of game' |
132
|
|
|
* scoreboards into the terminal. |
133
|
|
|
* |
134
|
|
|
* @param PHPCollections\Collections\GenericList |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
6 |
|
public function renderScoreBoard(?GenericList $games): void |
139
|
|
|
{ |
140
|
6 |
|
if (is_null($games)) { |
141
|
|
|
exit($this->command->line('Sorry, there is no games right now.')); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$games->forEach(function ($game, $key) { |
|
|
|
|
145
|
6 |
|
$this->printScoreBoard($game); |
146
|
6 |
|
}); |
147
|
6 |
|
} |
148
|
|
|
} |
149
|
|
|
|