Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 0523914

Browse files
committed
Added new method first() and removed unused matchDocuments() and added query IN testing, Fixed predicate exception handling. (placing the logic inside predicate instead of query class), added more testing coverage.
1 parent 47910aa commit 0523914

File tree

7 files changed

+344
-75
lines changed

7 files changed

+344
-75
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Change Log
22
==========
33

4+
### 08/05/2017 - 1.0.4
5+
* Added `first()` (if you want to only return the first array of the query result)
6+
* Ability to use Queries without needing `where()`, can now use queries to find all and order results
7+
* Fixed Predicate Exceptions for bad query arguments (now correctly parsing them)
8+
49
### 08/05/2017 - 1.0.3
510
* Added `orderBy()` (sorting field and direction `ASC` and `DESC`)
611
* Added `limit()` Limit results returned, includes Limit and Offset options.

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,33 @@ $users = $db->query()->where('status.language.english','=','blocked')->results()
325325
// Limit Example: Same query as above, except we only want to limit the results to 10
326326
$users = $db->query()->where('status.language.english','=','blocked')->limit(10)->results();
327327

328+
329+
328330
// Query LIKE Example: how about find all users that have a gmail account?
329331
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->results();
330332

331333
// OrderBy Example: From the above query, what if you want to order the results by nested array (profile name?)
332-
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->orderBy('profile.name', 'ASC')->results();
334+
$usersWithGmail = $db->query()
335+
->where('email','LIKE','@gmail.com')
336+
->orderBy('profile.name', 'ASC')
337+
->results();
333338

334339
// or just order the results by email address
335-
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->orderBy('email', 'ASC')->results();
340+
$usersWithGmail = $db->query()
341+
->where('email','LIKE','@gmail.com')
342+
->orderBy('email', 'ASC')
343+
->results();
344+
345+
346+
// this will get the user that has the most page views (and returning 1 result)
347+
$user = $db->query()->orderBy('page_views', 'DESC')->first();
348+
// print out the user name
349+
echo $user['name'];
350+
336351

337352
```
338353

339-
To run the query use `results()` or `resultDocuments()`
354+
To run the query use `results()` or `resultDocuments()` or `first()` if you want only the first item
340355

341356
### Methods:
342357

@@ -345,6 +360,10 @@ To run the query use `results()` or `resultDocuments()`
345360
- `orWhere()` *optional* see `where()`, this uses the logical `OR`
346361
- `limit()` *optional* limit/offset results `limit($number, $offset)`
347362
- `orderBy()` *optional* orders the results `orderBy($field, $direction)`, `$direction` = `ASC` or `DESC`
363+
364+
These methods execute the query and return results *(do not try to use them together)*
365+
366+
- `first()` Returns only the first query result (if you only want to return 1 item)
348367
- `results()` This will return all the document data as an array.
349368
- `resultDocuments()` This will return all the document objects
350369

src/Predicate.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ class Predicate
4343
*/
4444
public function add($logic,$arg)
4545
{
46+
if (!is_array($arg))
47+
{
48+
throw new \InvalidArgumentException('Predicate Error: argument passed must be type of array');
49+
}
50+
51+
if (count($arg) == 1)
52+
{
53+
if (isset($arg[0]) && is_array($arg[0]))
54+
{
55+
foreach($arg[0] as $key => $value)
56+
{
57+
if ($value == '') continue;
58+
59+
$arg = $this->formatWhere($key, $value);
60+
}
61+
}
62+
}
63+
4664
if (count($arg) != 3)
4765
{
4866
throw new \InvalidArgumentException('Predicate Error: Must have 3 arguments passed - '.count($arg).' given');
@@ -64,6 +82,18 @@ public function add($logic,$arg)
6482
}
6583

6684

85+
//--------------------------------------------------------------------
86+
87+
/**
88+
* formatWhere
89+
*
90+
*/
91+
protected function formatWhere($key, $value)
92+
{
93+
return [$key,'==',$value];
94+
}
95+
96+
6797
//--------------------------------------------------------------------
6898

6999

src/Query.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,81 +109,67 @@ public function orderBy(string $field, string $sort)
109109
*/
110110
protected function addPredicate($logic,$arg)
111111
{
112-
if (count($arg) == 3)
113-
{
114-
$this->predicate->add($logic, $arg);
115-
}
116-
117-
if (count($arg) == 1)
118-
{
119-
if (isset($arg[0]) && count($arg[0]))
120-
{
121-
foreach($arg[0] as $key => $value)
122-
{
123-
if ($value == '') continue;
124-
125-
$this->predicate->add($logic, $this->formatWhere($key, $value));
126-
}
127-
}
128-
}
112+
$this->predicate->add($logic, $arg);
129113
}
130114

131115

132116
//--------------------------------------------------------------------
133117

134118

135119
/**
136-
* formatWhere
120+
* ->getDocuments()
137121
*
138122
*/
139-
protected function formatWhere($key, $value)
123+
public function getDocuments()
140124
{
141-
return [$key,'==',$value];
125+
return $this->documents;
142126
}
143127

144128

145129
//--------------------------------------------------------------------
146130

147131

148132
/**
149-
* ->getDocuments()
133+
* ->results()
150134
*
151135
*/
152-
public function getDocuments()
136+
public function results()
153137
{
154-
return $this->documents;
138+
return parent::run()->toArray();
155139
}
156140

157141

158142
//--------------------------------------------------------------------
159143

160144

161145
/**
162-
* ->results()
146+
* ->resultDocuments()
163147
*
164148
*/
165-
public function results()
149+
public function resultDocuments()
166150
{
167-
return parent::run()->toArray();
151+
return parent::run()->getDocuments();
168152
}
169153

170154

171155
//--------------------------------------------------------------------
172156

173157

174158
/**
175-
* ->resultDocuments()
159+
* ->first()
176160
*
177161
*/
178-
public function resultDocuments()
162+
public function first()
179163
{
180-
return parent::run()->getDocuments();
164+
$results = parent::run()->toArray();
165+
return current($results);
181166
}
182167

183168

184169
//--------------------------------------------------------------------
185170

186171

172+
187173
/**
188174
* toArray
189175
*

src/QueryLogic.php

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,38 +60,44 @@ public function run()
6060
$this->documents = [];
6161
$cached_documents = false;
6262

63-
if (!empty($predicates))
63+
if (empty($predicates))
6464
{
65-
if ($this->cache !== false)
66-
{
67-
$this->cache->setKey(json_encode($predicates));
65+
$predicates = 'findAll';
66+
}
6867

69-
if ($cached_documents = $this->cache->get())
70-
{
71-
$this->documents = $cached_documents;
68+
if ($this->cache !== false)
69+
{
70+
$this->cache->setKey(json_encode($predicates));
7271

73-
$this->sort();
74-
$this->offsetLimit();
72+
if ($cached_documents = $this->cache->get())
73+
{
74+
$this->documents = $cached_documents;
7575

76-
return $this;
77-
}
76+
$this->sort();
77+
$this->offsetLimit();
78+
79+
return $this;
7880
}
81+
}
7982

80-
$this->documents = $this->database->findAll(true,false);
83+
$this->documents = $this->database->findAll(true,false);
84+
85+
if ($predicates !== 'findAll')
86+
{
8187
$this->documents = $this->filter($this->documents, $predicates);
88+
}
8289

83-
if ($this->cache !== false)
90+
if ($this->cache !== false)
91+
{
92+
if ($cached_documents === false)
8493
{
85-
if ($cached_documents === false)
94+
$dsave = [];
95+
foreach($this->documents as $document)
8696
{
87-
$dsave = [];
88-
foreach($this->documents as $document)
89-
{
90-
$dsave[] = $document->getId();
91-
}
92-
93-
$this->cache->store($dsave);
97+
$dsave[] = $document->getId();
9498
}
99+
100+
$this->cache->store($dsave);
95101
}
96102
}
97103

@@ -193,28 +199,6 @@ protected function sort()
193199
}
194200

195201

196-
//--------------------------------------------------------------------
197-
198-
199-
/**
200-
* matchDocuments
201-
*
202-
*/
203-
public function matchDocuments($documents, $field, $operator, $value)
204-
{
205-
$docs = [];
206-
207-
foreach($documents as $document)
208-
{
209-
if ($this->match($document, $field, $operator, $value)===true)
210-
{
211-
$docs[] = $document;
212-
}
213-
}
214-
215-
return $docs;
216-
}
217-
218202

219203
//--------------------------------------------------------------------
220204

tests/DocumentTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ public function testSetValue()
8181
//--------------------------------------------------------------------
8282

8383

84+
/**
85+
* testIssetUnsetUnknown()
86+
*
87+
* TEST CASE:
88+
* - Check if property isset
89+
* - Unset property and see if it now returns null
90+
*
91+
*/
92+
public function testIssetUnset()
93+
{
94+
$db = new \Filebase\Database([
95+
'dir' => __DIR__.'/databases',
96+
'cache' => false
97+
]);
98+
99+
$db->flush(true);
100+
101+
$test = $db->get('test2');
102+
$test->key = 'value';
103+
104+
$this->assertEquals('value', $test->key);
105+
106+
$this->assertEquals(1, isset($test->key));
107+
108+
unset($test->key);
109+
110+
$this->assertEquals(null, ($test->key));
111+
112+
}
113+
114+
115+
//--------------------------------------------------------------------
116+
117+
84118
public function testArraySetValueSave()
85119
{
86120
$db = new \Filebase\Database([

0 commit comments

Comments
 (0)