Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad-fouladgar committed Jul 13, 2020
2 parents 674e734 + 37e1f10 commit 53b1452
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/Console/FilterMakeCommand.php
Expand Up @@ -42,8 +42,6 @@ class FilterMakeCommand extends GeneratorCommand
* Execute the console command.
*
* @throws FileNotFoundException
*
* @return mixed
*/
public function handle(): void
{
Expand Down
2 changes: 0 additions & 2 deletions src/Console/PublishCommand.php
Expand Up @@ -54,8 +54,6 @@ public function __construct(Filesystem $files)

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/EloquentBuilder.php
Expand Up @@ -40,7 +40,7 @@ public function __construct(FilterFactory $filterFactory)
*
* @throws Exceptions\NotFoundFilterException
*
* @return Builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function to($query, array $filters = null): Builder
{
Expand Down
11 changes: 11 additions & 0 deletions src/ServiceProvider.php
Expand Up @@ -73,6 +73,17 @@ private function registerMacros()
{
Collection::macro('getFilters', function () {
$filters = $this->filter(static function ($value, $filter) {
if (is_array($value)) {
$result = [];
array_walk_recursive($value, static function ($val) use (&$result) {
if (!empty($val)) {
$result[] = $val;
}
});

return !empty($result);
}

return !is_int($filter) && !empty($value);
});

Expand Down
2 changes: 2 additions & 0 deletions src/Support/Foundation/AuthorizeWhenResolvedTrait.php
Expand Up @@ -8,6 +8,8 @@ trait AuthorizeWhenResolvedTrait
{
/**
* authorize the filter instance.
*
* @throws AuthorizationException
*/
public function authorizeResolved()
{
Expand Down
41 changes: 39 additions & 2 deletions tests/CollectionMacrosTest.php
Expand Up @@ -8,12 +8,49 @@ class CollectionMacrosTest extends TestCase
public function it_can_remove_lacking_value()
{
$inputs = collect([
'city' => 'isfahan',
'city' => 'isfahan',
'name',
'gender' => '',
'age' => null,
'date' => [
'from' => '',
'to' => '',
],
'price' => [
'min' => 10000,
'max' => '',
],
'area' => [
'min' => 50,
'max' => 100,
],
'rental' => [
'price' => [
'min' => '',
'max' => 500,
],
],
]);

$this->assertEquals(['city' => 'isfahan'], $inputs->getFilters());
$this->assertEquals(
[
'city' => 'isfahan',
'price' => [
'min' => 10000,
'max' => '',
],
'area' => [
'min' => 50,
'max' => 100,
],
'rental' => [
'price' => [
'min' => '',
'max' => 500,
],
],
],
$inputs->getFilters()
);
}
}
6 changes: 4 additions & 2 deletions tests/FilterMakeCommandTest.php
Expand Up @@ -29,8 +29,10 @@ public function setUp(): void

$this->basePath = $this->getBasePath();

$this->command = m::mock('Fouladgar\EloquentBuilder\Console\FilterMakeCommand[info,rootNamespace,getDefaultNamespace]',
[new Filesystem()])->shouldAllowMockingProtectedMethods();
$this->command = m::mock(
'Fouladgar\EloquentBuilder\Console\FilterMakeCommand[info,rootNamespace,getDefaultNamespace]',
[new Filesystem()]
)->shouldAllowMockingProtectedMethods();

$this->command->shouldReceive('info')->andReturn('Filter[s] created successfully.');
$this->command->shouldReceive('rootNamespace')->andReturn('AppTest');
Expand Down

0 comments on commit 53b1452

Please sign in to comment.