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