-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Setono/refactor-search-engine
First refactor of the search execution
- Loading branch information
Showing
11 changed files
with
154 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Engine; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class SearchRequest | ||
{ | ||
public function __construct( | ||
public readonly ?string $query, | ||
/** @var array<string, mixed> $filters */ | ||
public readonly array $filters = [], | ||
public readonly int $page = 1, | ||
public readonly ?string $sort = null, | ||
) { | ||
} | ||
|
||
public static function fromRequest(Request $request): self | ||
{ | ||
$q = $request->query->get('q'); | ||
if (!is_string($q)) { | ||
$q = null; | ||
} | ||
|
||
$page = max(1, (int) $request->query->get('p', 1)); | ||
|
||
// todo rename sort to s? | ||
$sort = $request->query->get('sort'); | ||
if (!is_string($sort)) { | ||
$sort = null; | ||
} | ||
|
||
// todo rename facets to f or filters? | ||
/** @var array<string, mixed> $filters */ | ||
$filters = $request->query->all('facets'); | ||
|
||
return new self($q, $filters, $page, $sort); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Tests\Unit\Engine; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Setono\SyliusMeilisearchPlugin\Engine\SearchRequest; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class SearchRequestTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_creates_search_request(): void | ||
{ | ||
$searchRequest = new SearchRequest( | ||
'jeans', | ||
['brand' => ['Celsius small', 'You are breathtaking'], 'price' => ['min' => '30', 'max' => '45']], | ||
2, | ||
'price:asc', | ||
); | ||
|
||
self::assertSame('jeans', $searchRequest->query); | ||
self::assertSame(['brand' => ['Celsius small', 'You are breathtaking'], 'price' => ['min' => '30', 'max' => '45']], $searchRequest->filters); | ||
self::assertSame(2, $searchRequest->page); | ||
self::assertSame('price:asc', $searchRequest->sort); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_creates_from_request(): void | ||
{ | ||
$request = Request::create('/search', 'GET', ['q' => 'jeans', 'facets' => ['brand' => ['Celsius small', 'You are breathtaking'], 'price' => ['min' => '30', 'max' => '45']], 'p' => 2, 'sort' => 'price:asc']); | ||
$searchRequest = SearchRequest::fromRequest($request); | ||
|
||
self::assertSame('jeans', $searchRequest->query); | ||
self::assertSame(['brand' => ['Celsius small', 'You are breathtaking'], 'price' => ['min' => '30', 'max' => '45']], $searchRequest->filters); | ||
self::assertSame(2, $searchRequest->page); | ||
self::assertSame('price:asc', $searchRequest->sort); | ||
} | ||
} |