|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: jaredchu |
|
5
|
|
|
* Date: 1/14/17 |
|
6
|
|
|
* Time: 2:25 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace JCFirebase\Models; |
|
10
|
|
|
|
|
11
|
|
|
use JCFirebase\JCFirebase; |
|
12
|
|
|
use JsonMapper; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class FirebaseModel |
|
16
|
|
|
* @package JCFirebase |
|
17
|
|
|
*/ |
|
18
|
|
|
class FirebaseModel |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
public static $nodeName = ''; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
public static $maps = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
public $key; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var JCFirebase |
|
38
|
|
|
*/ |
|
39
|
|
|
public $firebase; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* FirebaseModel constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \JCFirebase\JCFirebase $firebase |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public function __construct(JCFirebase $firebase = null) |
|
47
|
|
|
{ |
|
48
|
6 |
|
$this->firebase = $firebase; |
|
49
|
6 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
6 |
|
public static function getNodeName() |
|
55
|
|
|
{ |
|
56
|
6 |
|
return static::$nodeName ?: strtolower((new \ReflectionClass(get_called_class()))->getShortName()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $nodeName |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public static function setNodeName($nodeName) |
|
63
|
|
|
{ |
|
64
|
2 |
|
static::$nodeName = $nodeName; |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function getMaps() |
|
71
|
|
|
{ |
|
72
|
|
|
return static::$maps; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $maps |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public static function setMaps($maps) |
|
79
|
|
|
{ |
|
80
|
1 |
|
static::$maps = $maps; |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
6 |
|
public function getData() |
|
87
|
|
|
{ |
|
88
|
6 |
|
$object = clone $this; |
|
89
|
6 |
|
unset($object->firebase); |
|
90
|
6 |
|
unset($object->key); |
|
91
|
|
|
|
|
92
|
6 |
|
return static::mapAttributes(get_object_vars($object)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
4 |
|
public function create() |
|
99
|
|
|
{ |
|
100
|
4 |
|
$response = $this->firebase->post(self::getNodeName(), array( |
|
101
|
4 |
|
'data' => $this->getData() |
|
102
|
4 |
|
)); |
|
103
|
|
|
|
|
104
|
4 |
|
$this->key = json_decode($response->body())->name; |
|
105
|
|
|
|
|
106
|
4 |
|
return $response->success(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
4 |
|
public function save() |
|
114
|
|
|
{ |
|
115
|
4 |
|
if (!empty($this->key)) { |
|
116
|
3 |
|
$response = $this->firebase->put(self::getNodeName() . '/' . $this->key, array( |
|
117
|
3 |
|
'data' => $this->getData() |
|
118
|
3 |
|
)); |
|
119
|
|
|
|
|
120
|
3 |
|
$success = $response->success(); |
|
121
|
3 |
|
} else { |
|
122
|
1 |
|
$success = $this->create(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
4 |
|
return $success; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function delete() |
|
132
|
|
|
{ |
|
133
|
1 |
|
$success = false; |
|
134
|
1 |
|
if (!empty($this->key)) { |
|
135
|
1 |
|
$response = $this->firebase->delete(self::getNodeName() . '/' . $this->key); |
|
136
|
|
|
|
|
137
|
1 |
|
$success = $response->success(); |
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
return $success; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param $key |
|
145
|
|
|
* @param JCFirebase $firebase |
|
146
|
|
|
* |
|
147
|
|
|
* @return object |
|
148
|
|
|
*/ |
|
149
|
6 |
|
public static function findByKey($key, JCFirebase $firebase) |
|
150
|
|
|
{ |
|
151
|
6 |
|
$response = $firebase->get(self::getNodeName() . '/' . $key); |
|
152
|
6 |
|
$object = null; |
|
153
|
6 |
|
if ($response->success() && $response->body() != 'null') { |
|
154
|
5 |
|
$object = self::map($response->json(), new static()); |
|
155
|
5 |
|
$object->key = $key; |
|
156
|
5 |
|
$object->firebase = $firebase; |
|
157
|
5 |
|
} |
|
158
|
|
|
|
|
159
|
6 |
|
return $object; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param JCFirebase $firebase |
|
164
|
|
|
* |
|
165
|
|
|
* @return array(FirebaseModel) |
|
|
|
|
|
|
166
|
|
|
*/ |
|
167
|
2 |
|
public static function findAll(JCFirebase $firebase) |
|
168
|
|
|
{ |
|
169
|
1 |
|
$response = $firebase->get(self::getNodeName()); |
|
170
|
2 |
|
$objects = array(); |
|
171
|
|
|
|
|
172
|
1 |
|
$jsonObject = json_decode($response->body(), true); |
|
173
|
1 |
|
if ($response->success() && count($jsonObject)) { |
|
174
|
|
|
do { |
|
175
|
1 |
|
$object = self::map((object)current($jsonObject), new static()); |
|
176
|
1 |
|
$object->key = key($jsonObject); |
|
177
|
1 |
|
$object->firebase = $firebase; |
|
178
|
1 |
|
$objects[] = $object; |
|
179
|
1 |
|
} while (next($jsonObject)); |
|
180
|
1 |
|
} |
|
181
|
|
|
|
|
182
|
1 |
|
return $objects; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param $object |
|
187
|
|
|
* @param $instance |
|
188
|
|
|
* @return object |
|
189
|
|
|
*/ |
|
190
|
5 |
|
protected static function map($object, $instance) |
|
191
|
|
|
{ |
|
192
|
5 |
|
$mapper = new JsonMapper(); |
|
193
|
5 |
|
return $mapper->map((object)static::mapAttributes(get_object_vars($object), false), $instance); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param array $objectVars |
|
198
|
|
|
* @param bool $fromLocal |
|
199
|
|
|
* @return array |
|
200
|
|
|
*/ |
|
201
|
6 |
|
protected static function mapAttributes(array $objectVars, $fromLocal = true) |
|
202
|
|
|
{ |
|
203
|
6 |
|
foreach (static::$maps as $localAttr => $DBAttr) { |
|
204
|
1 |
|
if ($fromLocal) { |
|
205
|
1 |
|
$objectVars[$DBAttr] = $objectVars[$localAttr]; |
|
206
|
1 |
|
unset($objectVars[$localAttr]); |
|
207
|
1 |
|
} else { |
|
208
|
1 |
|
$objectVars[$localAttr] = $objectVars[$DBAttr]; |
|
209
|
1 |
|
unset($objectVars[$DBAttr]); |
|
210
|
|
|
} |
|
211
|
6 |
|
} |
|
212
|
|
|
|
|
213
|
6 |
|
return $objectVars; |
|
214
|
|
|
} |
|
215
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.