Skip to content

Commit

Permalink
Refactor body parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
drmad committed Jan 14, 2023
1 parent 1101ff4 commit 13b6b05
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 28 deletions.
12 changes: 5 additions & 7 deletions src/BodyParser/BodyParserInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

namespace Vendimia\Http\BodyParser;

use Vendimia\Http\Request;

/**
* Interface for body parsers
*
Expand All @@ -9,12 +12,7 @@
interface BodyParserInterface
{
/**
* Returns whether this parser can decode a MIME type
*/
public static function canDecode(string $mime): bool;

/**
* Decodes a string into an array
* Decodes a body string into an array
*/
public static function parse(string $source): array;
public static function parseBody(Request $request): Request;
}
26 changes: 26 additions & 0 deletions src/BodyParser/FormData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Vendimia\Http\BodyParser;

use Vendimia\Http\Request;
use Vendimia\Exception\VendimiaException;
use Exception;
use JsonException;

/**
* Parse a multipart/form-data body to obtain the parsedBody
*
* @author Oliver Etchebarne <yo@drmad.org>
*/
class FormData implements BodyParserInterface
{
/**
* FIXME: This should not use $_POST
*/
public static function parseBody(Request $request): Request
{
$request = $request->withParsedBody($_POST);

return $request;
}
}
22 changes: 13 additions & 9 deletions src/BodyParser/Json.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace Vendimia\Http\BodyParser;

use Vendimia\Http\Request;
use Vendimia\Exception\VendimiaException;
use Exception;
use JsonException;
use Vendimia\Exception\VendimiaException;

/**
* Parses a JSON body to an array.
Expand All @@ -12,30 +14,32 @@
*/
class Json implements BodyParserInterface
{
public static function canDecode(string $mime): bool
public static function parseBody(Request $request): Request
{
return $mime == 'application/json';
}
$body = $request->getBody()->getContents();
if (!$body) {
return $request;
}

public static function parse($source): array
{
try {
$result = json_decode(
$source,
$body,
associative: true,
flags: JSON_THROW_ON_ERROR
);
} catch (JsonException $e) {
// Lo rethrowamos
if (class_exists(VendimiaException::class)) {
throw new VendimiaException("Error parsing JSON body: " . $e->getMessage(),
original_body: $source,
original_body: $body,
);
} else {
throw new Exception("Error parsing JSON body: " . $e->getMessage());
}
}

return $result;
$request = $request->withParsedBody($result);

return $request;
}
}
14 changes: 7 additions & 7 deletions src/BodyParser/Url.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

namespace Vendimia\Http\BodyParser;

use Vendimia\Http\Request;

/**
* Parses a url-encoded body to an array.
*
* @author Oliver Etchebarne <yo@drmad.org>
*/
class Url implements BodyParserInterface
{
public static function canDecode(string $mime): bool
public static function parseBody(Request $request): Request
{
return $mime == 'application/x-www-form-urlencoded';
}
parse_str($request->getBody()->getContents(), $parsed_body);
$request = $request->withParsedBody($parsed_body);

public static function parse($source): array
{
parse_str($source, $parsed_body);
return $parsed_body;
return $request;
}
}
15 changes: 10 additions & 5 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class Request extends Psr\ServerRequest
/**
* Default Body parsers
*/
const REGISTERED_PARSERS = [
BodyParser\Url::class,
BodyParser\Json::class,
private static $REGISTERED_PARSERS = [
'application/x-www-form-urlencoded' => BodyParser\Url::class,
'application/json' => BodyParser\Json::class,
'multipart/form-data' => BodyParser\FormData::class,
];

// Shortcut to $this->getParsedBody(), in a Vendimia\Collection\Collection
Expand Down Expand Up @@ -69,7 +70,11 @@ public static function fromPHP(): self
// Ignoramos los parámetros extras
$content_type = trim(explode(';', $content_type)[0]);

foreach (self::REGISTERED_PARSERS as $parse_class) {
if ($parser_class = self::$REGISTERED_PARSERS[$content_type] ?? null) {
$server_request = $parser_class::parseBody($server_request);
}

/*foreach (self::$REGISTERED_PARSERS as $mime => $parse_class) {
if ($parse_class::canDecode($content_type)) {
$body_content = $body->getContents();
Expand All @@ -82,7 +87,7 @@ public static function fromPHP(): self
break;
}
}
}*/
}

$server_request->parsed_body = $server_request->getParsedBody();
Expand Down

0 comments on commit 13b6b05

Please sign in to comment.