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

Commit 19de7a7

Browse files
committed
Fixed query sorting which now allows for "natural order", Uses php strnatcasecmp(). Added argument results( false ) - false on results() method that allows it to return the full document object or (by default = true) only the document data. Added argument first( false ) - false on first() method that allows it to return the full document object or (by default = true) only the document data. Minor additions to the documentation, changelog and tests.
1 parent 9d09cda commit 19de7a7

File tree

6 files changed

+103
-14
lines changed

6 files changed

+103
-14
lines changed

CHANGELOG.md

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

4+
### 12/10/2017 - 1.0.10
5+
* Fixed query `sort` which allows for "natural order", issues before would assume "1" and "10" are equal in value, but this has been resolved with this update. Uses php `strnatcasecmp()`
6+
* Added argument `results( false )` - `false` on `results()` method that allows it to return the full document object or (by default = `true`) only the document data.
7+
* Added argument `first( false )` - `false` on `first()` method that allows it to return the full document object or (by default = `true`) only the document data.
8+
* Minor additions to the documentation.
9+
410
### 09/09/2017 - 1.0.9
511
* Fixed `customFilter` on #5 issue with array keys not properly resetting.
612
* Improved speed of `filter()` since it was running the function closure function twice

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Use `composer require tmarois/filebase`
3434

3535
If you want to modify the `composer.json` manually, add `"tmarois/filebase" : "^1.0"` to your `required`
3636

37-
You do not need to use composer, just download the files, and include it within your application, it does not have any dependencies, you will just need to keep it updated with any future releases.
37+
You do not need to use composer, just download the files, and include it within your application, it does not have any dependencies, you will just need to keep it updated with any future releases.
3838

3939
## Usage
4040

@@ -350,8 +350,8 @@ $usersWithGmail = $db->query()
350350
->results();
351351

352352

353-
// this will get the user that has the most page views (and returning 1 result)
354-
$user = $db->query()->orderBy('page_views', 'DESC')->first();
353+
// this will return the first user in the list based on ascending order of user name.
354+
$user = $db->query()->orderBy('name', 'ASC')->first();
355355
// print out the user name
356356
echo $user['name'];
357357

@@ -361,7 +361,7 @@ $users = $db->query()->where('email','REGEX','/[a-z\d._%+-]+@[a-z\d.-]+\.[a-z]{2
361361

362362
```
363363

364-
To run the query use `results()` or `resultDocuments()` or `first()` if you want only the first item
364+
To run the query use `results()` or `resultDocuments()` or if you only want to return the first item use `first()`
365365

366366
### Methods:
367367

@@ -374,8 +374,8 @@ To run the query use `results()` or `resultDocuments()` or `first()` if you want
374374
These methods execute the query and return results *(do not try to use them together)*
375375

376376
- `first()` Returns only the first query result (if you only want to return 1 item)
377-
- `results()` This will return all the document data as an array.
378-
- `resultDocuments()` This will return all the document objects
377+
- `results()` This will return all the document data as an array. Passing the argument of `false` will be the same as `resultDocuments()` (returning the full document), but default it's set to `true` and only returns the data array of your documents.
378+
- `resultDocuments()` This will return all the document objects, or you can do `results(false)` which is the alias.
379379

380380
### Comparison Operators:
381381

src/Database.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Database
1010
* Stores the version of Filebase
1111
* use $db->getVersion()
1212
*/
13-
const VERSION = '1.0.9';
13+
const VERSION = '1.0.10';
1414

1515

1616
//--------------------------------------------------------------------
@@ -76,6 +76,7 @@ public function version()
7676
* Then returns you a list of those documents.
7777
*
7878
* @param bool $include_documents (include all document objects in array)
79+
* @param bool $data_only (if true only return the documents data not the full object)
7980
*
8081
* @return array $items
8182
*/

src/Query.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ protected function addPredicate($logic,$arg)
119119
/**
120120
* ->getDocuments()
121121
*
122+
*
122123
*/
123124
public function getDocuments()
124125
{
@@ -132,10 +133,17 @@ public function getDocuments()
132133
/**
133134
* ->results()
134135
*
136+
* @param bool $data_only - default:true (if true only return the documents data not the full object)
137+
*
135138
*/
136-
public function results()
139+
public function results( $data_only = true )
137140
{
138-
return parent::run()->toArray();
141+
if ($data_only === true)
142+
{
143+
return parent::run()->toArray();
144+
}
145+
146+
return $this->resultDocuments();
139147
}
140148

141149

@@ -158,10 +166,18 @@ public function resultDocuments()
158166
/**
159167
* ->first()
160168
*
169+
* @param bool $data_only - default:true (if true only return the documents data not the full object)
170+
*
161171
*/
162-
public function first()
172+
public function first( $data_only = true )
163173
{
164-
$results = parent::run()->toArray();
174+
if ($data_only === true)
175+
{
176+
$results = parent::run()->toArray();
177+
return current($results);
178+
}
179+
180+
$results = parent::run()->getDocuments();
165181
return current($results);
166182
}
167183

src/QueryLogic.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,22 @@ protected function sort()
190190

191191
if ($sortBy == 'DESC')
192192
{
193-
return $b->field($orderBy) <=> $a->field($orderBy);
193+
$propA = $a->field($orderBy);
194+
$propB = $b->field($orderBy);
195+
196+
// strnatcasecmp allows us to test in "natural" order
197+
return strnatcasecmp($propB, $propA) <=> strnatcasecmp($propA, $propB);
198+
199+
// return $b->field($orderBy) <=> $a->field($orderBy);
194200
}
195201

196202
return $a->field($orderBy) <=> $b->field($orderBy);
203+
197204
});
198205

199206
}
200207

201208

202-
203209
//--------------------------------------------------------------------
204210

205211

tests/QueryTest.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ public function testWhereIn()
432432
'tags' => [
433433
'social','network'
434434
]
435+
],
436+
'Microsoft'=>[
437+
'tags' => [
438+
'windows','xbox','search'
439+
]
435440
]
436441
];
437442

@@ -445,11 +450,26 @@ public function testWhereIn()
445450

446451

447452
// test that they are ordered by name ASC (check first, second, and last)
448-
$test1 = $db->query()->where('tags','IN','search')->first();
453+
$test1 = $db->query()->where('tags','IN','display')->first();
449454
$test2 = $db->query()->where('tags','IN','network')->first();
455+
$test3 = $db->query()->where('tags','IN','windows')->first();
456+
$test4 = $db->query()->where('tags','IN','search')->results();
450457

458+
// testing the object return boolean argument
459+
$test5 = $db->query()->where('tags','IN','search')->results(false);
460+
$test6 = $db->query()->where('tags','IN','search')->results(true);
461+
462+
// make sure the results equal the right names
451463
$this->assertEquals('Google', $test1['name']);
452464
$this->assertEquals('Facebook', $test2['name']);
465+
$this->assertEquals('Microsoft', $test3['name']);
466+
467+
// check if the method createdAt() exists or not based on the argument boolean
468+
$this->assertEquals(true, method_exists($test5[0], 'createdAt'));
469+
$this->assertEquals(false, method_exists($test6[0], 'createdAt'));
470+
471+
// check if the results = 2
472+
$this->assertEquals(2, count($test4));
453473

454474
// this will test the IN clause if name matches one of these
455475
$test3 = $db->query()->where('name','IN',['Google','Facebook'])->results();
@@ -508,6 +528,11 @@ public function testWithoutWhere()
508528

509529
// test that they are ordered by name ASC (check first, second, and last)
510530
$test1 = $db->query()->orderBy('name', 'DESC')->first();
531+
$test2 = $db->query()->orderBy('name', 'DESC')->first( false );
532+
$test3 = $db->query()->orderBy('name', 'DESC')->first( true );
533+
534+
$this->assertEquals(true, method_exists($test2, 'createdAt'));
535+
$this->assertEquals(false, method_exists($test3, 'createdAt'));
511536

512537
$this->assertEquals('Yahoo', $test1['name']);
513538

@@ -613,6 +638,41 @@ public function testingMissingQueryArguments()
613638
}
614639

615640

641+
642+
//--------------------------------------------------------------------
643+
644+
645+
/**
646+
* testUserNameQuery()
647+
*
648+
*
649+
*/
650+
public function testUserNameQuery()
651+
{
652+
$db = new \Filebase\Database([
653+
'dir' => __DIR__.'/databases/users_names',
654+
'cache' => false
655+
]);
656+
657+
$db->flush(true);
658+
659+
for ($x = 1; $x <= 10; $x++)
660+
{
661+
$user = $db->get(uniqid());
662+
$user->name = 'John '.$x;
663+
$user->contact['email'] = 'john@john.com';
664+
$user->save();
665+
}
666+
667+
$userAccount = $db->query()->orderBy('name', 'DESC')->first();
668+
669+
$this->assertEquals('John 10', $userAccount['name']);
670+
671+
$db->flush(true);
672+
}
673+
674+
675+
616676
//--------------------------------------------------------------------
617677

618678

0 commit comments

Comments
 (0)