forked from nashtgc/firebase-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsers.php
65 lines (48 loc) · 1.74 KB
/
Users.php
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
<?php
require_once './vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
class Users {
protected $database;
protected $dbname = 'users';
public function __construct(){
$acc = ServiceAccount::fromJsonFile(__DIR__ . '/secret/arthursystems-php-tutorials-0066e2bd5954.json');
$firebase = (new Factory)->withServiceAccount($acc)->create();
$this->database = $firebase->getDatabase();
}
public function get(int $userID = NULL){
if (empty($userID) || !isset($userID)) { return FALSE; }
if ($this->database->getReference($this->dbname)->getSnapshot()->hasChild($userID)){
return $this->database->getReference($this->dbname)->getChild($userID)->getValue();
} else {
return FALSE;
}
}
public function insert(array $data) {
if (empty($data) || !isset($data)) { return FALSE; }
foreach ($data as $key => $value){
$this->database->getReference()->getChild($this->dbname)->getChild($key)->set($value);
}
return TRUE;
}
public function delete(int $userID) {
if (empty($userID) || !isset($userID)) { return FALSE; }
if ($this->database->getReference($this->dbname)->getSnapshot()->hasChild($userID)){
$this->database->getReference($this->dbname)->getChild($userID)->remove();
return TRUE;
} else {
return FALSE;
}
}
}
$users = new Users();
//var_dump($users->insert([
// '1' => 'John',
// '2' => 'Doe',
// '3' => 'Smith'
//]));
//var_dump($users->get(1));
//var_dump($users->delete(2));
var_dump($users->insert([
'1' => 'John Doe',
]));