Skip to content

Commit

Permalink
Check for member types before hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Feb 14, 2017
1 parent 08388a7 commit 4957da5
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ ADDED:

CHANGED:

- Renamed some methods of `ExceptionFactoryInterface` which didn't end with `Exception` (e.g. `createRelationshipNotExists()` to `createRelationshipNotExistsException()`)

REMOVED:

FIXED:

- [#59](https://github.com/woohoolabs/yin/issues/59): Resource schema validating

## 2.0.0-beta1 - 2017-02-09

ADDED:
Expand Down
20 changes: 15 additions & 5 deletions src/JsonApi/Exception/DefaultExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function createQueryParamUnrecognizedException(
return new QueryParamUnrecognized($queryParamName);
}

public function createRelationshipNotExists(string $relationship): RelationshipNotExists
public function createRelationshipNotExistsException(string $relationship): RelationshipNotExists
{
return new RelationshipNotExists($relationship);
}
Expand Down Expand Up @@ -113,17 +113,27 @@ public function createRequestBodyInvalidJsonApiException(
return new RequestBodyInvalidJsonApi($request, $validationErrors, $includeOriginalBody);
}

public function createResourceIdentifierIdMissing(array $resourceIdentifier): ResourceIdentifierIdMissing
public function createResourceIdentifierIdInvalidException($id): ResourceIdentifierIdInvalid
{
return new ResourceIdentifierIdInvalid($id);
}

public function createResourceIdentifierIdMissingException(array $resourceIdentifier): ResourceIdentifierIdMissing
{
return new ResourceIdentifierIdMissing($resourceIdentifier);
}

public function createResourceIdentifierTypeMissing(array $resourceIdentifier): ResourceIdentifierTypeMissing
public function createResourceIdentifierTypeInvalidException($type): ResourceIdentifierTypeInvalid
{
return new ResourceIdentifierTypeInvalid($type);
}

public function createResourceIdentifierTypeMissingException(array $resourceIdentifier): ResourceIdentifierTypeMissing
{
return new ResourceIdentifierTypeMissing($resourceIdentifier);
}

public function createResourceIdInvalidException(string $id): ResourceIdInvalid
public function createResourceIdInvalidException($id): ResourceIdInvalid
{
return new ResourceIdInvalid($id);
}
Expand All @@ -144,7 +154,7 @@ public function createResourceNotFoundException(RequestInterface $request): Reso
}

public function createResourceTypeUnacceptableException(
string $currentType,
$currentType,
array $acceptedTypes
): ResourceTypeUnacceptable {
return new ResourceTypeUnacceptable($currentType, $acceptedTypes);
Expand Down
22 changes: 17 additions & 5 deletions src/JsonApi/Exception/ExceptionFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function createQueryParamUnrecognizedException(RequestInterface $request,
/**
* @return JsonApiExceptionInterface|Exception
*/
public function createRelationshipNotExists(string $relationship);
public function createRelationshipNotExistsException(string $relationship);

/**
* @return JsonApiExceptionInterface|Exception
Expand Down Expand Up @@ -102,19 +102,31 @@ public function createRequestBodyInvalidJsonApiException(
);

/**
* @param mixed $id
* @return JsonApiExceptionInterface|Exception
*/
public function createResourceIdentifierIdMissing(array $resourceIdentifier);
public function createResourceIdentifierIdInvalidException($id);

/**
* @return JsonApiExceptionInterface|Exception
*/
public function createResourceIdentifierTypeMissing(array $resourceIdentifier);
public function createResourceIdentifierIdMissingException(array $resourceIdentifier);

/**
* @param mixed $type
* @return JsonApiExceptionInterface|Exception
*/
public function createResourceIdInvalidException(string $id);
public function createResourceIdentifierTypeInvalidException($type);

/**
* @return JsonApiExceptionInterface|Exception
*/
public function createResourceIdentifierTypeMissingException(array $resourceIdentifier);

/**
* @return JsonApiExceptionInterface|Exception
*/
public function createResourceIdInvalidException($id);

/**
* @return JsonApiExceptionInterface|Exception
Expand All @@ -134,7 +146,7 @@ public function createResourceTypeMissingException();
/**
* @return JsonApiExceptionInterface|Exception
*/
public function createResourceTypeUnacceptableException(string $currentType, array $acceptedTypes);
public function createResourceTypeUnacceptableException($currentType, array $acceptedTypes);

/**
* @return JsonApiExceptionInterface|Exception
Expand Down
9 changes: 6 additions & 3 deletions src/JsonApi/Exception/ResourceIdInvalid.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
class ResourceIdInvalid extends JsonApiException
{
/**
* @var string
* @var mixed
*/
protected $id;

public function __construct(string $id)
{
parent::__construct("The resource ID '$id' is invalid!");
parent::__construct("The resource ID '$id' must be a string!");
$this->id = $id;
}

Expand All @@ -31,7 +31,10 @@ protected function getErrors(): array
];
}

public function getId(): string
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
Expand Down
37 changes: 37 additions & 0 deletions src/JsonApi/Exception/ResourceIdentifierIdInvalid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace WoohooLabs\Yin\JsonApi\Exception;

use WoohooLabs\Yin\JsonApi\Schema\Error;
use WoohooLabs\Yin\JsonApi\Schema\ErrorSource;

class ResourceIdentifierIdInvalid extends JsonApiException
{
/**
* @var mixed
*/
protected $id;

public function __construct($id)
{
parent::__construct("The resource ID '$id' must be a string!");
$this->id = $id;
}

protected function getErrors(): array
{
return [
Error::create()
->setStatus("400")
->setCode("RESOURCE_IDENTIFIER_ID_INVALID")
->setTitle("Resource identifier ID is invalid")
->setDetail("The resource ID '$this->id' must be a string!")
];
}

public function getId(): string
{
return $this->id;
}
}
37 changes: 37 additions & 0 deletions src/JsonApi/Exception/ResourceIdentifierTypeInvalid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace WoohooLabs\Yin\JsonApi\Exception;

use WoohooLabs\Yin\JsonApi\Schema\Error;
use WoohooLabs\Yin\JsonApi\Schema\ErrorSource;

class ResourceIdentifierTypeInvalid extends JsonApiException
{
/**
* @var mixed
*/
protected $type;

public function __construct($type)
{
parent::__construct("The resource type '$type' must be a string!");
$this->type = $type;
}

protected function getErrors(): array
{
return [
Error::create()
->setStatus("400")
->setCode("RESOURCE_IDENTIFIER_TYPE_INVALID")
->setTitle("Resource identifier type is invalid")
->setDetail("The resource type '$this->type' must be a string!")
];
}

public function getType(): string
{
return $this->type;
}
}
11 changes: 7 additions & 4 deletions src/JsonApi/Exception/ResourceTypeUnacceptable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ResourceTypeUnacceptable extends JsonApiException
{
/**
* @var string
* @var mixed
*/
protected $currentType;

Expand All @@ -18,9 +18,9 @@ class ResourceTypeUnacceptable extends JsonApiException
*/
protected $acceptedTypes;

public function __construct(string $currentType, array $acceptedTypes)
public function __construct($currentType, array $acceptedTypes)
{
parent::__construct("Resource type '$currentType' can't be accepted by the Hydrator!");
parent::__construct("Resource type '$currentType' is not a string or can't be accepted by the Hydrator!");
$this->currentType = $currentType;
$this->acceptedTypes = $acceptedTypes;
}
Expand All @@ -37,7 +37,10 @@ public function getErrors(): array
];
}

public function getCurrentType(): string
/**
* @return mixed
*/
public function getCurrentType()
{
return $this->currentType;
}
Expand Down
6 changes: 5 additions & 1 deletion src/JsonApi/Hydrator/CreateHydratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ protected function hydrateIdForCreate(
RequestInterface $request,
ExceptionFactoryInterface $exceptionFactory
) {
$id = isset($data["id"]) ? (string) $data["id"] : "";
if (empty($data["id"]) === false && is_string($data["id"]) === false) {
throw $exceptionFactory->createResourceIdInvalidException($data["id"]);
}

$id = empty($data["id"]) ? "" : $data["id"];
$this->validateClientGeneratedId($id, $request, $exceptionFactory);

if ($id === "") {
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApi/Hydrator/HydratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ protected function validateType(array $data, ExceptionFactoryInterface $exceptio

$acceptedType = $this->getAcceptedType();

if (is_string($acceptedType) === true && $data["type"] !== $acceptedType) {
if (is_string($acceptedType) && $data["type"] !== $acceptedType) {
throw $exceptionFactory->createResourceTypeUnacceptableException($data["type"], [$acceptedType]);
}

if (is_array($acceptedType) && in_array($data["type"], $acceptedType) === false) {
if (is_array($acceptedType) && in_array($data["type"], $acceptedType, true) === false) {
throw $exceptionFactory->createResourceTypeUnacceptableException($data["type"], $acceptedType);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/JsonApi/Hydrator/UpdateHydratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function hydrateForRelationshipUpdate(
$relationshipHydrators = $this->getRelationshipHydrator($domainObject);

if (isset($relationshipHydrators[$relationship]) === false) {
throw $exceptionFactory->createRelationshipNotExists($relationship);
throw $exceptionFactory->createRelationshipNotExistsException($relationship);
}

$relationshipHydrator = $relationshipHydrators[$relationship];
Expand All @@ -148,7 +148,11 @@ protected function hydrateIdForUpdate($domainObject, array $data, ExceptionFacto
throw $exceptionFactory->createResourceIdMissingException();
}

$result = $this->setId($domainObject, (string) $data["id"]);
if (is_string($data["id"]) === false) {
throw $exceptionFactory->createResourceIdInvalidException($data["id"]);
}

$result = $this->setId($domainObject, $data["id"]);
if ($result) {
$domainObject = $result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApi/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function validateQueryParams()
{
foreach ($this->getQueryParams() as $queryParamName => $queryParamValue) {
if (preg_match("/^([a-z]+)$/", $queryParamName) &&
in_array($queryParamName, ["fields", "include", "sort", "page", "filter"]) === false
in_array($queryParamName, ["fields", "include", "sort", "page", "filter"], true) === false
) {
throw $this->exceptionFactory->createQueryParamUnrecognizedException($this, $queryParamName);
}
Expand Down
16 changes: 12 additions & 4 deletions src/JsonApi/Schema/ResourceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ class ResourceIdentifier
*/
public static function fromArray(array $array, ExceptionFactoryInterface $exceptionFactory): ResourceIdentifier
{
if (isset($array["type"]) === false) {
throw $exceptionFactory->createResourceIdentifierTypeMissing($array);
if (empty($array["type"])) {
throw $exceptionFactory->createResourceIdentifierTypeMissingException($array);
}

if (isset($array["id"]) === false) {
throw $exceptionFactory->createResourceIdentifierIdMissing($array);
if (is_string($array["type"]) === false) {
throw $exceptionFactory->createResourceIdentifierTypeInvalidException($array["type"]);
}

if (empty($array["id"])) {
throw $exceptionFactory->createResourceIdentifierIdMissingException($array);
}

if (is_string($array["id"]) === false) {
throw $exceptionFactory->createResourceIdentifierIdInvalidException($array["id"]);
}

$resourceIdentifier = new self();
Expand Down

0 comments on commit 4957da5

Please sign in to comment.