1 | <?php |
||
22 | class FirebaseLib implements FirebaseInterface |
||
23 | { |
||
24 | private $baseURI; |
||
25 | private $timeout; |
||
26 | private $token; |
||
27 | private $curlHandler; |
||
28 | private $sslConnection; |
||
29 | |||
30 | /** |
||
31 | * Constructor |
||
32 | * |
||
33 | * @param string $baseURI |
||
34 | * @param string $token |
||
35 | */ |
||
36 | public function __construct($baseURI = '', $token = '') |
||
37 | { |
||
38 | if ($baseURI === '') { |
||
39 | trigger_error('You must provide a baseURI variable.', E_USER_ERROR); |
||
40 | } |
||
41 | |||
42 | if (!extension_loaded('curl')) { |
||
43 | trigger_error('Extension CURL is not loaded.', E_USER_ERROR); |
||
44 | } |
||
45 | |||
46 | $this->setBaseURI($baseURI); |
||
47 | $this->setTimeOut(10); |
||
48 | $this->setToken($token); |
||
49 | $this->initCurlHandler(); |
||
50 | $this->setSSLConnection(true); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Initializing the CURL handler |
||
55 | * |
||
56 | * @return void |
||
57 | */ |
||
58 | public function initCurlHandler() |
||
62 | |||
63 | /** |
||
64 | * Closing the CURL handler |
||
65 | * |
||
66 | * @return void |
||
67 | */ |
||
68 | public function closeCurlHandler() |
||
72 | |||
73 | /** |
||
74 | * Enabling/Disabling SSL Connection |
||
75 | * |
||
76 | * @param bool $enableSSLConnection |
||
77 | */ |
||
78 | public function setSSLConnection($enableSSLConnection) |
||
82 | |||
83 | /** |
||
84 | * Returns status of SSL Connection |
||
85 | * |
||
86 | * @return boolean |
||
87 | */ |
||
88 | public function getSSLConnection() |
||
92 | |||
93 | /** |
||
94 | * Sets Token |
||
95 | * |
||
96 | * @param string $token Token |
||
97 | * |
||
98 | * @return void |
||
99 | */ |
||
100 | public function setToken($token) |
||
101 | { |
||
102 | $this->token = $token; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Sets Base URI, ex: http://yourcompany.firebase.com/youruser |
||
107 | * |
||
108 | * @param string $baseURI Base URI |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | public function setBaseURI($baseURI) |
||
113 | { |
||
114 | $baseURI .= (substr($baseURI, -1) === '/' ? '' : '/'); |
||
115 | $this->baseURI = $baseURI; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Returns with the normalized JSON absolute path |
||
120 | * |
||
121 | * @param string $path Path |
||
122 | * @param array $options Options |
||
123 | * @return string |
||
124 | */ |
||
125 | private function getJsonPath($path, $options = array()) |
||
135 | |||
136 | /** |
||
137 | * Sets REST call timeout in seconds |
||
138 | * |
||
139 | * @param integer $seconds Seconds to timeout |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function setTimeOut($seconds) |
||
144 | { |
||
145 | $this->timeout = $seconds; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Writing data into Firebase with a PUT request |
||
150 | * HTTP 200: Ok |
||
151 | * |
||
152 | * @param string $path Path |
||
153 | * @param mixed $data Data |
||
154 | * @param array $options Options |
||
155 | * |
||
156 | * @return array Response |
||
157 | */ |
||
158 | public function set($path, $data, array $options = []) |
||
159 | { |
||
160 | return $this->writeData($path, $data, 'PUT', $options); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Pushing data into Firebase with a POST request |
||
165 | * HTTP 200: Ok |
||
166 | * |
||
167 | * @param string $path Path |
||
168 | * @param mixed $data Data |
||
169 | * @param array $options Options |
||
170 | * |
||
171 | * @return array Response |
||
172 | */ |
||
173 | public function push($path, $data, array $options = []) |
||
174 | { |
||
175 | return $this->writeData($path, $data, 'POST', $options); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Updating data into Firebase with a PATH request |
||
180 | * HTTP 200: Ok |
||
181 | * |
||
182 | * @param string $path Path |
||
183 | * @param mixed $data Data |
||
184 | * @param array $options Options |
||
185 | * |
||
186 | * @return array Response |
||
187 | */ |
||
188 | public function update($path, $data, array $options = []) |
||
189 | { |
||
190 | return $this->writeData($path, $data, 'PATCH', $options); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Reading data from Firebase |
||
195 | * HTTP 200: Ok |
||
196 | * |
||
197 | * @param string $path Path |
||
198 | * @param array $options Options |
||
199 | * |
||
200 | * @return array Response |
||
201 | */ |
||
202 | public function get($path, array $options = []) |
||
203 | { |
||
204 | try { |
||
205 | $ch = $this->getCurlHandler($path, 'GET', $options); |
||
206 | $return = curl_exec($ch); |
||
207 | } catch (Exception $e) { |
||
208 | $return = null; |
||
209 | } |
||
210 | return $return; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Deletes data from Firebase |
||
215 | * HTTP 204: Ok |
||
216 | * |
||
217 | * @param string $path Path |
||
218 | * @param array $options Options |
||
219 | * |
||
220 | * @return array Response |
||
221 | */ |
||
222 | public function delete($path, array $options = []) |
||
223 | { |
||
224 | try { |
||
225 | $ch = $this->getCurlHandler($path, 'DELETE', $options); |
||
226 | $return = curl_exec($ch); |
||
227 | } catch (Exception $e) { |
||
228 | $return = null; |
||
229 | } |
||
230 | return $return; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Returns with Initialized CURL Handler |
||
235 | * |
||
236 | * @param string $path Path |
||
237 | * @param string $mode Mode |
||
238 | * @param array $options Options |
||
239 | * |
||
240 | * @return resource Curl Handler |
||
241 | */ |
||
242 | private function getCurlHandler($path, $mode, $options = array()) |
||
255 | |||
256 | private function writeData($path, $data, $method = 'PUT', $options = array()) |
||
273 | } |
||
274 |