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

Commit

Permalink
Added new method first() and removed unused matchDocuments() and adde…
Browse files Browse the repository at this point in the history
…d query IN testing, Fixed predicate exception handling. (placing the logic inside predicate instead of query class), added more testing coverage.
  • Loading branch information
timothymarois committed Aug 5, 2017
1 parent 47910aa commit 0523914
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 75 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

### 08/05/2017 - 1.0.4
* Added `first()` (if you want to only return the first array of the query result)
* Ability to use Queries without needing `where()`, can now use queries to find all and order results
* Fixed Predicate Exceptions for bad query arguments (now correctly parsing them)

### 08/05/2017 - 1.0.3
* Added `orderBy()` (sorting field and direction `ASC` and `DESC`)
* Added `limit()` Limit results returned, includes Limit and Offset options.
Expand Down
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,33 @@ $users = $db->query()->where('status.language.english','=','blocked')->results()
// Limit Example: Same query as above, except we only want to limit the results to 10
$users = $db->query()->where('status.language.english','=','blocked')->limit(10)->results();



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

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

// or just order the results by email address
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->orderBy('email', 'ASC')->results();
$usersWithGmail = $db->query()
->where('email','LIKE','@gmail.com')
->orderBy('email', 'ASC')
->results();


// this will get the user that has the most page views (and returning 1 result)
$user = $db->query()->orderBy('page_views', 'DESC')->first();
// print out the user name
echo $user['name'];


```

To run the query use `results()` or `resultDocuments()`
To run the query use `results()` or `resultDocuments()` or `first()` if you want only the first item

### Methods:

Expand All @@ -345,6 +360,10 @@ To run the query use `results()` or `resultDocuments()`
- `orWhere()` *optional* see `where()`, this uses the logical `OR`
- `limit()` *optional* limit/offset results `limit($number, $offset)`
- `orderBy()` *optional* orders the results `orderBy($field, $direction)`, `$direction` = `ASC` or `DESC`

These methods execute the query and return results *(do not try to use them together)*

- `first()` Returns only the first query result (if you only want to return 1 item)
- `results()` This will return all the document data as an array.
- `resultDocuments()` This will return all the document objects

Expand Down
30 changes: 30 additions & 0 deletions src/Predicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ class Predicate
*/
public function add($logic,$arg)
{
if (!is_array($arg))
{
throw new \InvalidArgumentException('Predicate Error: argument passed must be type of array');
}

if (count($arg) == 1)
{
if (isset($arg[0]) && is_array($arg[0]))
{
foreach($arg[0] as $key => $value)
{
if ($value == '') continue;

$arg = $this->formatWhere($key, $value);
}
}
}

if (count($arg) != 3)
{
throw new \InvalidArgumentException('Predicate Error: Must have 3 arguments passed - '.count($arg).' given');
Expand All @@ -64,6 +82,18 @@ public function add($logic,$arg)
}


//--------------------------------------------------------------------

/**
* formatWhere
*
*/
protected function formatWhere($key, $value)
{
return [$key,'==',$value];
}


//--------------------------------------------------------------------


Expand Down
44 changes: 15 additions & 29 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,81 +109,67 @@ public function orderBy(string $field, string $sort)
*/
protected function addPredicate($logic,$arg)
{
if (count($arg) == 3)
{
$this->predicate->add($logic, $arg);
}

if (count($arg) == 1)
{
if (isset($arg[0]) && count($arg[0]))
{
foreach($arg[0] as $key => $value)
{
if ($value == '') continue;

$this->predicate->add($logic, $this->formatWhere($key, $value));
}
}
}
$this->predicate->add($logic, $arg);
}


//--------------------------------------------------------------------


/**
* formatWhere
* ->getDocuments()
*
*/
protected function formatWhere($key, $value)
public function getDocuments()
{
return [$key,'==',$value];
return $this->documents;
}


//--------------------------------------------------------------------


/**
* ->getDocuments()
* ->results()
*
*/
public function getDocuments()
public function results()
{
return $this->documents;
return parent::run()->toArray();
}


//--------------------------------------------------------------------


/**
* ->results()
* ->resultDocuments()
*
*/
public function results()
public function resultDocuments()
{
return parent::run()->toArray();
return parent::run()->getDocuments();
}


//--------------------------------------------------------------------


/**
* ->resultDocuments()
* ->first()
*
*/
public function resultDocuments()
public function first()
{
return parent::run()->getDocuments();
$results = parent::run()->toArray();
return current($results);
}


//--------------------------------------------------------------------



/**
* toArray
*
Expand Down
70 changes: 27 additions & 43 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,44 @@ public function run()
$this->documents = [];
$cached_documents = false;

if (!empty($predicates))
if (empty($predicates))
{
if ($this->cache !== false)
{
$this->cache->setKey(json_encode($predicates));
$predicates = 'findAll';
}

if ($cached_documents = $this->cache->get())
{
$this->documents = $cached_documents;
if ($this->cache !== false)
{
$this->cache->setKey(json_encode($predicates));

$this->sort();
$this->offsetLimit();
if ($cached_documents = $this->cache->get())
{
$this->documents = $cached_documents;

return $this;
}
$this->sort();
$this->offsetLimit();

return $this;
}
}

$this->documents = $this->database->findAll(true,false);
$this->documents = $this->database->findAll(true,false);

if ($predicates !== 'findAll')
{
$this->documents = $this->filter($this->documents, $predicates);
}

if ($this->cache !== false)
if ($this->cache !== false)
{
if ($cached_documents === false)
{
if ($cached_documents === false)
$dsave = [];
foreach($this->documents as $document)
{
$dsave = [];
foreach($this->documents as $document)
{
$dsave[] = $document->getId();
}

$this->cache->store($dsave);
$dsave[] = $document->getId();
}

$this->cache->store($dsave);
}
}

Expand Down Expand Up @@ -193,28 +199,6 @@ protected function sort()
}


//--------------------------------------------------------------------


/**
* matchDocuments
*
*/
public function matchDocuments($documents, $field, $operator, $value)
{
$docs = [];

foreach($documents as $document)
{
if ($this->match($document, $field, $operator, $value)===true)
{
$docs[] = $document;
}
}

return $docs;
}


//--------------------------------------------------------------------

Expand Down
34 changes: 34 additions & 0 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,40 @@ public function testSetValue()
//--------------------------------------------------------------------


/**
* testIssetUnsetUnknown()
*
* TEST CASE:
* - Check if property isset
* - Unset property and see if it now returns null
*
*/
public function testIssetUnset()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'cache' => false
]);

$db->flush(true);

$test = $db->get('test2');
$test->key = 'value';

$this->assertEquals('value', $test->key);

$this->assertEquals(1, isset($test->key));

unset($test->key);

$this->assertEquals(null, ($test->key));

}


//--------------------------------------------------------------------


public function testArraySetValueSave()
{
$db = new \Filebase\Database([
Expand Down
Loading

0 comments on commit 0523914

Please sign in to comment.