-
Notifications
You must be signed in to change notification settings - Fork 32
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 #171 from arbory/1.0-filters-refactor
Refactor filters
- Loading branch information
Showing
51 changed files
with
2,497 additions
and
538 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
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 |
---|---|---|
|
@@ -13,4 +13,5 @@ | |
], | ||
'enabled' => 'Enabled', | ||
'apply' => 'Apply', | ||
'reset' => 'Reset', | ||
]; |
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
|
||
/** | ||
* Interface ExportInterface. | ||
* @package Arbory\Base\Admin\Grid | ||
*/ | ||
interface ExportInterface | ||
{ | ||
|
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,16 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Admin\Filter\Concerns; | ||
|
||
use Arbory\Base\Admin\Filter\FilterItem; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
interface WithCustomExecutor | ||
{ | ||
/** | ||
* @param FilterItem $filterItem | ||
* @param Builder $builder | ||
* @return void | ||
*/ | ||
public function execute(FilterItem $filterItem, Builder $builder): void; | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Admin\Filter\Concerns; | ||
|
||
use Arbory\Base\Admin\Filter\Parameters\FilterParameters; | ||
|
||
interface WithParameterValidation | ||
{ | ||
/** | ||
* @param FilterParameters $parameters | ||
* @param callable $attributeResolver | ||
* | ||
* @return array | ||
*/ | ||
public function rules(FilterParameters $parameters, callable $attributeResolver): array; | ||
|
||
/** | ||
* @param FilterParameters $parameters | ||
* @param callable $attributeResolver | ||
* | ||
* @return array | ||
*/ | ||
public function messages(FilterParameters $parameters, callable $attributeResolver): array; | ||
|
||
/** | ||
* @param FilterParameters $parameters | ||
* @param callable $attributeResolver | ||
* | ||
* @return array | ||
*/ | ||
public function attributes(FilterParameters $parameters, callable $attributeResolver): array; | ||
} |
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 Arbory\Base\Admin\Filter\Config; | ||
|
||
use ReflectionClass; | ||
use Illuminate\Support\Str; | ||
use Arbory\Base\Support\ExtendedFluent; | ||
|
||
class BaseConfig extends ExtendedFluent | ||
{ | ||
private const PREFIX_CONFIG = 'CONFIG_'; | ||
|
||
/** | ||
* Returns defined config options. | ||
* | ||
* @return array | ||
* @throws \ReflectionException | ||
*/ | ||
public static function getAvailable(): array | ||
{ | ||
$reflection = new ReflectionClass(static::class); | ||
|
||
$available = []; | ||
|
||
foreach ($reflection->getConstants() as $constant) { | ||
if (Str::startsWith($constant, self::PREFIX_CONFIG)) { | ||
$available[] = Str::after($constant, self::PREFIX_CONFIG); | ||
} | ||
} | ||
|
||
return $available; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Admin\Filter\Config; | ||
|
||
class SelectLikeTypeConfig extends BaseConfig | ||
{ | ||
/** | ||
* Accepts a key => value iterable array. | ||
*/ | ||
public const CONFIG_OPTIONS = 'options'; | ||
|
||
/** | ||
* Accepts bool. | ||
*/ | ||
public const CONFIG_MULTIPLE = 'multiple'; | ||
|
||
/** | ||
* @param iterable $options | ||
* @return SelectLikeTypeConfig | ||
*/ | ||
public function setOptions(iterable $options): self | ||
{ | ||
$this->set(static::CONFIG_OPTIONS, $options); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return iterable | ||
*/ | ||
public function getOptions(): iterable | ||
{ | ||
return $this->get(static::CONFIG_OPTIONS); | ||
} | ||
|
||
/** | ||
* @param bool $options | ||
* @return SelectLikeTypeConfig | ||
*/ | ||
public function setMultiple(?bool $options): self | ||
{ | ||
$this->set(static::CONFIG_OPTIONS, $options); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isMultiple(): ?bool | ||
{ | ||
return $this->get(static::CONFIG_MULTIPLE); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Admin\Filter\Config; | ||
|
||
class TextLikeTypeConfig extends BaseConfig | ||
{ | ||
public const CONFIG_TYPE = 'type'; | ||
|
||
/** | ||
* @param string $type | ||
* @return TextLikeTypeConfig | ||
*/ | ||
public function setType(string $type): self | ||
{ | ||
$this->set(static::CONFIG_TYPE, $type); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getType(): ?string | ||
{ | ||
return $this->get(static::CONFIG_TYPE); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Admin\Filter; | ||
|
||
interface CustomizedTargetInterface | ||
{ | ||
public function getFilterTitle(): string; | ||
} |
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,45 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Admin\Filter; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class FilterCollection extends Collection | ||
{ | ||
/** | ||
* @var FilterItem[] | ||
*/ | ||
protected $items = []; | ||
|
||
/** | ||
* @param string[] $concerns | ||
* @return FilterCollection|FilterItem[] | ||
*/ | ||
public function findByConcerns(array $concerns): self | ||
{ | ||
return $this->filter(static function (FilterItem $filterItem) use ($concerns) { | ||
$implements = array_values(class_implements($filterItem->getType())); | ||
|
||
return count(array_intersect($concerns, $implements)) === count($concerns); | ||
}); | ||
} | ||
|
||
/** | ||
* @param $owner | ||
* @return FilterCollection|FilterItem[] | ||
*/ | ||
public function findByOwner($owner): self | ||
{ | ||
return $this->filter(static function (FilterItem $filterItem) use ($owner) { | ||
return $owner === $filterItem->getOwner(); | ||
}); | ||
} | ||
|
||
/** | ||
* @return FilterCollection | ||
*/ | ||
public function findWithoutOwners(): self | ||
{ | ||
return $this->findByOwner(null); | ||
} | ||
} |
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,72 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Admin\Filter; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Arbory\Base\Admin\Filter\Concerns\WithCustomExecutor; | ||
use Arbory\Base\Admin\Filter\Parameters\FilterParameters; | ||
|
||
class FilterExecutor | ||
{ | ||
/** | ||
* @param FilterManager $filterManager | ||
* @param Builder $builder | ||
* | ||
* @return Builder | ||
*/ | ||
public function execute(FilterManager $filterManager, Builder $builder): Builder | ||
{ | ||
$parameters = $filterManager->getParameters(); | ||
|
||
foreach ($filterManager->getFilters() as $filterItem) { | ||
$this->executeForItem($filterItem, $parameters, $builder); | ||
} | ||
|
||
return $builder; | ||
} | ||
|
||
/** | ||
* @param FilterItem $filterItem | ||
* @param FilterParameters $parameters | ||
* @param Builder $builder | ||
*/ | ||
protected function executeForItem(FilterItem $filterItem, FilterParameters $parameters, Builder $builder): void | ||
{ | ||
if (! $parameters->has($filterItem->getName())) { | ||
return; | ||
} | ||
|
||
// Use user defined executor | ||
if ($executor = $filterItem->getExecutor()) { | ||
$executor($filterItem, $builder); | ||
|
||
return; | ||
} | ||
|
||
$type = $filterItem->getType(); | ||
|
||
if ($type instanceof WithCustomExecutor) { | ||
$type->execute($filterItem, $builder); | ||
|
||
return; | ||
} | ||
|
||
$this->applyQuery($parameters, $filterItem, $builder); | ||
} | ||
|
||
/** | ||
* @param FilterParameters $parameters | ||
* @param FilterItem $filterItem | ||
* @param Builder $builder | ||
*/ | ||
protected function applyQuery(FilterParameters $parameters, FilterItem $filterItem, Builder $builder): void | ||
{ | ||
$value = $parameters->getFromFilter($filterItem); | ||
|
||
if (is_array($value)) { | ||
$builder->whereIn($filterItem->getName(), $value); | ||
} else { | ||
$builder->where($filterItem->getName(), $value); | ||
} | ||
} | ||
} |
Oops, something went wrong.