1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace Zend\Firebase; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* PHP7 FIREBASE LIBRARY (http://samuelventimiglia.it/) |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/samuel20miglia/zend_Firebase |
10
|
|
|
* @copyright Copyright (c) 2016-now Ventimiglia Samuel - Biasin Davide |
11
|
|
|
* @license BSD 3-Clause License |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @author Davide Biasin |
18
|
|
|
* @package ZendFirebase |
19
|
|
|
*/ |
20
|
|
|
class FirebaseResponce |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Data from Firebase |
25
|
|
|
* |
26
|
|
|
* @var array $firebaseData |
27
|
|
|
*/ |
28
|
|
|
protected $firebaseData; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Type of operation |
32
|
|
|
* |
33
|
|
|
* @var string $operation |
34
|
|
|
*/ |
35
|
|
|
protected $operation; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Http server code |
39
|
|
|
* |
40
|
|
|
* @var integer $status |
41
|
|
|
*/ |
42
|
|
|
protected $status; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Format to array the responce |
46
|
|
|
* |
47
|
|
|
* @return array $firebaseData |
48
|
|
|
*/ |
49
|
|
|
public function getFirebaseData(): array |
50
|
|
|
{ |
51
|
|
|
return $this->firebaseData; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Type of Operation |
56
|
|
|
* |
57
|
|
|
* @return string $operation |
58
|
|
|
*/ |
59
|
|
|
public function getOperation(): string |
60
|
|
|
{ |
61
|
|
|
return $this->operation; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Status from http server |
66
|
|
|
* |
67
|
|
|
* @return integer $status |
68
|
|
|
*/ |
69
|
|
|
public function getStatus(): int |
70
|
|
|
{ |
71
|
|
|
return $this->status; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set data from firebase api |
76
|
|
|
* |
77
|
|
|
* @param array $firebaseData |
78
|
|
|
*/ |
79
|
|
|
protected function setFirebaseData($firebaseData) |
80
|
|
|
{ |
81
|
|
|
$this->firebaseData = $firebaseData; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set type of operation |
86
|
|
|
* |
87
|
|
|
* @param string $operation |
88
|
|
|
*/ |
89
|
|
|
protected function setOperation($operation) |
90
|
|
|
{ |
91
|
|
|
$this->operation = $operation; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set status responce |
96
|
|
|
* |
97
|
|
|
* @param integer $status |
98
|
|
|
*/ |
99
|
|
|
protected function setStatus($status) |
100
|
|
|
{ |
101
|
|
|
$this->status = $status; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Method for validate data arrives in _costruct. |
106
|
|
|
* If all data was correct skip function without returns. |
107
|
|
|
* |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
|
|
protected function validateResponce() |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
/* check validity of Operation */ |
114
|
|
|
$this->validateOperation(); |
115
|
|
|
|
116
|
|
|
/* check validity of Status */ |
117
|
|
|
$this->validateStatus(); |
118
|
|
|
|
119
|
|
|
/* check validity of FirebaseData */ |
120
|
|
|
$this->validateData(); |
121
|
|
|
} catch (\Exception $e) { |
122
|
|
|
throw $e->getMessage(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Validate type of data receved |
128
|
|
|
* |
129
|
|
|
* @throws \Exception |
130
|
|
|
*/ |
131
|
|
|
private function validateOperation() |
132
|
|
|
{ |
133
|
|
|
if (! is_string($this->getOperation())) { |
134
|
|
|
$getOperation = "Operation parameter must be STRING and NOT EMPTY. Received : "; |
135
|
|
|
$getOperation .= gettype($this->getOperation()) . " ({$this->getOperation()})."; |
136
|
|
|
|
137
|
|
|
throw new \Exception($getOperation); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Validate type of data receved |
143
|
|
|
* |
144
|
|
|
* @throws \Exception |
145
|
|
|
*/ |
146
|
|
|
private function validateStatus() |
147
|
|
|
{ |
148
|
|
|
if (! is_numeric($this->getStatus())) { |
149
|
|
|
$getStatus = "Status parameter must be NUMERIC. Received : "; |
150
|
|
|
$getStatus .= gettype($this->getStatus()) . " ({$this->getStatus()})."; |
151
|
|
|
|
152
|
|
|
throw new \Exception($getStatus); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Validate type of data receved |
158
|
|
|
* |
159
|
|
|
* @throws \Exception |
160
|
|
|
*/ |
161
|
|
|
private function validateData() |
162
|
|
|
{ |
163
|
|
|
if (! is_array($this->getFirebaseData())) { |
164
|
|
|
$gettype = "FirebaseData parameter must be ARRAY. Received : " . gettype($this->getFirebaseData()) . "."; |
165
|
|
|
throw new \Exception($gettype); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Validate type of json receved |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
protected function validateJson():string |
175
|
|
|
{ |
176
|
|
|
switch (json_last_error()) { |
177
|
|
|
case JSON_ERROR_NONE: |
178
|
|
|
$jsonValidator = ''; |
179
|
|
|
break; |
180
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
181
|
|
|
$jsonValidator = ' - Underflow or the modes mismatch'; |
182
|
|
|
break; |
183
|
|
|
case JSON_ERROR_SYNTAX: |
184
|
|
|
$jsonValidator = ' - Syntax error, malformed JSON'; |
185
|
|
|
break; |
186
|
|
|
case JSON_ERROR_UTF8: |
187
|
|
|
$jsonValidator = ' - Malformed UTF-8 characters, possibly incorrectly encoded'; |
188
|
|
|
break; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $jsonValidator; |
|
|
|
|
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: