Repository navigation
Expand file tree
/
Copy pathPhpFirebase.php
More file actions
183 lines (159 loc) · 4.62 KB
/
Copy pathPhpFirebase.php
File metadata and controls
183 lines (159 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* A simple crud system using php and firebase
*
* Creator: Josiah O Yahaha
* Email: josiahoyahaya@gmail.com
*/
namespace Coderatio\PhpFirebase;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
class PhpFirebase {
protected $database;
protected $table = 'records';
public $primaryKey = 'id';
protected $firebase;
public $secretKeyJsonPath = '';
/**
* The class constructor. We get our firebase database ready here.
*
* @var string
*/
public function __construct() {
$this->secretKeyJsonPath = Engine::$secretKeyPath;
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__ . $this->secretKeyJsonPath);
$this->firebase = (new Factory)->withServiceAccount($serviceAccount)->create();
$this->database = $this->firebase->getDatabase();
}
/**
* Set the path to your Google service account service key json file
*
* @param $keyJsonFilePath
* @return $this
*/
public function setSecretKeyJsonFilePath($keyJsonFilePath)
{
$this->secretKeyJsonPath = $keyJsonFilePath;
return $this;
}
/**
* Use library dependent factory
*
* @return \Kreait\Firebase
*/
public function factory()
{
return $this->firebase;
}
/**
* The function creates a table in the firebase datastore.
*
* @param string $table
* @return $this
*/
public function setTable(string $table)
{
$this->table = $table;
return $this;
}
/**
* Set the primary key to be used for crud actions
*
* @param string $primaryKey
* @return $this
*/
public function setPrimaryKey(string $primaryKey)
{
$this->primaryKey = $primaryKey;
return $this;
}
/**
* This function returns all records
*
* @return mixed
*/
public function getRecords(){
return $this->database->getReference($this->table)->getValue();
}
/**
* This function returns a specific record by id
*
* @param int $recordID
* @return null|array
*/
public function getRecord(int $recordID)
{
foreach ($this->getRecords() as $key => $record) {
if ($record[$this->primaryKey] == $recordID) {
return $record;
}
}
return null;
}
/**
* This function adds new records|record
* It will return this or inserted records if $returnData is set true.
*
* @param array $data
* @param bool $returnData
* @return $this|mixed
* @throws \Exception
*/
public function insertRecord(array $data, $returnData = false)
{
$countedRecords = count($this->getRecords());
$data[$this->primaryKey] = $countedRecords > 1 ? $countedRecords + 1 : 1;
$this->database->getReference()
->getChild($this->table)
->getChild($countedRecords)
->set($data);
if ($returnData) {
return $this->getRecord($data[$this->primaryKey]);
}
return $this;
}
/**
* This function updates a record
* It will return updated records
*
* @param int $recordID
* @param array $data
* @return array
*/
public function updateRecord(int $recordID, array $data)
{
foreach ($this->getRecords() as $key => $record) {
if ($recordID == $record[$this->primaryKey]) {
foreach ($data as $dataKey => $dataValue) {
if (isset($record[$dataKey])) {
$record[$dataKey] = $dataValue;
}
}
$this->database->getReference()
->getChild($this->table)
->getChild($key)
->set($record);
}
}
return $this->getRecord($recordID);
}
/**
* This function deletes a record from your database.
* It will return boolean after action was commited
*
* @param int $recordID
* @return bool
*/
public function deleteRecord(int $recordID) {
foreach ($this->getRecords() as $key => $record) {
if ($record['id'] == $recordID) {
$this->database->getReference()
->getChild($this->table)
->getChild($key)
->set(null);
return true;
}
}
return false;
}
}