This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HttpFoundationSearchProcessor for Symfony
This was originally part of the bundle but to add support the API-Platform this needs to life in a more central location.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the RollerworksSearch package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Component\Search\Processor; | ||
|
||
use Psr\Http\Message\ServerRequestInterface as ServerRequest; | ||
use Rollerworks\Component\Search\Exception\UnexpectedTypeException; | ||
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* HttpFoundationSearchProcessor handles a search provided with | ||
* eg. a PSR-7 ServerRequest or a HttpFoundation Request. | ||
* | ||
* @author Sebastiaan Stok <[email protected]> | ||
*/ | ||
final class HttpFoundationSearchProcessor implements SearchProcessor | ||
{ | ||
private $processor; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param SearchProcessor $processor | ||
*/ | ||
public function __construct(SearchProcessor $processor) | ||
{ | ||
$this->processor = $processor; | ||
} | ||
|
||
/** | ||
* Process the request for a search operation. | ||
* | ||
* A processor is expected to follow a few simple conventions: | ||
* | ||
* * The processor must return a SearchPayload with the results of the processing. | ||
* * A new condition must mark the payload as changed. | ||
* * The SearchPayload#searchCode property is expected to contain an input processable | ||
* search-condition (like JSON) which can be used safely within an URI, when the condition is valid. | ||
* * The client may provide the input format using the `format` query/parsedBody information, but the | ||
* processor's implementation can choose to ignore this. | ||
* | ||
* A Processor must first check if a new condition is provided and fall-back | ||
* to the `searchCode` as active condition. | ||
* | ||
* @param ServerRequest|Request $request The ServerRequest to extract information from | ||
* @param ProcessorConfig $config Input processor configuration | ||
* | ||
* @return SearchPayload The SearchPayload contains READ-ONLY information about | ||
* the processing, and 'when there were no errors' the SearchCondition | ||
*/ | ||
public function processRequest($request, ProcessorConfig $config): SearchPayload | ||
{ | ||
if ($request instanceof ServerRequest) { | ||
return $this->processor->processRequest($request, $config); | ||
} | ||
|
||
if (!$request instanceof Request) { | ||
throw new UnexpectedTypeException($request, [ServerRequest::class, Request::class]); | ||
} | ||
|
||
/** @var ServerRequest $request */ | ||
$request = (new DiactorosFactory())->createRequest($request); | ||
|
||
return $this->processor->processRequest($request, $config); | ||
} | ||
} |