From 3e0c592fabe685b8c929de7a4ee12b706aa28502 Mon Sep 17 00:00:00 2001 From: Rias Date: Fri, 18 Sep 2020 10:48:24 +0200 Subject: [PATCH 1/3] Allow for transforming values --- src/Search/Searchables.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Search/Searchables.php b/src/Search/Searchables.php index e6f6a72497..168baa534b 100644 --- a/src/Search/Searchables.php +++ b/src/Search/Searchables.php @@ -2,6 +2,7 @@ namespace Statamic\Search; +use Closure; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Statamic\Facades\Asset; @@ -65,11 +66,18 @@ public function contains($searchable) public function fields($searchable): array { $fields = $this->index->config()['fields']; + $transformers = $this->index->config()['transformers'] ?? []; return collect($fields)->mapWithKeys(function ($field) use ($searchable) { $value = method_exists($searchable, $field) ? $searchable->{$field}() : $searchable->get($field); return [$field => $value]; + })->flatMap(function ($value, $field) use ($transformers) { + if (! isset($transformers[$field]) || ! $transformers[$field] instanceof Closure) { + return [$field => $value]; + } + + return $transformers[$field]($value); })->all(); } } From 71517d0b3881fb2de0bfa5ba6ac05d2ca1fa3f43 Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 6 Oct 2020 09:05:24 +0200 Subject: [PATCH 2/3] Add tests --- src/Search/Searchables.php | 9 +++- tests/Search/SearchablesTest.php | 71 ++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/Search/SearchablesTest.php diff --git a/src/Search/Searchables.php b/src/Search/Searchables.php index 168baa534b..44c52c7c46 100644 --- a/src/Search/Searchables.php +++ b/src/Search/Searchables.php @@ -70,14 +70,19 @@ public function fields($searchable): array return collect($fields)->mapWithKeys(function ($field) use ($searchable) { $value = method_exists($searchable, $field) ? $searchable->{$field}() : $searchable->get($field); - return [$field => $value]; })->flatMap(function ($value, $field) use ($transformers) { if (! isset($transformers[$field]) || ! $transformers[$field] instanceof Closure) { return [$field => $value]; } - return $transformers[$field]($value); + $transformedValue = $transformers[$field]($value); + + if (is_array($transformedValue)) { + return $transformedValue; + } + + return [$field => $transformedValue]; })->all(); } } diff --git a/tests/Search/SearchablesTest.php b/tests/Search/SearchablesTest.php new file mode 100644 index 0000000000..1b4f902a23 --- /dev/null +++ b/tests/Search/SearchablesTest.php @@ -0,0 +1,71 @@ +set('statamic.search.indexes.default', [ + 'fields' => [ + 'title', + ], + 'transformers' => [ + 'title' => function ($value) { + return strtoupper($value); + } + ] + ]); + + $index = app(\Statamic\Search\Comb\Index::class, [ + 'name' => 'default', + 'config' => config('statamic.search.indexes.default'), + ]); + + $searchable = Entry::make()->data(['title' => 'Hello']); + $searchables = new Searchables($index); + + $this->assertEquals([ + 'title' => 'HELLO', + ], $searchables->fields($searchable)); + } + + /** @test */ + public function if_a_transformer_returns_an_array_it_gets_combined_into_the_results() + { + config()->set('statamic.search.indexes.default', [ + 'fields' => [ + 'title', + ], + 'transformers' => [ + 'title' => function ($value) { + return [ + 'title' => $value, + 'title_upper' => strtoupper($value), + ]; + } + ] + ]); + + $index = app(\Statamic\Search\Comb\Index::class, [ + 'name' => 'default', + 'config' => config('statamic.search.indexes.default'), + ]); + + $searchable = Entry::make()->data(['title' => 'Hello']); + $searchables = new Searchables($index); + + $this->assertEquals([ + 'title' => 'Hello', + 'title_upper' => 'HELLO', + ], $searchables->fields($searchable)); + } +} From 7ba63301f009691a7ed2490f28deab08457e0ebe Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 6 Oct 2020 09:06:52 +0200 Subject: [PATCH 3/3] make styleci happy --- src/Search/Searchables.php | 1 + tests/Search/SearchablesTest.php | 11 ++++------- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Search/Searchables.php b/src/Search/Searchables.php index 44c52c7c46..98c096b464 100644 --- a/src/Search/Searchables.php +++ b/src/Search/Searchables.php @@ -70,6 +70,7 @@ public function fields($searchable): array return collect($fields)->mapWithKeys(function ($field) use ($searchable) { $value = method_exists($searchable, $field) ? $searchable->{$field}() : $searchable->get($field); + return [$field => $value]; })->flatMap(function ($value, $field) use ($transformers) { if (! isset($transformers[$field]) || ! $transformers[$field] instanceof Closure) { diff --git a/tests/Search/SearchablesTest.php b/tests/Search/SearchablesTest.php index 1b4f902a23..c94cb49af3 100644 --- a/tests/Search/SearchablesTest.php +++ b/tests/Search/SearchablesTest.php @@ -2,10 +2,7 @@ namespace Tests\Search; -use Mockery; use Statamic\Facades\Entry; -use Statamic\Search\Algolia\Index; -use Statamic\Search\ItemResolver; use Statamic\Search\Searchables; use Tests\TestCase; @@ -21,8 +18,8 @@ public function it_transforms_values_set_in_the_config_file() 'transformers' => [ 'title' => function ($value) { return strtoupper($value); - } - ] + }, + ], ]); $index = app(\Statamic\Search\Comb\Index::class, [ @@ -51,8 +48,8 @@ public function if_a_transformer_returns_an_array_it_gets_combined_into_the_resu 'title' => $value, 'title_upper' => strtoupper($value), ]; - } - ] + }, + ], ]); $index = app(\Statamic\Search\Comb\Index::class, [