1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PeterColes\XmlSoccer\Converters\Json; |
4
|
|
|
|
5
|
|
|
class Match |
6
|
|
|
{ |
7
|
|
|
const PLAYERS = [ |
8
|
|
|
'HomeLineupGoalkeeper', |
9
|
|
|
'HomeLineupDefense', |
10
|
|
|
'HomeLineupMidfield', |
11
|
|
|
'HomeLineupForward', |
12
|
|
|
'HomeLineupSubstitutes', |
13
|
|
|
'AwayLineupGoalkeeper', |
14
|
|
|
'AwayLineupDefense', |
15
|
|
|
'AwayLineupMidfield', |
16
|
|
|
'AwayLineupForward', |
17
|
|
|
'AwayLineupSubstitutes' |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
const GOAL_DETAILS = [ |
21
|
|
|
'HomeGoalDetails', |
22
|
|
|
'AwayGoalDetails' |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
const CARDS = [ |
26
|
|
|
'HomeTeamYellowCardDetails', |
27
|
|
|
'HomeTeamRedCardDetails', |
28
|
|
|
'AwayTeamYellowCardDetails', |
29
|
|
|
'AwayTeamRedCardDetails' |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
const SUBSTITUTIONS = [ |
33
|
|
|
'HomeSubDetails', |
34
|
|
|
'AwaySubDetails' |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
const NUMERIC = [ |
38
|
|
|
'Id', |
39
|
|
|
'FixtureMatch_Id', |
40
|
|
|
'Round', |
41
|
|
|
'Spectators', |
42
|
|
|
'HomeTeam_Id', |
43
|
|
|
'HomeGoals', |
44
|
|
|
'HalfTimeHomeGoals', |
45
|
|
|
'HomeShots', |
46
|
|
|
'HomeShotsOnTarget', |
47
|
|
|
'HomeFouls', |
48
|
|
|
'HomeYellowCards', |
49
|
|
|
'HomeRedCards', |
50
|
|
|
'AwayTeam_Id', |
51
|
|
|
'AwayGoals', |
52
|
|
|
'HalfTimeAwayGoals', |
53
|
|
|
'AwayShots', |
54
|
|
|
'AwayShotsOnTarget', |
55
|
|
|
'AwayFouls', |
56
|
|
|
'AwayYellowCards', |
57
|
|
|
'AwayRedCards' |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
public function handle($match) |
61
|
|
|
{ |
62
|
|
|
$object = []; |
63
|
|
|
|
64
|
|
|
foreach ($match as $child) { |
65
|
|
|
$name = $child->getName(); |
66
|
|
|
$object[ $name ] = $this->processAtrribute($name, $child); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $object; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function processAtrribute($name, $data) |
73
|
|
|
{ |
74
|
|
|
if (in_array($name, self::PLAYERS)) { |
75
|
|
|
return $this->players($data); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (in_array($name, self::GOAL_DETAILS)) { |
79
|
|
|
return array_map([$this, 'goals'], $this->toArray($data)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (in_array($name, self::CARDS)) { |
83
|
|
|
return array_map([$this, 'cards'], $this->toArray($data)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (in_array($name, self::SUBSTITUTIONS)) { |
87
|
|
|
return array_map([$this, 'substitutions'], $this->toArray($data)); } |
88
|
|
|
|
89
|
|
|
if (in_array($name, self::NUMERIC)) { |
90
|
|
|
return (int) $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($name == 'HasBeenRescheduled') { |
94
|
|
|
return (boolean) $data; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $match[$name] = (string) $data; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function players($data) |
101
|
|
|
{ |
102
|
|
|
return array_map('ltrim', $this->toArray($data)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function goals($goal) |
106
|
|
|
{ |
107
|
|
|
list($minute, $player) = explode("':", $goal); |
108
|
|
|
if (substr($player, 0, 3) == 'Own') { |
109
|
|
|
return [ |
110
|
|
|
'Minute' => (int) $minute, |
111
|
|
|
'Player' => ltrim(str_replace('Own', '', $player)), |
112
|
|
|
'Own' => true |
113
|
|
|
]; |
114
|
|
|
} else { |
115
|
|
|
return [ |
116
|
|
|
'Minute' => (int) $minute, |
117
|
|
|
'Player' => ltrim($player), |
118
|
|
|
'Own' => false |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function cards($card) |
124
|
|
|
{ |
125
|
|
|
list($minute, $player) = explode("': ", $card); |
126
|
|
|
return [ 'Minute' => (int) $minute, 'Player' => $player ]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function substitutions($substitution) |
130
|
|
|
{ |
131
|
|
|
list($minute, $player) = explode("': ", $substitution); |
132
|
|
|
if (strpos($player, 'in ') === 0) { |
133
|
|
|
return [ 'Minute' => (int) $minute, 'Type' => 'In', 'Player' => substr($player, 3) ]; |
134
|
|
|
} elseif (strpos($player, 'out ') === 0) { |
135
|
|
|
return [ 'Minute' => (int) $minute, 'Type' => 'Out', 'Player' => substr($player, 4) ]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function toArray($data) |
140
|
|
|
{ |
141
|
|
|
$data = $this->cleanse($data); |
142
|
|
|
|
143
|
|
|
return empty($data) ? [] : explode(';', rtrim($data,";")); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Very occasionally the data has spurious hard spaces that need to be removed |
148
|
|
|
*/ |
149
|
|
|
protected function cleanse($data) |
150
|
|
|
{ |
151
|
|
|
return str_replace(' ', '', $data); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.