1 | <?php |
||
14 | class ApiClient |
||
15 | { |
||
16 | /** |
||
17 | * Default API endpoint. |
||
18 | */ |
||
19 | protected $apiEndpoint; |
||
20 | |||
21 | /** |
||
22 | * Subscriber's API key. |
||
23 | */ |
||
24 | protected $apiKey; |
||
25 | |||
26 | /** |
||
27 | * Guzzle client instance. |
||
28 | */ |
||
29 | protected $guzzleClient; |
||
30 | |||
31 | /** |
||
32 | * The raw XML response returned from the last XMLSoccer request. |
||
33 | */ |
||
34 | protected $response; |
||
35 | |||
36 | /** |
||
37 | * Optional (recommended) setting of an API key when a new instance is instantiated. |
||
38 | * Setting the API endpoint, default or demo according to the (optional) parameter. |
||
39 | * Advanced users may wish to a Guzzle client with their own configuration settings |
||
40 | * but this will rarely be needed. |
||
41 | * |
||
42 | * @param string | null $apiKey |
||
43 | * @param boolean $demo |
||
44 | * @param GuzzleClient $guzzleClient |
||
45 | */ |
||
46 | public function __construct($apiKey = null, $demo = false, $guzzleClient = null) |
||
52 | |||
53 | /** |
||
54 | * Override default API endpoint. |
||
55 | * |
||
56 | * @param $apiEndpoint |
||
57 | */ |
||
58 | public function setApiEndpoint($demo = false) |
||
62 | |||
63 | /** |
||
64 | * Set or override the API key. |
||
65 | * |
||
66 | * @param $apiKey |
||
67 | */ |
||
68 | public function setApiKey($apiKey) |
||
72 | |||
73 | /** |
||
74 | * Accept the requested method and its parameters, make the request to XML Soccer and validate the response. |
||
75 | * |
||
76 | * @param $name |
||
77 | * @param $params |
||
78 | * @return SimpleXMLElement |
||
79 | */ |
||
80 | public function __call($method, $params) |
||
94 | |||
95 | public function get() |
||
99 | |||
100 | public function xml() |
||
110 | |||
111 | public function object() |
||
115 | |||
116 | public function json() |
||
120 | |||
121 | /** |
||
122 | * Build the base URI for the API from its endpoint and the resource being requested. |
||
123 | * |
||
124 | * @param $method |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function buildUri($method) |
||
131 | |||
132 | /** |
||
133 | * Almost all API calls require an API Key so we add it to the parameters. |
||
134 | * |
||
135 | * @param $params |
||
136 | * @return string |
||
137 | */ |
||
138 | protected function prepareParams($method, $params) |
||
150 | |||
151 | /** |
||
152 | * Initialise or inject an instance of the Guzzle client. |
||
153 | * |
||
154 | * @return GuzzleClient |
||
155 | */ |
||
156 | protected function initGuzzleClient($guzzleClient = null) |
||
160 | |||
161 | /** |
||
162 | * Make the request to the XML Soccer service and validate response. |
||
163 | * |
||
164 | * @param string $uri |
||
165 | * @param array | null $params |
||
166 | * @throws RequestFailedException |
||
167 | * @return SimpleXMLElement |
||
168 | */ |
||
169 | protected function request($uri, $params) |
||
179 | } |
||
180 |