-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from jjgrainger/feature/add-scopes-methods
add addScopes method
- Loading branch information
Showing
10 changed files
with
264 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Changelog | ||
|
||
### v0.2.0 | ||
|
||
* Add `Query::addScope` method for custom scopes | ||
* Create `BootableTraits` trait | ||
* Create `QueriesPosts` trait |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Query\Concerns; | ||
|
||
use ReflectionClass; | ||
|
||
trait BootsTraits | ||
{ | ||
/** | ||
* Bootstrap bootable traits. | ||
* | ||
* @return void | ||
*/ | ||
public function bootTraits() | ||
{ | ||
// Get traits associated to the class. | ||
$traits = (new ReflectionClass(static::class))->getTraitNames(); | ||
|
||
// Loop over traits and call their bootable method. | ||
foreach ($traits as $trait) { | ||
// Create the bootable method string. | ||
$method = 'boot' . (new ReflectionClass($trait))->getShortName(); | ||
|
||
// Skip, if no bootable method available. | ||
if (!method_exists($this, $method)) { | ||
continue; | ||
} | ||
|
||
// Call the bootable method. | ||
$this->{$method}(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Query\Concerns; | ||
|
||
trait QueriesPosts | ||
{ | ||
/** | ||
* Add Post Scopes to the query on boot. | ||
* | ||
* @var array | ||
*/ | ||
protected function bootQueriesPosts() | ||
{ | ||
$scopes = [ | ||
new \Query\Scopes\Author, | ||
new \Query\Scopes\AuthorIn, | ||
new \Query\Scopes\AuthorNotIn, | ||
new \Query\Scopes\Comments, | ||
new \Query\Scopes\Meta, | ||
new \Query\Scopes\Order, | ||
new \Query\Scopes\OrderBy, | ||
new \Query\Scopes\Page, | ||
new \Query\Scopes\ParentIn, | ||
new \Query\Scopes\ParentNotIn, | ||
new \Query\Scopes\Password, | ||
new \Query\Scopes\Post, | ||
new \Query\Scopes\PostIn, | ||
new \Query\Scopes\PostNotIn, | ||
new \Query\Scopes\PostParent, | ||
new \Query\Scopes\PostStatus, | ||
new \Query\Scopes\PostType, | ||
new \Query\Scopes\PostsPerPage, | ||
new \Query\Scopes\Search, | ||
new \Query\Scopes\Taxonomy, | ||
]; | ||
|
||
foreach ($scopes as $scope) { | ||
$this->addScope($scope); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Query\Query; | ||
use Query\Builder; | ||
use Query\Scope; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class HasScopesTest extends TestCase | ||
{ | ||
public function test_query_has_bootable_scopes() | ||
{ | ||
$result = Query::search('test')->post_type('event')->getParameters(); | ||
|
||
$this->assertEquals(['s' => 'test', 'post_type' => 'event'], $result); | ||
} | ||
|
||
public function test_query_can_add_scopes_with_scope_class() | ||
{ | ||
Query::addScope(new \Query\Tests\Unit\Query\Stubs\TestScope); | ||
|
||
$result = Query::test(true)->getParameters(); | ||
|
||
$this->assertEquals(['test' => true], $result); | ||
} | ||
|
||
public function test_query_can_add_scopes_with_closure() | ||
{ | ||
Query::addScope('test', function (Builder $builder, $var) { | ||
return $builder->where('test', $var); | ||
}); | ||
|
||
$result = Query::test(true)->getParameters(); | ||
|
||
$this->assertEquals(['test' => true], $result); | ||
} | ||
} |
Oops, something went wrong.