diff --git a/examples/not-symfony.php b/examples/not-symfony.php index eca623d..1e5d2be 100644 --- a/examples/not-symfony.php +++ b/examples/not-symfony.php @@ -12,45 +12,60 @@ require __DIR__ . '/../vendor/autoload.php'; -trait CustomControllerHelperTrait +class CustomRequestParserFactory implements \MPScholten\RequestParser\RequestParserFactory { - private $queryParser; - private $bodyParser; + /** + * @var array + */ + private $request; + private $exceptionFactory; + + public function __construct(array $request, $exceptionFactory) + { + $this->request = $request; + $this->exceptionFactory = $exceptionFactory; + } - protected final function initRequestParser() + public function createQueryParser() { - $this->queryParser = new MPScholten\RequestParser\RequestParser( + return new MPScholten\RequestParser\RequestParser( function ($parameterName) { - if (isset($_GET[$parameterName])) { - return $_GET[$parameterName]; + if (isset($this->request[$parameterName])) { + return $this->request[$parameterName]; } return null; }, - null + $this->exceptionFactory ); - $this->bodyParser = new MPScholten\RequestParser\RequestParser( + } + + public function createBodyParser() + { + return new MPScholten\RequestParser\RequestParser( function ($parameterName) { - if (isset($_POST[$parameterName])) { - return $_POST[$parameterName]; + if (isset($this->request[$parameterName])) { + return $this->request[$parameterName]; } return null; }, - null + $this->exceptionFactory ); } +} - protected function queryParameter($name) - { - return $this->queryParser->get($name); - } +trait CustomControllerHelperTrait +{ + use \MPScholten\RequestParser\BaseControllerHelperTrait; - protected function bodyParameter($name) + /** + * Will be called during the `initRequestParser()` call in `MyController` + */ + protected final function createRequestParserFactory($request, $exceptionFactory) { - return $this->bodyParser->get($name); + return new CustomRequestParserFactory($request, $exceptionFactory); } - } class MyController @@ -59,7 +74,13 @@ class MyController public function __construct() { - $this->initRequestParser(); + if ($_SERVER['REQUEST_METHOD'] === 'GET') { + $request = $_GET; + } else { + $request = $_POST; + } + + $this->initRequestParser($request); } public function hello() diff --git a/src/BaseControllerHelperTrait.php b/src/BaseControllerHelperTrait.php index e3c2fbd..491695d 100644 --- a/src/BaseControllerHelperTrait.php +++ b/src/BaseControllerHelperTrait.php @@ -16,9 +16,10 @@ trait BaseControllerHelperTrait protected final function initRequestParser($request, callable $exceptionFactory = null) { - $requestParser = $this->createRequestParser($request, $exceptionFactory); - $this->queryParser = $requestParser->createQueryParser(); - $this->bodyParser = $requestParser->createBodyParser(); + /** @var $requestParserFactory RequestParserFactory */ + $requestParserFactory = $this->createRequestParserFactory($request, $exceptionFactory); + $this->queryParser = $requestParserFactory->createQueryParser(); + $this->bodyParser = $requestParserFactory->createBodyParser(); } /** diff --git a/src/Psr7/ControllerHelperTrait.php b/src/Psr7/ControllerHelperTrait.php index a6ffb10..e48fac0 100644 --- a/src/Psr7/ControllerHelperTrait.php +++ b/src/Psr7/ControllerHelperTrait.php @@ -8,8 +8,8 @@ trait ControllerHelperTrait { use BaseControllerHelperTrait; - protected final function createRequestParser($request, $exceptionFactory) + protected final function createRequestParserFactory($request, $exceptionFactory) { - return new Psr7RequestParser($request, $exceptionFactory); + return new Psr7RequestParserFactory($request, $exceptionFactory); } } diff --git a/src/Psr7/Psr7RequestParser.php b/src/Psr7/Psr7RequestParserFactory.php similarity index 90% rename from src/Psr7/Psr7RequestParser.php rename to src/Psr7/Psr7RequestParserFactory.php index 6c1d2ed..be760e1 100644 --- a/src/Psr7/Psr7RequestParser.php +++ b/src/Psr7/Psr7RequestParserFactory.php @@ -3,9 +3,10 @@ namespace MPScholten\RequestParser\Psr7; use MPScholten\RequestParser\RequestParser; +use MPScholten\RequestParser\RequestParserFactory; use Psr\Http\Message\ServerRequestInterface; -class Psr7RequestParser +class Psr7RequestParserFactory implements RequestParserFactory { private $request; private $exceptionFactory; diff --git a/src/RequestParserFactory.php b/src/RequestParserFactory.php new file mode 100644 index 0000000..17eeb78 --- /dev/null +++ b/src/RequestParserFactory.php @@ -0,0 +1,16 @@ +request = $request; - $this->exceptionFactory = $exceptionFactory; - } - - public function createQueryParser() - { - $readParameter = function ($name) { - return $this->request->query->get($name, null); - }; - - return new RequestParser($readParameter, $this->exceptionFactory); - } - - public function createBodyParser() - { - $readParameter = function ($name) { - return $this->request->request->get($name, null); - }; - - return new RequestParser($readParameter, $this->exceptionFactory); - } } diff --git a/src/Symfony/SymfonyRequestParserFactory.php b/src/Symfony/SymfonyRequestParserFactory.php new file mode 100644 index 0000000..8fd48bb --- /dev/null +++ b/src/Symfony/SymfonyRequestParserFactory.php @@ -0,0 +1,37 @@ +request = $request; + $this->exceptionFactory = $exceptionFactory; + } + + public function createQueryParser() + { + $readParameter = function ($name) { + return $this->request->query->get($name, null); + }; + + return new RequestParser($readParameter, $this->exceptionFactory); + } + + public function createBodyParser() + { + $readParameter = function ($name) { + return $this->request->request->get($name, null); + }; + + return new RequestParser($readParameter, $this->exceptionFactory); + } +}