1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PeterColes\XmlSoccer\Converters\Objects; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use SimpleXMLElement; |
7
|
|
|
|
8
|
|
|
class Match |
9
|
|
|
{ |
10
|
|
|
const PLAYERS = [ |
11
|
|
|
'HomeLineupGoalkeeper', |
12
|
|
|
'HomeLineupDefense', |
13
|
|
|
'HomeLineupMidfield', |
14
|
|
|
'HomeLineupForward', |
15
|
|
|
'HomeLineupSubstitutes', |
16
|
|
|
'AwayLineupGoalkeeper', |
17
|
|
|
'AwayLineupDefense', |
18
|
|
|
'AwayLineupMidfield', |
19
|
|
|
'AwayLineupForward', |
20
|
|
|
'AwayLineupSubstitutes' |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
const GOAL_DETAILS = [ |
24
|
|
|
'HomeGoalDetails', |
25
|
|
|
'AwayGoalDetails' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
const CARDS = [ |
29
|
|
|
'HomeTeamYellowCardDetails', |
30
|
|
|
'HomeTeamRedCardDetails', |
31
|
|
|
'AwayTeamYellowCardDetails', |
32
|
|
|
'AwayTeamRedCardDetails' |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
const SUBSTITUTIONS = [ |
36
|
|
|
'HomeSubDetails', |
37
|
|
|
'AwaySubDetails' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
const NUMERIC = [ |
41
|
|
|
'Id', |
42
|
|
|
'FixtureMatch_Id', |
43
|
|
|
'Round', |
44
|
|
|
'Spectators', |
45
|
|
|
'HomeTeam_Id', |
46
|
|
|
'HomeGoals', |
47
|
|
|
'HalfTimeHomeGoals', |
48
|
|
|
'HomeShots', |
49
|
|
|
'HomeShotsOnTarget', |
50
|
|
|
'HomeFouls', |
51
|
|
|
'HomeYellowCards', |
52
|
|
|
'HomeRedCards', |
53
|
|
|
'AwayTeam_Id', |
54
|
|
|
'AwayGoals', |
55
|
|
|
'HalfTimeAwayGoals', |
56
|
|
|
'AwayShots', |
57
|
|
|
'AwayShotsOnTarget', |
58
|
|
|
'AwayFouls', |
59
|
|
|
'AwayYellowCards', |
60
|
|
|
'AwayRedCards' |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
public function handle(SimpleXMLElement $match) |
64
|
|
|
{ |
65
|
|
|
$object = new stdClass; |
66
|
|
|
|
67
|
|
|
foreach ($match as $child) { |
68
|
|
|
$name = $child->getName(); |
69
|
|
|
$object->$name = $this->processAtrribute($name, $child); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $object; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function processAtrribute($name, $data) |
76
|
|
|
{ |
77
|
|
|
if (in_array($name, self::PLAYERS)) { |
78
|
|
|
return $this->players($data); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (in_array($name, self::GOAL_DETAILS)) { |
82
|
|
|
return array_map([ $this, 'goals' ], $this->toArray($data)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (in_array($name, self::CARDS)) { |
86
|
|
|
return array_map([ $this, 'cards' ], $this->toArray($data)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (in_array($name, self::SUBSTITUTIONS)) { |
90
|
|
|
return array_map([ $this, 'substitutions' ], $this->toArray($data)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (in_array($name, self::NUMERIC)) { |
94
|
|
|
return (int) $data; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($name == 'HasBeenRescheduled') { |
98
|
|
|
return (boolean) $data; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return (string) $data; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function players($data) |
105
|
|
|
{ |
106
|
|
|
return array_map('ltrim', $this->toArray($data)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function goals($goal) |
110
|
|
|
{ |
111
|
|
|
list($minute, $player) = explode("':", $goal); |
112
|
|
|
if (substr($player, 0, 3) == 'Own') { |
113
|
|
|
return (object) [ |
114
|
|
|
'Minute' => (int) $minute, |
115
|
|
|
'Player' => ltrim(str_replace('Own', '', $player)), |
116
|
|
|
'Own' => true |
117
|
|
|
]; |
118
|
|
|
} else { |
119
|
|
|
return (object) [ |
120
|
|
|
'Minute' => (int) $minute, |
121
|
|
|
'Player' => ltrim($player), |
122
|
|
|
'Own' => false |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function cards($card) |
128
|
|
|
{ |
129
|
|
|
list($minute, $player) = explode("': ", $card); |
130
|
|
|
return (object) [ 'Minute' => (int) $minute, 'Player' => $player ]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function substitutions($substitution) |
134
|
|
|
{ |
135
|
|
|
list($minute, $player) = explode("': ", $substitution); |
136
|
|
|
if (strpos($player, 'in ') === 0) { |
137
|
|
|
return (object) [ 'Minute' => (int) $minute, 'Type' => 'In', 'Player' => substr($player, 3) ]; |
138
|
|
|
} elseif (strpos($player, 'out ') === 0) { |
139
|
|
|
return (object) [ 'Minute' => (int) $minute, 'Type' => 'Out', 'Player' => substr($player, 4) ]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function toArray($data) |
144
|
|
|
{ |
145
|
|
|
$data = $this->cleanse($data); |
146
|
|
|
|
147
|
|
|
return empty($data) ? [ ] : explode(';', rtrim($data, ";")); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Very occasionally the data has spurious hard spaces that need to be removed |
152
|
|
|
*/ |
153
|
|
|
protected function cleanse($data) |
154
|
|
|
{ |
155
|
|
|
return str_replace(' ', '', $data); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|