Skip to content

Commit

Permalink
Modify using helper methods to facades
Browse files Browse the repository at this point in the history
  • Loading branch information
amaelftah committed Feb 1, 2019
1 parent 05da762 commit d81e6cd
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/ModelSearchAspect.php
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Searchable;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -105,7 +106,7 @@ protected function addSearchConditions(Builder $query, string $term)
$searchTerms = explode(' ', $term);

$query->where(function (Builder $query) use ($attributes, $term, $searchTerms) {
foreach (array_wrap($attributes) as $attribute) {
foreach (Arr::wrap($attributes) as $attribute) {
foreach ($searchTerms as $searchTerm) {
$sql = "LOWER({$attribute->getAttribute()}) LIKE ?";
$searchTerm = mb_strtolower($searchTerm, 'UTF8');
Expand Down
3 changes: 2 additions & 1 deletion src/Search.php
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Searchable;

use Illuminate\Support\Arr;
use Illuminate\Foundation\Auth\User;

class Search
Expand Down Expand Up @@ -30,7 +31,7 @@ public function registerModel(string $modelClass, ...$attributes): self
$attributes = $attributes[0];
}

if (is_array(array_get($attributes, 0))) {
if (is_array(Arr::get($attributes, 0))) {
$attributes = $attributes[0];
}

Expand Down
7 changes: 4 additions & 3 deletions src/SearchAspect.php
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Searchable;

use Illuminate\Support\Str;
use Illuminate\Support\Collection;

abstract class SearchAspect
Expand All @@ -16,10 +17,10 @@ public function getType(): string

$className = class_basename(static::class);

$type = str_before($className, 'SearchAspect');
$type = Str::before($className, 'SearchAspect');

$type = snake_case(str_plural($type));
$type = Str::snake(Str::plural($type));

return str_plural($type);
return Str::plural($type);
}
}
3 changes: 2 additions & 1 deletion tests/ModelSearchAspectTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Searchable\Tests;

use ReflectionObject;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Spatie\Searchable\ModelSearchAspect;
Expand Down Expand Up @@ -72,7 +73,7 @@ public function it_can_build_an_eloquent_query()

$expectedQuery = 'select * from "test_models" where (LOWER(name) LIKE ? or "email" = ?)';

$executedQuery = array_get(DB::getQueryLog(), '0.query');
$executedQuery = Arr::get(DB::getQueryLog(), '0.query');

$this->assertEquals($expectedQuery, $executedQuery);
}
Expand Down
15 changes: 8 additions & 7 deletions tests/SearchTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Searchable\Tests;

use ReflectionObject;
use Illuminate\Support\Arr;
use Spatie\Searchable\Search;
use Spatie\Searchable\ModelSearchAspect;
use Spatie\Searchable\Tests\Models\TestModel;
Expand Down Expand Up @@ -98,7 +99,7 @@ public function it_can_register_a_class_name_as_search_aspect()
$aspects = $search->getSearchAspects();

$this->assertCount(1, $aspects);
$this->assertInstanceOf(CustomNameSearchAspect::class, array_first($aspects));
$this->assertInstanceOf(CustomNameSearchAspect::class, Arr::first($aspects));
}

/** @test */
Expand All @@ -111,7 +112,7 @@ public function it_can_register_search_aspect()
$aspects = $search->getSearchAspects();

$this->assertCount(1, $aspects);
$this->assertInstanceOf(CustomNameSearchAspect::class, array_first($aspects));
$this->assertInstanceOf(CustomNameSearchAspect::class, Arr::first($aspects));
}

/** @test */
Expand All @@ -124,8 +125,8 @@ public function it_can_register_a_model_search_aspect()
$aspects = $search->getSearchAspects();

$this->assertCount(1, $aspects);
$this->assertInstanceOf(ModelSearchAspect::class, array_first($aspects));
$this->assertEquals('test_models', array_first($aspects)->getType());
$this->assertInstanceOf(ModelSearchAspect::class, Arr::first($aspects));
$this->assertEquals('test_models', Arr::first($aspects)->getType());
}

/** @test */
Expand All @@ -135,7 +136,7 @@ public function it_can_register_a_model_search_aspect_with_attributes()

$search->registerModel(TestModel::class, 'name', 'email');

$aspect = array_first($search->getSearchAspects());
$aspect = Arr::first($search->getSearchAspects());

$refObject = new ReflectionObject($aspect);
$refProperty = $refObject->getProperty('attributes');
Expand All @@ -152,7 +153,7 @@ public function it_can_register_a_model_search_aspect_with_an_array_of_attribute

$search->registerModel(TestModel::class, ['name', 'email']);

$aspect = array_first($search->getSearchAspects());
$aspect = Arr::first($search->getSearchAspects());

$refObject = new ReflectionObject($aspect);
$refProperty = $refObject->getProperty('attributes');
Expand All @@ -173,7 +174,7 @@ public function it_can_register_a_model_search_aspect_with_a_attributes_from_a_c
->addExactSearchableAttribute('email');
});

$aspect = array_first($search->getSearchAspects());
$aspect = Arr::first($search->getSearchAspects());

$refObject = new ReflectionObject($aspect);
$refProperty = $refObject->getProperty('attributes');
Expand Down
3 changes: 2 additions & 1 deletion tests/stubs/CustomNameSearchAspect.php
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Searchable\Tests\stubs;

use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Spatie\Searchable\SearchAspect;

Expand All @@ -22,7 +23,7 @@ public function getResults(string $term): Collection
{
return collect($this->accounts)
->filter(function (Account $account) use ($term) {
return str_contains($account->name, $term);
return Str::contains($account->name, $term);
});
}
}

0 comments on commit d81e6cd

Please sign in to comment.