1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: jaredchu |
5
|
|
|
* Date: 11/29/16 |
6
|
|
|
* Time: 3:47 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace JCFirebase; |
10
|
|
|
|
11
|
|
|
use Requests; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class JCFirebase |
15
|
|
|
* @package JCFirebase |
16
|
|
|
* reference https://www.firebase.com/docs/rest/api/ |
17
|
|
|
*/ |
18
|
|
|
class JCFirebase |
19
|
|
|
{ |
20
|
|
|
public $firebaseSecret; |
21
|
|
|
public $firebaseURI; |
22
|
|
|
public $firebaseDefaultPath; |
23
|
|
|
|
24
|
|
|
public $auth; |
25
|
|
|
public $shallow; |
26
|
|
|
public $print; |
27
|
|
|
public $callback; |
28
|
|
|
public $format; |
29
|
|
|
public $download; |
30
|
|
|
public $orderBy; |
31
|
|
|
public $limitToFirst; |
32
|
|
|
public $limitToLast; |
33
|
|
|
public $startAt; |
34
|
|
|
public $endAt; |
35
|
|
|
public $equalTo; |
36
|
|
|
|
37
|
|
|
public $streaming = 'text/event-stream'; |
38
|
|
|
public $priority = '.priority'; |
39
|
|
|
public $serverValues = '.sv'; |
40
|
|
|
|
41
|
|
|
public $rulePath = '.settings/rules.json'; |
42
|
|
|
|
43
|
|
|
public $requestHeader = array( |
44
|
|
|
'accept' => 'application/json', |
45
|
|
|
'contentType' => 'application/json; charset=utf-8', |
46
|
|
|
'dataType' => 'json' |
47
|
|
|
); |
48
|
|
|
public $requestOptions = array(); |
49
|
|
|
|
50
|
|
|
public function __construct($firebaseURI,$firebaseSecret = '',$firebaseDefaultPath = '/') |
51
|
|
|
{ |
52
|
|
|
$this->firebaseSecret = $firebaseSecret; |
53
|
|
|
$this->firebaseURI = $firebaseURI; |
54
|
|
|
$this->firebaseDefaultPath = $firebaseDefaultPath; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getPathURI($path = ''){ |
58
|
|
|
//remove .json or last slash from firebaseURI |
59
|
|
|
$templates = array( |
60
|
|
|
'.json', |
61
|
|
|
'/.json', |
62
|
|
|
'/' |
63
|
|
|
); |
64
|
|
|
foreach ($templates as $template){ |
65
|
|
|
$this->firebaseURI = rtrim($this->firebaseURI,$template); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
//check https |
69
|
|
|
if(strpos($this->firebaseURI, 'http://') !== false){ |
70
|
|
|
throw new \Exception("https is required."); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
//check firebaseURI |
74
|
|
|
if(strlen($this->firebaseURI) == 0){ |
75
|
|
|
throw new \Exception("firebase URI is required"); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$pathURI = $this->firebaseURI.$this->firebaseDefaultPath.$path.".json"; |
79
|
|
|
return $pathURI; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $options |
85
|
|
|
* @return \Requests_Response |
86
|
|
|
*/ |
87
|
|
|
public function get($path = '',$options = array()){ |
88
|
|
|
return Requests::get($this->getPathURI($path),$this->requestHeader,$this->mergeRequestOptions($options)); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function put($path = '',$options = array()){ |
92
|
|
|
return Requests::put($this->getPathURI($path),$this->requestHeader,$this->mergeRequestOptions($options,true)); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function post($path = '',$options = array()){ |
96
|
|
|
return Requests::post($this->getPathURI($path),$this->requestHeader,$this->mergeRequestOptions($options,true)); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function patch($path = '',$options = array()){ |
100
|
|
|
return Requests::patch($this->getPathURI($path),$this->requestHeader,$this->mergeRequestOptions($options,true)); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function delete($path = '',$options = array()){ |
104
|
|
|
return Requests::delete($this->getPathURI($path),$this->requestHeader,$this->mergeRequestOptions($options)); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function mergeRequestOptions($options = array(),$encode = false){ |
108
|
|
|
$requestOptions = array(); |
109
|
|
|
if(isset($options['settings'])){ |
110
|
|
|
$requestOptions = array_merge($options['settings'],$requestOptions); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if(isset($options['data'])){ |
114
|
|
|
$requestOptions = array_merge($options['data'],$requestOptions); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if($encode){ |
118
|
|
|
$requestOptions = json_encode($requestOptions); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $requestOptions; |
122
|
|
|
} |
123
|
|
|
} |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.