1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFLScores\Models; |
4
|
|
|
|
5
|
|
|
use NFLScores\Exceptions\NonExistingPropertyException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Represents a NFL game. |
9
|
|
|
*/ |
10
|
|
|
class Game |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The data about the game. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $data; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Populate the $data array. |
21
|
|
|
* |
22
|
|
|
* @param array $data |
23
|
|
|
*/ |
24
|
1 |
|
public function __construct(array $data) |
25
|
|
|
{ |
26
|
1 |
|
$this->data = $data; |
27
|
1 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return the index stored in the $data |
31
|
|
|
* property, if doesn't exist throw an |
32
|
|
|
* exception. |
33
|
|
|
* |
34
|
|
|
* @param string $name |
35
|
|
|
* |
36
|
|
|
* @throws \NFLScores\Exceptions\NonExistingPropertyException |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
9 |
|
public function __get(string $name) |
41
|
|
|
{ |
42
|
9 |
|
if (array_key_exists($name, $this->data)) { |
43
|
9 |
|
return $this->data[$name]; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
throw new NonExistingPropertyException(sprintf('There is not property called %s in this object', $name)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the the current down, |
51
|
|
|
* if the game hasn't started return null. |
52
|
|
|
* |
53
|
|
|
* @return string|null |
54
|
|
|
*/ |
55
|
1 |
|
public function getCurrentDown(): ?string |
56
|
|
|
{ |
57
|
1 |
|
if ($this->score['down'] === 0) { |
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return sprintf( |
62
|
1 |
|
'%d%s & %d', |
63
|
1 |
|
$this->score['down'], |
64
|
1 |
|
getOrdinalSuffix($this->score['down']), |
65
|
1 |
|
$this->score['yardsToGo'] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the team with the current |
71
|
|
|
* ball possesion, if the game hasn't |
72
|
|
|
* started return null. |
73
|
|
|
* |
74
|
|
|
* @return string|null |
75
|
|
|
*/ |
76
|
1 |
|
public function getPossesionTeam(): ?string |
77
|
|
|
{ |
78
|
1 |
|
return !$this->isFinished() ? $this->score['possessionTeamAbbr'] : null; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the the current quarter, |
83
|
|
|
* if the game hasn't started return null. |
84
|
|
|
* |
85
|
|
|
* @return string|null |
86
|
|
|
*/ |
87
|
1 |
|
public function getCurrentQuarter(): ?string |
88
|
|
|
{ |
89
|
1 |
|
return $this->score['phaseDescription']; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return whether the game is |
94
|
|
|
* finished or not. |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
1 |
|
public function isFinished(): bool |
99
|
|
|
{ |
100
|
1 |
|
return in_array($this->score['phase'], ['FINAL', 'FINAL OVERTIME']); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Return whether the game is |
105
|
|
|
* suspended or not. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function isSuspended(): bool |
110
|
|
|
{ |
111
|
|
|
return $this->score['phase'] === 'SUSPENDED'; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|