Skip to content

Commit

Permalink
Merge pull request #13 from avalanche-development/remove-param-casting
Browse files Browse the repository at this point in the history
Remove param casting
  • Loading branch information
jacobemerick committed Apr 8, 2017
2 parents 42b16f9 + 57ad784 commit 61e7948
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 826 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ There is some routing being done here. If the request route cannot be found in s

### Parameter Parsing

The middleware will do it's best to parse out the parameters for the request without too much validation. It loops through the swagger definition, looking for the parameters that are applicable for the route, and pull them from header/query/path/etc. Parameters will be cast to their defined type (most notable, date and date-time will be cast into DateTime objects). If there is an issue on this step (like, an object cannot be parsed or a date is invalid) this is where that BadRequest will be thrown.
The middleware will do it's best to parse out the parameters for the request without too much validation. It loops through the swagger definition, looking for the parameters that are applicable for the route, and pull them from header/query/path/etc.

Again, this middleware will not check the existence or validity of parameters based on the spec. It only tries to pull and format it and expects something else in the stack to verify integrity.
Again, this middleware will not check the existence, validity, or type of parameters based on the spec. It only tries to pull and expects something else in the stack to verify integrity.

## Development

Expand Down
126 changes: 0 additions & 126 deletions src/ParameterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace AvalancheDevelopment\SwaggerRouterMiddleware;

use AvalancheDevelopment\Peel\HttpError\BadRequest;
use DateTime;
use Exception;
use Psr\Http\Message\ServerRequestInterface as Request;

Expand All @@ -25,7 +24,6 @@ public function __invoke(Request $request, array $parameter, $route)
$value = $parameter['default'];
}

$value = $this->castType($value, $parameter);
return $value;
}

Expand Down Expand Up @@ -59,128 +57,4 @@ protected function getParser(Request $request, array $parameter, $route)
}
return $parser;
}

/**
* @param mixed $value
* @param array $parameter
* @return mixed
*/
protected function castType($value, array $parameter)
{
$type = $this->getParameterType($parameter);

switch ($type) {
case 'array':
foreach ($value as $key => $row) {
$value[$key] = $this->castType($row, $parameter['items']);
}
break;
case 'boolean':
$value = (boolean) $value;
break;
case 'file':
break;
case 'integer':
$value = (int) $value;
break;
case 'number':
$value = (float) $value;
break;
case 'object':
$value = $this->formatObject($value, $parameter);
break;
case 'string':
$value = (string) $value;
$value = $this->formatString($value, $parameter);
break;
default:
throw new Exception('Invalid parameter type value defined in swagger');
break;
}

return $value;
}

/**
* @param array $parameter
* @return string
*/
protected function getParameterType(array $parameter)
{
$type = '';

if (isset($parameter['type'])) {
$type = $parameter['type'];
}
if (isset($parameter['in']) && $parameter['in'] === 'body') {
$type = $parameter['schema']['type'];
}

if (empty($type)) {
throw new Exception('Parameter type is not defined in swagger');
}
return $type;
}

/**
* @param string $value
* @param array $parameter
* @return object
*/
protected function formatObject($value, array $parameter)
{
$object = $value;
if (!is_object($object)) {
$object = (string) $object;
$object = json_decode($object);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new BadRequest('Bad json object passed in as parameter');
}
}

$schema = array_key_exists('schema', $parameter) ? $parameter['schema'] : $parameter;
if (empty($schema['properties'])) {
return $object;
}
$properties = $schema['properties'];

foreach ($object as $key => $attribute) {
$object->{$key} = $this->castType($attribute, $properties[$key]);
}

return $object;
}

/**
* @param string $value
* @param array $parameter
* @return mixed
*/
protected function formatString($value, array $parameter)
{
if (!array_key_exists('format', $parameter)) {
return $value;
}

switch ($parameter['format']) {
case 'date':
$value = DateTime::createFromFormat('Y-m-d', $value);
if (!$value) {
throw new BadRequest('Invalid date parameter passed in');
}
break;
case 'date-time':
try {
$value = new DateTime($value);
} catch (Exception $e) {
throw new BadRequest('Invalid date parameter passed in');
}
break;
default:
// this is an open-type property
break;
}

return $value;
}
}
Loading

0 comments on commit 61e7948

Please sign in to comment.