-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollection.php
213 lines (199 loc) · 5.53 KB
/
Collection.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Netesy\Gringotts;;
/**
* Class: \Netesy\Gringotts\Collection
* =============================================================================
* This class represents a collection. Used by the Db class.
*/
class Collection
{
/**
* Property: db
* =========================================================================
* This is where we keep our own copy of the database class, as a reference.
*/
private $db;
/**
* Property: path
* =========================================================================
* This is where we keep track of the path to the collection.
*/
private $path = '';
/**
* Method: __construct
* =========================================================================
* This sets the db and root for the collection.
*
* Parameters:
* -------------------------------------------------------------------------
* $db
* $root
*
* Throws:
* -------------------------------------------------------------------------
* n/a
*
* Returns:
* -------------------------------------------------------------------------
* void
*/
public function __construct(&$db, $root)
{
$this->db = &$db;
$this->path = '/'.$root;
}
/**
* Method: __get
* =========================================================================
* This allows us to implicitly set the collection path.
*
* Parameters:
* -------------------------------------------------------------------------
* $value - The next folder.
*
* Throws:
* -------------------------------------------------------------------------
* n/a
*
* Returns:
* -------------------------------------------------------------------------
* self for chaining
*/
public function __get($value)
{
$this->path = $this->path.'/'.$value;
return $this;
}
/**
* Method: create
* =========================================================================
* This is how you add data to the database
*
* Parameters:
* -------------------------------------------------------------------------
* $data - This must be an array, the data you would like to store.
*
* Throws:
* -------------------------------------------------------------------------
* n/a
*
* Returns:
* -------------------------------------------------------------------------
* boolean
*/
public function create($data)
{
// Pass on the request to the driver
return $this->db->getBackend()->create($this->path, $data);
}
// Alias method
public function insert($data)
{
return $this->create($data);
}
/**
* Method: read
* =========================================================================
* This is how you read data from the database
*
* Parameters:
* -------------------------------------------------------------------------
* $query - This must be an array, an empty array means "everything".
* $values - This must be an array
*
* Throws:
* -------------------------------------------------------------------------
* n/a
*
* Returns:
* -------------------------------------------------------------------------
* array
*/
public function read($query = array(), $values = array())
{
// Pass on the request to the driver
return $this->db->getBackend()->read($this->path, $query, $values);
}
// Alias method
public function find($query = array(), $values = array())
{
return $this->read($query, $values);
}
// Alias method
// TODO: WTF Brad shouldn't this be an alias for create???
public function save($query = array(), $values = array())
{
return $this->read($query, $values);
}
/**
* Method: update
* =========================================================================
* This is how you update data in the database
*
* Parameters:
* -------------------------------------------------------------------------
* $what - This is passed on to the read function
* $with - This is the new values
*
* Throws:
* -------------------------------------------------------------------------
* n/a
*
* Returns:
* -------------------------------------------------------------------------
* array
*/
public function update($what = array(), $with)
{
// Pass on the request to the driver
return $this->db->getBackend()->update($this->path, $what, $with);
}
/**
* Method: delete
* =========================================================================
* This is how you delete data in the database
*
* Parameters:
* -------------------------------------------------------------------------
* $what - This is passed on to the read function
*
* Throws:
* -------------------------------------------------------------------------
* n/a
*
* Returns:
* -------------------------------------------------------------------------
* array
*/
public function delete($what = array())
{
// Pass on the request to the driver
return $this->db->getBackend()->delete($this->path, $what);
}
// Alias method
public function remove($what = array())
{
return $this->delete($what);
}
/**
* Method: exists
* =========================================================================
* Does the collection currently exist?
*
* Parameters:
* -------------------------------------------------------------------------
* n/a
*
* Throws:
* -------------------------------------------------------------------------
* n/a
*
* Returns:
* -------------------------------------------------------------------------
* boolean
*/
public function exists()
{
return is_dir($this->db->getRootPath().$this->path);
}
}