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

add ability to search with where* like whereName whereCountry ... #51

Open
wants to merge 18 commits into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Change Log
==========

### 02/24/2019 - 1.0.24
* Merged [Pull Request](https://github.com/filebase/Filebase/pull/50) Fixed [bug](https://github.com/filebase/Filebase/issues/41) returning unexpected results.

### 02/24/2019 - 1.0.23
* Merged [Pull Request](https://github.com/filebase/Filebase/pull/49) Added support for order by multiple columns
* Merged [Pull Request](https://github.com/filebase/Filebase/pull/46) Added ability to query document ids (internal id)
Expand Down
18 changes: 15 additions & 3 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Database
* Stores the version of Filebase
* use $db->getVersion()
*/
const VERSION = '1.0.23';
const VERSION = '1.0.24';

/**
* $config
Expand Down Expand Up @@ -422,11 +422,23 @@ public function __call($method,$args)
return $this->$method(...$args);
}

if(method_exists(Query::class,$method)) {
return (new Query($this))->$method(...$args);
if(is_callable([$obj=(new Query($this)),$method])) {
return $obj->$method(...$args);
}

throw new \BadMethodCallException("method {$method} not found on 'Database::class' and 'Query::class'");
}
/**
* getColumns
*/
public function getColumns()
{
$items=[];
foreach(array_keys($this->first()) as $item)
{
$items[]=strtolower($item);
}
return $items;
}

}
47 changes: 42 additions & 5 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,52 @@ public function delete($input = null)
foreach($items as $item)
{
if (is_object($input)) {
$condition = $input($item);

if ($condition) {
if ($input($item)===true) {
$item->delete();
}
continue;
}
else {
$item->delete();
$item->delete();
}
}
/**
* call magic method
*/
public function __call($method,$args)
{

// it will just hanle Dynamic where
// other exist methods will handle with system on call

if($name=$this->sanatizeWhere($method))
{
$names=$this->database->getColumns();
if(in_array($name,$names))
{
if(count($args) == 1)
{
return $this->where($name,'==',$args[0]);
}
if(count($args) == 2)
{
return $this->where($name,$args[0],$args[1]);
}
throw new \InvalidArgumentException("input count must be 1 or 2 " . count($args) . "given");
}
}
throw new \BadMethodCallException('method not found on '.__CLASS__);

}
/**
* sanatizeWhere
* find where* with regex
*/
function sanatizeWhere($method, $parameters=0)
{
if(strpos('_',$method))
{
return false;
}
return strtolower(substr($method, 5));
}
}
14 changes: 11 additions & 3 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ public function __construct(Database $database)
}
}

/**
* loadDocuments
*
*/
private function loadDocuments()
{
$predicates = $this->predicate->get();

if (empty($predicates))
{
$predicates = 'findAll';
}
if ($this->cache===false)
{
$this->documents = $this->database->findAll(true,false);
Expand All @@ -60,11 +67,12 @@ private function loadDocuments()

$this->sort();
$this->offsetLimit();
return $this;
return $this;
}
$this->documents = $this->database->findAll(true,false);
return $this;
}

/**
* run
*
Expand All @@ -81,7 +89,7 @@ public function run()
}

$this->loadDocuments();

if ($predicates !== 'findAll')
{
$this->documents = $this->filter($this->documents, $predicates);
Expand Down
21 changes: 21 additions & 0 deletions tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,25 @@ public function test_must_return_array_on_select_an_culomn_from_cache()
$this->assertInternalType('string', $result_from_cache[0]['email']);
$db->flush(true);
}
public function test_must_return_name_of_culomns()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/saved',
'cache' => true
]);

$db->flush(true);

for ($x = 1; $x <= 10; $x++)
{
$user = $db->get(uniqid());
$user->name = 'John';
$user->email = '[email protected]';
$user->save();
}

$r=$db->first();
$names=array_keys($r);
$this->assertEquals($names, $db->getColumns());
}
}
80 changes: 80 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1261,4 +1261,84 @@ public function testWhereInUsingDocId()

$db->flush(true);
}
public function test_must_detect_and_return_culomn_with_regex()
{
$query=new Query(new Database);
$result=$query->sanatizeWhere('whereName');
$this->assertEquals('name',$result);

$result=$query->sanatizeWhere('whereUser');
$this->assertEquals('user',$result);

}
public function test_must_call_method_on_regex_matched_culumn()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_1',
'cache' => false
]);

$db->flush(true);

$user1 = $db->get('obj1')->save(['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'[email protected]']);
$user2 = $db->get('obj2')->save(['name' => 'Jenny','user_profile_is_some_query'=>'data','email'=>'[email protected]']);
$user3 = $db->get('obj3')->save(['name' => 'Cyrus','user_profile_is_some_query'=>'data','email'=>'[email protected]']);

// check with one input
$test1 = $db->query()->whereName('Bob')->first();
$expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'[email protected]'];
$this->assertEquals($expected, $test1);

// check with two input
$test1 = $db->query()->whereEmail('==','[email protected]')->first();
$expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'[email protected]'];
$this->assertEquals($expected, $test1);

// check with two input withour query()
$test1 = $db->whereEmail('==','[email protected]')->first();
$expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'[email protected]'];
$this->assertEquals($expected, $test1);


$db->flush(true);
}
public function test_must_return_exception_on_none_exist_method()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_1',
'cache' => false
]);

$db->flush(true);

$user1 = $db->get('obj1')->save(['name' => 'Bob','email'=>'[email protected]']);
$this->expectException(\BadMethodCallException::class);
$db->query()->wherenone('name')->first();
}
public function test_must_return_exception_on_empty_whereName_call_method()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_1',
'cache' => false
]);

$db->flush(true);

$user1 = $db->get('obj1')->save(['name' => 'Bob','email'=>'[email protected]']);
$this->expectException(\InvalidArgumentException::class);
$db->query()->whereName()->first();
}
public function test_must_return_exception_on_where_contain_underscore()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_1',
'cache' => false
]);

$db->flush(true);

$user1 = $db->get('obj1')->save(['name' => 'Bob','email'=>'[email protected]']);
$this->expectException(\BadMethodCallException::class);
$db->query()->where_Name('Bob')->first();
}
}