forked from nashtgc/firebase-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirestore.php
134 lines (123 loc) · 3.91 KB
/
Firestore.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
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
<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* Created by Arthur Mann.
* Date: 10/05/2019
* Time: 21:13
* @important Do not forget install Firestore dependency
* $ composer require google/cloud-firestore
*/
require_once 'vendor/autoload.php';
use Google\Cloud\Firestore\FirestoreClient;
class Firestore {
protected $db;
protected $name;
public function __construct(string $collection)
{
$this->db = new FirestoreClient([
'projectId' => 'asystems-7692e'
]);
$this->name = $collection;
}
/**
* Get document and all data with checking for exists
* @param string $name
* @return array|null|string
*/
public function getDocument(string $name)
{
try {
if (empty($name)) throw new Exception('Document name missing');
if ($this->db->collection($this->name)->document($name)->snapshot()->exists()) {
return $this->db->collection($this->name)->document($name)->snapshot()->data();
} else {
throw new Exception('Document are not exists');
}
} catch (Exception $exception) {
return $exception->getMessage();
}
}
/**
* Get document with where condition
* @param string $field
* @param string $operator
* @param $value
* @return array
*/
public function getWhere(string $field, string $operator, $value)
{
$arr = [];
$query = $this->db->collection($this->name)->where($field, $operator, $value)->documents()->rows();
if (!empty($query)) {
foreach ($query as $value) {
$arr[] = $value->data();
}
}
return $arr;
}
/**
* Create new document with data
* @param string $name
* @param array $data
* @return bool|string
*/
public function newDocument(string $name, array $data = [])
{
try {
$this->db->collection($this->name)->document($name)->create($data);
return true;
} catch (Exception $exception){
return $exception->getMessage();
}
}
/**
* Create new collection
* @param string $name
* @param string $doc_name
* @param array $data
* @return bool|string
*/
public function newCollection(string $name, string $doc_name, array $data = [])
{
try {
$this->db->collection($name)->document($doc_name)->create($data);
return true;
} catch (Exception $exception) {
return $exception->getMessage();
}
}
/**
* Drop exists document in collection
* @param string $name
* @return void
*/
public function dropDocument(string $name)
{
$this->db->collection($this->name)->document($name)->delete();
}
/**
* Drop exists collection
* @param string $name
* @return void
*/
public function dropCollection(string $name)
{
$documents = $this->db->collection($name)->limit(1)->documents();
while (!$documents->isEmpty()) {
foreach ($documents as $item) {
$item->reference()->delete();
}
}
}
}