From fc9cc05a9394a2a83c48cb25f1723b4db0a07da5 Mon Sep 17 00:00:00 2001 From: Sebastiaan Stok Date: Wed, 22 Mar 2017 16:13:36 +0100 Subject: [PATCH] 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. --- composer.json | 1 + src/HttpFoundationSearchProcessor.php | 77 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/HttpFoundationSearchProcessor.php diff --git a/composer.json b/composer.json index e103547..f3ff4bb 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ }, "require-dev": { "symfony/phpunit-bridge": "^3.2.4", + "symfony/psr-http-message-bridge": "^1.0.0", "zendframework/zend-diactoros": "^1.3.9", "psr/simple-cache": "^1.0.0" }, diff --git a/src/HttpFoundationSearchProcessor.php b/src/HttpFoundationSearchProcessor.php new file mode 100644 index 0000000..e042894 --- /dev/null +++ b/src/HttpFoundationSearchProcessor.php @@ -0,0 +1,77 @@ + + * + * 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 + */ +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); + } +}