Skip to content

Commit

Permalink
Further refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Scholten committed Jul 16, 2016
1 parent 0aa2b16 commit 4e55181
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 58 deletions.
61 changes: 41 additions & 20 deletions examples/not-symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions src/BaseControllerHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Psr7/ControllerHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/RequestParserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MPScholten\RequestParser;

interface RequestParserFactory
{
/**
* @return RequestParser
*/
public function createQueryParser();

/**
* @return RequestParser
*/
public function createBodyParser();
}
4 changes: 2 additions & 2 deletions src/Symfony/ControllerHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ trait ControllerHelperTrait
{
use BaseControllerHelperTrait;

protected final function createRequestParser($request, $exceptionFactory)
protected final function createRequestParserFactory($request, $exceptionFactory)
{
return new SymfonyRequestParser($request, $exceptionFactory);
return new SymfonyRequestParserFactory($request, $exceptionFactory);
}
}
34 changes: 4 additions & 30 deletions src/Symfony/SymfonyRequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,9 @@

namespace MPScholten\RequestParser\Symfony;

use MPScholten\RequestParser\RequestParser;
use Symfony\Component\HttpFoundation\Request;

class SymfonyRequestParser
/**
* @deprecated This class got renamed to `SymfonyRequestParserFactory`
*/
class SymfonyRequestParser extends SymfonyRequestParserFactory
{
private $request;
private $exceptionFactory;

public function __construct(Request $request, $exceptionFactory = null)
{
$this->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);
}
}
37 changes: 37 additions & 0 deletions src/Symfony/SymfonyRequestParserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace MPScholten\RequestParser\Symfony;

use MPScholten\RequestParser\RequestParser;
use MPScholten\RequestParser\RequestParserFactory;
use Symfony\Component\HttpFoundation\Request;

class SymfonyRequestParserFactory implements RequestParserFactory
{
private $request;
private $exceptionFactory;

public function __construct(Request $request, $exceptionFactory = null)
{
$this->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);
}
}

0 comments on commit 4e55181

Please sign in to comment.