|
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 string |
|
28
|
|
|
*/ |
|
29
|
|
|
public $key; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var JCFirebase |
|
33
|
|
|
*/ |
|
34
|
|
|
public $firebase; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* FirebaseModel constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \JCFirebase\JCFirebase $firebase |
|
40
|
|
|
*/ |
|
41
|
5 |
|
public function __construct(JCFirebase $firebase = null) |
|
42
|
|
|
{ |
|
43
|
5 |
|
$this->firebase = $firebase; |
|
44
|
5 |
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
public static function getNodeName() |
|
47
|
|
|
{ |
|
48
|
5 |
|
return static::$nodeName ?: strtolower((new \ReflectionClass(get_called_class()))->getShortName()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public static function setNodeName($nodeName) |
|
52
|
|
|
{ |
|
53
|
1 |
|
static::$nodeName = $nodeName; |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public function getData() |
|
60
|
|
|
{ |
|
61
|
5 |
|
$object = clone $this; |
|
62
|
5 |
|
unset($object->firebase); |
|
63
|
5 |
|
unset($object->key); |
|
64
|
|
|
|
|
65
|
5 |
|
return get_object_vars($object); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function create() |
|
72
|
|
|
{ |
|
73
|
3 |
|
$response = $this->firebase->post(self::getNodeName(), array( |
|
74
|
3 |
|
'data' => $this->getData() |
|
75
|
3 |
|
)); |
|
76
|
|
|
|
|
77
|
3 |
|
$this->key = json_decode($response->body())->name; |
|
78
|
|
|
|
|
79
|
3 |
|
return $response->success(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function save() |
|
87
|
|
|
{ |
|
88
|
4 |
|
if (!empty($this->key)) { |
|
89
|
3 |
|
$response = $this->firebase->put(self::getNodeName() . '/' . $this->key, array( |
|
90
|
3 |
|
'data' => $this->getData() |
|
91
|
3 |
|
)); |
|
92
|
|
|
|
|
93
|
3 |
|
$success = $response->success(); |
|
94
|
3 |
|
} else { |
|
95
|
1 |
|
$success = $this->create(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
return $success; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function delete() |
|
105
|
|
|
{ |
|
106
|
1 |
|
$success = false; |
|
107
|
1 |
|
if (!empty($this->key)) { |
|
108
|
1 |
|
$response = $this->firebase->delete(self::getNodeName() . '/' . $this->key); |
|
109
|
|
|
|
|
110
|
1 |
|
$success = $response->success(); |
|
111
|
1 |
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
return $success; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param $key |
|
118
|
|
|
* @param JCFirebase $firebase |
|
119
|
|
|
* |
|
120
|
|
|
* @return object |
|
121
|
|
|
*/ |
|
122
|
5 |
|
public static function findByKey($key, JCFirebase $firebase) |
|
123
|
|
|
{ |
|
124
|
5 |
|
$response = $firebase->get(self::getNodeName() . '/' . $key); |
|
125
|
5 |
|
$object = null; |
|
126
|
5 |
|
if ($response->success() && $response->body() != 'null') { |
|
127
|
4 |
|
$object = self::map($response->json(), new static()); |
|
128
|
4 |
|
$object->key = $key; |
|
129
|
4 |
|
$object->firebase = $firebase; |
|
130
|
4 |
|
} |
|
131
|
|
|
|
|
132
|
5 |
|
return $object; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param JCFirebase $firebase |
|
137
|
|
|
* |
|
138
|
|
|
* @return array(FirebaseModel) |
|
|
|
|
|
|
139
|
|
|
*/ |
|
140
|
1 |
|
public static function findAll(JCFirebase $firebase) |
|
141
|
|
|
{ |
|
142
|
1 |
|
$response = $firebase->get(self::getNodeName()); |
|
143
|
1 |
|
$objects = array(); |
|
144
|
|
|
|
|
145
|
1 |
|
$jsonObject = json_decode($response->body(), true); |
|
146
|
1 |
|
if ($response->success() && count($jsonObject)) { |
|
147
|
|
|
do { |
|
148
|
1 |
|
$object = self::map((object)current($jsonObject), new static()); |
|
149
|
1 |
|
$object->key = key($jsonObject); |
|
150
|
1 |
|
$object->firebase = $firebase; |
|
151
|
1 |
|
$objects[] = $object; |
|
152
|
1 |
|
} while (next($jsonObject)); |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
return $objects; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
4 |
|
protected static function map($object, $instance) |
|
159
|
|
|
{ |
|
160
|
4 |
|
$mapper = new JsonMapper(); |
|
161
|
4 |
|
return $mapper->map($object, $instance); |
|
162
|
|
|
} |
|
163
|
|
|
} |
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.