Skip to content

Commit

Permalink
Merge 928f075 into f94562e
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Jun 17, 2021
2 parents f94562e + 928f075 commit 05f4534
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coveralls.yml
Expand Up @@ -42,7 +42,7 @@ jobs:
uses: "ramsey/composer-install@v1"
with:
dependency-versions: "${{ matrix.dependencies }}"
composer-options: "--prefer-dist"
composer-options: "--prefer-dist --no-cache"

- name: Execute tests
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Expand Up @@ -49,7 +49,7 @@ jobs:
uses: "ramsey/composer-install@v1"
with:
dependency-versions: "${{ matrix.dependencies }}"
composer-options: "--prefer-dist"
composer-options: "--prefer-dist --no-cache"

- name: Execute tests
run: vendor/bin/phpunit
21 changes: 21 additions & 0 deletions src/Concerns/ConditionallySearchingWildcard.php
Expand Up @@ -11,6 +11,27 @@ trait ConditionallySearchingWildcard
*/
public $wildcardSearching = null;

/**
* Widlcard search variants.
*
* @var array|null
*/
public $wildcardSearchVariants = null;

/**
* Set wildcard search variants.
*
* @param array $searchVariants
*
* @return $this
*/
public function wildcardSearchVariants(?array $searchVariants)
{
$this->wildcardSearchVariants = $searchVariants;

return $this;
}

/**
* Set wildcard searching status.
*
Expand Down
14 changes: 10 additions & 4 deletions src/Keyword.php
Expand Up @@ -60,7 +60,8 @@ public function all(): array
$this->value,
$this->wildcardCharacter,
$this->wildcardReplacement,
$this->wildcardSearching ?? true
$this->wildcardSearching ?? true,
$this->wildcardSearchVariants ?? null
);
}

Expand Down Expand Up @@ -94,16 +95,21 @@ public function __toString()
/**
* Convert basic string to searchable result.
*/
public static function searchable(string $text, ?string $wildcard = '*', ?string $replacement = '%', bool $wildcardSearching = true): array
{
public static function searchable(
string $text,
?string $wildcard = '*',
?string $replacement = '%',
bool $wildcardSearching = true,
?array $wildcardSearchVariants = null
): array {
$text = static::sanitize($text);

if (empty($text)) {
return [];
} elseif (\is_null($replacement)) {
return [$text];
} elseif (! Str::contains($text, array_filter([$wildcard, $replacement])) && $wildcardSearching === true) {
return Collection::make(static::$defaultSearchVariations)
return Collection::make($wildcardSearchVariants ?? static::$defaultSearchVariations)
->map(static function ($string) use ($text) {
return Str::replaceFirst('{keyword}', $text, $string);
})->all();
Expand Down
4 changes: 4 additions & 0 deletions src/Searchable.php
Expand Up @@ -60,6 +60,7 @@ public function apply($query)

$keywords->wildcardCharacter($this->wildcardCharacter)
->wildcardReplacement($this->wildcardReplacement)
->wildcardSearchVariants($this->wildcardSearchVariants)
->wildcardSearching($this->wildcardSearching ?? true);

$likeOperator = like_operator(connection_type($query));
Expand Down Expand Up @@ -130,6 +131,7 @@ protected function queryOnColumnUsing(
$this->searchKeyword()
->wildcardCharacter($this->wildcardCharacter)
->wildcardReplacement($this->wildcardReplacement)
->wildcardSearchVariants($this->wildcardSearchVariants)
->wildcardSearching($field->wildcardSearching ?? $this->wildcardSearching ?? true)
->handle($filter),
$likeOperator,
Expand Down Expand Up @@ -159,6 +161,7 @@ protected function queryOnJsonColumnUsing(
$this->searchKeyword()
->wildcardCharacter($this->wildcardCharacter)
->wildcardReplacement($this->wildcardReplacement)
->wildcardSearchVariants($this->wildcardSearchVariants)
->wildcardSearching($field->wildcardSearching ?? $this->wildcardSearching ?? true)
->handle($filter),
$likeOperator,
Expand All @@ -183,6 +186,7 @@ protected function queryOnColumnUsingRelation(
$this->searchKeyword()
->wildcardCharacter($this->wildcardCharacter)
->wildcardReplacement($this->wildcardReplacement)
->wildcardSearchVariants($this->wildcardSearchVariants)
->wildcardSearching($field->wildcardSearching ?? $this->wildcardSearching ?? true)
->handle($filter),
$likeOperator,
Expand Down
31 changes: 31 additions & 0 deletions tests/Feature/EloquentSearchableTest.php
Expand Up @@ -43,6 +43,37 @@ public function it_can_build_search_query()
$this->assertSame(5, $query->count());
}

/** @test */
public function it_can_build_search_query_with_custom_search_variants()
{
UserFactory::new()->times(5)->create([
'name' => 'hello world',
]);

UserFactory::new()->times(3)->create([
'name' => 'goodbye world',
]);

$stub = (new Searchable(
'hello', ['name']
))->wildcardSearchVariants(['%{keyword}%']);

$query = User::query();
$stub->apply($query);

$this->assertSame(
'select * from "users" where ("users"."name" like ?)',
$query->toSql()
);

$this->assertSame(
['%hello%'],
$query->getBindings()
);

$this->assertSame(5, $query->count());
}

/** @test */
public function it_can_build_search_query_with_combined_with_search_filters()
{
Expand Down

0 comments on commit 05f4534

Please sign in to comment.