1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JeroenDesloovere\VolleyAdmin2; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* In this file we store all generic functions that we will be using for VolleyAdmin2. |
7
|
|
|
* |
8
|
|
|
* @author Jeroen Desloovere <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class VolleyAdmin2 |
11
|
|
|
{ |
12
|
|
|
const API_URL = 'http://www.volleyadmin2.be/services'; |
13
|
|
|
|
14
|
|
|
// Possible methods |
15
|
|
|
const API_METHOD_STANDINGS = 'rangschikking'; |
16
|
|
|
const API_METHOD_MATCHES = 'wedstrijden'; |
17
|
|
|
const API_METHOD_TEAMS = 'series'; |
18
|
|
|
|
19
|
|
|
// Possible variables |
20
|
|
|
const CLUB_NUMBER = 'stamnummer'; |
21
|
|
|
const PROVINCE_ID = 'province_id'; |
22
|
|
|
const SERIES_ID = 'reeks'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Do call |
26
|
|
|
* |
27
|
|
|
* @param string $method |
28
|
|
|
* @param array $parameters |
29
|
|
|
* @return array |
30
|
|
|
* @throws Exception |
31
|
|
|
*/ |
32
|
|
|
protected function doCall($method, $parameters = array()) |
33
|
|
|
{ |
34
|
|
|
// check if curl is available |
35
|
|
|
if (!function_exists('curl_init')) { |
36
|
|
|
throw new Exception('This method requires cURL (http://php.net/curl), it seems like the extension isn\'t installed.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$parameters['format'] = 'json'; |
40
|
|
|
$parameterString = ''; |
41
|
|
|
foreach ($parameters as $key => $value) { |
42
|
|
|
$parameterString .= ($parameterString == '') ? '?' : '&'; |
43
|
|
|
$value = str_replace(' ', '%20', $value); |
44
|
|
|
$parameterString .= $key . '=' . $value; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// define endPoint |
48
|
|
|
$endPoint = self::API_URL . '/' . $method . '_xml.php' . $parameterString; |
49
|
|
|
|
50
|
|
|
// init curl |
51
|
|
|
$curl = curl_init(); |
52
|
|
|
|
53
|
|
|
// set options |
54
|
|
|
curl_setopt($curl, CURLOPT_URL, $endPoint); |
55
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
56
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 10); |
57
|
|
|
|
58
|
|
|
// execute curl |
59
|
|
|
$response = curl_exec($curl); |
60
|
|
|
|
61
|
|
|
// fetch errors from curl |
62
|
|
|
$errorNumber = curl_errno($curl); |
63
|
|
|
$errorMessage = curl_error($curl); |
64
|
|
|
|
65
|
|
|
// close curl |
66
|
|
|
curl_close($curl); |
67
|
|
|
|
68
|
|
|
// we have errors |
69
|
|
|
if ($errorNumber != '') { |
70
|
|
|
throw new Exception($errorMessage); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// redefine response as json decoded |
74
|
|
|
return json_decode($response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check parameters |
79
|
|
|
* |
80
|
|
|
* @param array $parameters |
81
|
|
|
* @return array |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
|
|
protected function checkParameters($parameters) |
85
|
|
|
{ |
86
|
|
|
$result = array(); |
87
|
|
|
|
88
|
|
|
// We loop all parameters to find their real key (= dutch key which the API uses) |
89
|
|
|
foreach ($parameters as $key => $value) { |
90
|
|
|
if ($value === null) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!in_array($key, $this->getPossibleParameters())) { |
95
|
|
|
throw new Exception('The key "' . $key . '" is invalid.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$result[$key] = $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
private function getPossibleParameters() |
108
|
|
|
{ |
109
|
|
|
return array( |
110
|
|
|
self::CLUB_NUMBER, |
111
|
|
|
self::PROVINCE_ID, |
112
|
|
|
self::SERIES_ID, |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get matches |
118
|
|
|
* |
119
|
|
|
* @param string $seriesId |
120
|
|
|
* @param int $provinceId |
121
|
|
|
* @param string $clubNumber |
122
|
|
|
* @return array |
123
|
|
|
* @throws Exception |
124
|
|
|
*/ |
125
|
|
|
public function getMatches( |
126
|
|
|
$seriesId = null, |
127
|
|
|
$provinceId = null, |
128
|
|
|
$clubNumber = null |
129
|
|
|
) { |
130
|
|
|
return $this->doCall( |
131
|
|
|
self::API_METHOD_MATCHES, |
132
|
|
|
$this->checkParameters(array( |
133
|
|
|
self::SERIES_ID => $seriesId, |
134
|
|
|
self::PROVINCE_ID => $provinceId, |
135
|
|
|
self::CLUB_NUMBER => $clubNumber, |
136
|
|
|
)) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get series |
142
|
|
|
* |
143
|
|
|
* @param int $provinceId - Fill in if you want to filter for province. |
144
|
|
|
* @return array |
145
|
|
|
* @throws Exception |
146
|
|
|
*/ |
147
|
|
|
public function getSeries($provinceId = null) |
148
|
|
|
{ |
149
|
|
|
return $this->doCall( |
150
|
|
|
self::API_METHOD_TEAMS, |
151
|
|
|
$this->checkParameters(array( |
152
|
|
|
self::PROVINCE_ID => $provinceId, |
153
|
|
|
)) |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get standings |
159
|
|
|
* |
160
|
|
|
* @param string $seriesId |
161
|
|
|
* @param int $provinceId |
162
|
|
|
* @return array |
163
|
|
|
* @throws Exception |
164
|
|
|
*/ |
165
|
|
|
public function getStandings( |
166
|
|
|
$seriesId = null, |
167
|
|
|
$provinceId = null |
168
|
|
|
) { |
169
|
|
|
return $this->doCall( |
170
|
|
|
self::API_METHOD_STANDINGS, |
171
|
|
|
$this->checkParameters(array( |
172
|
|
|
self::SERIES_ID => $seriesId, |
173
|
|
|
self::PROVINCE_ID => $provinceId, |
174
|
|
|
)) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|