Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.

Commit

Permalink
Add HttpFoundationSearchProcessor for Symfony
Browse files Browse the repository at this point in the history
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
sstok committed Mar 22, 2017
1 parent 87e2aa6 commit fc9cc05
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
77 changes: 77 additions & 0 deletions src/HttpFoundationSearchProcessor.php
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);
}
}

0 comments on commit fc9cc05

Please sign in to comment.