From d81e6cdf06909327d6497729f6bed9d0340d2ca6 Mon Sep 17 00:00:00 2001 From: Te7a-Houdini Date: Fri, 1 Feb 2019 19:26:10 +0200 Subject: [PATCH] Modify using helper methods to facades --- src/ModelSearchAspect.php | 3 ++- src/Search.php | 3 ++- src/SearchAspect.php | 7 ++++--- tests/ModelSearchAspectTest.php | 3 ++- tests/SearchTest.php | 15 ++++++++------- tests/stubs/CustomNameSearchAspect.php | 3 ++- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/ModelSearchAspect.php b/src/ModelSearchAspect.php index c0e4735..8e575d7 100644 --- a/src/ModelSearchAspect.php +++ b/src/ModelSearchAspect.php @@ -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; @@ -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'); diff --git a/src/Search.php b/src/Search.php index 0e27f41..038f4be 100644 --- a/src/Search.php +++ b/src/Search.php @@ -2,6 +2,7 @@ namespace Spatie\Searchable; +use Illuminate\Support\Arr; use Illuminate\Foundation\Auth\User; class Search @@ -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]; } diff --git a/src/SearchAspect.php b/src/SearchAspect.php index 245f56d..f71732b 100644 --- a/src/SearchAspect.php +++ b/src/SearchAspect.php @@ -2,6 +2,7 @@ namespace Spatie\Searchable; +use Illuminate\Support\Str; use Illuminate\Support\Collection; abstract class SearchAspect @@ -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); } } diff --git a/tests/ModelSearchAspectTest.php b/tests/ModelSearchAspectTest.php index 4471c82..2b4fee8 100644 --- a/tests/ModelSearchAspectTest.php +++ b/tests/ModelSearchAspectTest.php @@ -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; @@ -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); } diff --git a/tests/SearchTest.php b/tests/SearchTest.php index 14cea68..d60eb58 100644 --- a/tests/SearchTest.php +++ b/tests/SearchTest.php @@ -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; @@ -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 */ @@ -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 */ @@ -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 */ @@ -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'); @@ -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'); @@ -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'); diff --git a/tests/stubs/CustomNameSearchAspect.php b/tests/stubs/CustomNameSearchAspect.php index 3c32b47..825eba0 100644 --- a/tests/stubs/CustomNameSearchAspect.php +++ b/tests/stubs/CustomNameSearchAspect.php @@ -2,6 +2,7 @@ namespace Spatie\Searchable\Tests\stubs; +use Illuminate\Support\Str; use Illuminate\Support\Collection; use Spatie\Searchable\SearchAspect; @@ -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); }); } }