diff --git a/CHANGELOG.md b/CHANGELOG.md index 026e4b2..856a4e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/Database.php b/src/Database.php index 559b8cb..71fc178 100644 --- a/src/Database.php +++ b/src/Database.php @@ -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 @@ -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; + } } diff --git a/src/Query.php b/src/Query.php index 3712a29..a78f323 100644 --- a/src/Query.php +++ b/src/Query.php @@ -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)); } } diff --git a/src/QueryLogic.php b/src/QueryLogic.php index e573d86..df0795a 100644 --- a/src/QueryLogic.php +++ b/src/QueryLogic.php @@ -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); @@ -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 * @@ -81,7 +89,7 @@ public function run() } $this->loadDocuments(); - + if ($predicates !== 'findAll') { $this->documents = $this->filter($this->documents, $predicates); diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index e187e71..9535700 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -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 = 'john@example.com'; + $user->save(); + } + + $r=$db->first(); + $names=array_keys($r); + $this->assertEquals($names, $db->getColumns()); + } } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 1589e4a..7462cbc 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -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@addres.com']); + $user2 = $db->get('obj2')->save(['name' => 'Jenny','user_profile_is_some_query'=>'data','email'=>'email@addres.com']); + $user3 = $db->get('obj3')->save(['name' => 'Cyrus','user_profile_is_some_query'=>'data','email'=>'email@addres.com']); + + // check with one input + $test1 = $db->query()->whereName('Bob')->first(); + $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; + $this->assertEquals($expected, $test1); + + // check with two input + $test1 = $db->query()->whereEmail('==','email@addres.com')->first(); + $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; + $this->assertEquals($expected, $test1); + + // check with two input withour query() + $test1 = $db->whereEmail('==','email@addres.com')->first(); + $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; + $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@addres.com']); + $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@addres.com']); + $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@addres.com']); + $this->expectException(\BadMethodCallException::class); + $db->query()->where_Name('Bob')->first(); + } }