-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
275 additions
and
1 deletion.
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
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,98 @@ | ||
<?php | ||
|
||
/* | ||
* doctrine-base-repositories (https://github.com/juliangut/doctrine-base-repositories). | ||
* Doctrine2 utility repositories. | ||
* | ||
* @license MIT | ||
* @link https://github.com/juliangut/doctrine-base-repositories | ||
* @author Julián Gutiérrez <[email protected]> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jgut\Doctrine\Repository; | ||
|
||
/** | ||
* Filters trait. | ||
*/ | ||
trait FiltersTrait | ||
{ | ||
/** | ||
* List of disabled filters. | ||
* | ||
* @var object[] | ||
*/ | ||
protected $disabledFilters = []; | ||
|
||
/** | ||
* Disable all filters. | ||
*/ | ||
public function disableFilters() | ||
{ | ||
foreach (array_keys($this->getFilterCollection()->getEnabledFilters()) as $filter) { | ||
$this->disableFilter($filter); | ||
} | ||
} | ||
|
||
/** | ||
* Disable filter. | ||
* | ||
* @param string $filter | ||
*/ | ||
public function disableFilter(string $filter) | ||
{ | ||
if (in_array($filter, $this->disabledFilters, true)) { | ||
return; | ||
} | ||
|
||
$this->getFilterCollection()->disable($filter); | ||
|
||
$this->disabledFilters[] = $filter; | ||
} | ||
|
||
/** | ||
* Restore all disabled filters. | ||
*/ | ||
public function restoreFilters() | ||
{ | ||
$filterCollection = $this->getFilterCollection(); | ||
|
||
foreach ($this->disabledFilters as $filter) { | ||
$filterCollection->enable($filter); | ||
} | ||
|
||
$this->disabledFilters = []; | ||
} | ||
|
||
/** | ||
* Restore disabled filter. | ||
* | ||
* @param string $filter | ||
*/ | ||
public function restoreFilter(string $filter) | ||
{ | ||
$position = array_search($filter, $this->disabledFilters, true); | ||
if ($position === false) { | ||
return; | ||
} | ||
|
||
$this->getFilterCollection()->enable($filter); | ||
|
||
array_splice($this->disabledFilters, $position, 1); | ||
} | ||
|
||
/** | ||
* Get filter collection. | ||
* | ||
* @return object | ||
*/ | ||
abstract protected function getFilterCollection(); | ||
|
||
/** | ||
* Get object manager. | ||
* | ||
* @return \Doctrine\Common\Persistence\ObjectManager | ||
*/ | ||
abstract protected function getManager(); | ||
} |
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,68 @@ | ||
<?php | ||
|
||
/* | ||
* doctrine-base-repositories (https://github.com/juliangut/doctrine-base-repositories). | ||
* Doctrine2 utility repositories. | ||
* | ||
* @license MIT | ||
* @link https://github.com/juliangut/doctrine-base-repositories | ||
* @author Julián Gutiérrez <[email protected]> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jgut\Doctrine\Repository\Tests; | ||
|
||
use Doctrine\ORM\Configuration; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Query\FilterCollection; | ||
use Jgut\Doctrine\Repository\Tests\Stubs\FilterStub; | ||
use Jgut\Doctrine\Repository\Tests\Stubs\RepositoryStub; | ||
|
||
/** | ||
* Filters trait tests. | ||
*/ | ||
class FiltersTraitTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testFiltersManagement() | ||
{ | ||
$manager = $this->getMockBuilder(EntityManager::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
/* @var EntityManager $manager */ | ||
|
||
$config = new Configuration(); | ||
$config->addFilter('anything', new FilterStub($manager)); | ||
|
||
$manager->expects(self::any()) | ||
->method('getConfiguration') | ||
->will(static::returnValue($config)); | ||
|
||
$filterCollection = new FilterCollection($manager); | ||
$filterCollection->enable('anything'); | ||
|
||
$manager->expects(self::any()) | ||
->method('getFilters') | ||
->will(static::returnValue($filterCollection)); | ||
|
||
$repository = new RepositoryStub($manager); | ||
|
||
$repository->disableFilter('anything'); | ||
static::assertCount(0, $filterCollection->getEnabledFilters()); | ||
|
||
$repository->disableFilter('anything'); | ||
static::assertCount(0, $filterCollection->getEnabledFilters()); | ||
|
||
$repository->restoreFilter('unknown'); | ||
static::assertCount(0, $filterCollection->getEnabledFilters()); | ||
|
||
$repository->restoreFilter('anything'); | ||
static::assertCount(1, $filterCollection->getEnabledFilters()); | ||
|
||
$repository->disableFilters(); | ||
static::assertCount(0, $filterCollection->getEnabledFilters()); | ||
|
||
$repository->restoreFilters(); | ||
static::assertCount(1, $filterCollection->getEnabledFilters()); | ||
} | ||
} |
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 | ||
|
||
/* | ||
* doctrine-base-repositories (https://github.com/juliangut/doctrine-base-repositories). | ||
* Doctrine2 utility repositories. | ||
* | ||
* @license MIT | ||
* @link https://github.com/juliangut/doctrine-base-repositories | ||
* @author Julián Gutiérrez <[email protected]> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jgut\Doctrine\Repository\Tests\Stubs; | ||
|
||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Query\Filter\SQLFilter; | ||
|
||
/** | ||
* Filter stub. | ||
*/ | ||
class FilterStub extends SQLFilter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias) | ||
{ | ||
// Check if the entity implements the LocalAware interface | ||
if (!$targetEntity->reflClass->implementsInterface('anything')) { | ||
return ''; | ||
} | ||
|
||
return $targetTableAlias . '.anything = ' . $this->getParameter('anything'); | ||
} | ||
} |
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