Skip to content

Commit

Permalink
Merge pull request #5 from moafred/integrate-yin-3.0
Browse files Browse the repository at this point in the history
feat: Yin 3.0 integration
  • Loading branch information
moafred committed Jan 19, 2018
2 parents 072d363 + 4f1e2bf commit ffd1405
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 65 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: php
php:
- '5.5'
- '5.6'
- '7.1'
- '7.2'

install: composer install

script: vendor/bin/phpspec run -n -f dot
script: composer test
21 changes: 12 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@
}
],
"require": {
"php": ">=5.5",
"symfony/psr-http-message-bridge": "^0.2.0",
"php": "^7.1.0",
"symfony/psr-http-message-bridge": "^1.0.2",
"zendframework/zend-diactoros": "^1.3",
"woohoolabs/yin": "^0.11.0"
"woohoolabs/yin": "^3.0"
},
"require-dev": {
"fabpot/php-cs-fixer": "^1.11",
"phpspec/phpspec": "^2.5",
"sensio/framework-extra-bundle": "^3.0"
},
"suggest": {
"sensio/framework-extra-bundle": "Allows you to use ParamConverter and access to JsonApi instance from your controllers"
"friendsofphp/php-cs-fixer": "^2.0",
"phpspec/phpspec": "^4.3",
"sensio/framework-extra-bundle": "^5.1"
},
"autoload": {
"psr-4": {
"QP\\WoohoolabsYinBundle\\": "src/"
}
},
"scripts": {
"phpspec": "vendor/bin/phpspec run -n -f dot",
"test": [
"@phpspec"
]
}
}
107 changes: 58 additions & 49 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use WoohooLabs\Yin\JsonApi\Request\Pagination\PagePagination;
use WoohooLabs\Yin\JsonApi\Schema\ResourceIdentifier;
use WoohooLabs\Yin\JsonApi\Request\RequestInterface;
use WoohooLabs\Yin\JsonApi\Request\Pagination\FixedPageBasedPagination;
use WoohooLabs\Yin\JsonApi\Request\Pagination\PageBasedPagination;
use WoohooLabs\Yin\JsonApi\Request\Pagination\OffsetBasedPagination;
use WoohooLabs\Yin\JsonApi\Request\Pagination\CursorBasedPagination;

class Request implements RequestInterface
{
Expand Down Expand Up @@ -56,7 +60,9 @@ class Request implements RequestInterface
protected $filtering;

/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* Request constructor.
* @param ServerRequestInterface $request
* @param ExceptionFactoryInterface $exceptionFactory
*/
public function __construct(ServerRequestInterface $request, ExceptionFactoryInterface $exceptionFactory)
{
Expand All @@ -67,7 +73,7 @@ public function __construct(ServerRequestInterface $request, ExceptionFactoryInt
/**
* @throws \WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnsupported
*/
public function validateContentTypeHeader()
public function validateContentTypeHeader(): void
{
if ($this->isValidMediaTypeHeader("Content-Type") === false) {
throw new MediaTypeUnsupported($this->getHeaderLine("Content-Type"));
Expand All @@ -77,7 +83,7 @@ public function validateContentTypeHeader()
/**
* @throws \WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnacceptable
*/
public function validateAcceptHeader()
public function validateAcceptHeader(): void
{
if ($this->isValidMediaTypeHeader("Accept") === false) {
throw new MediaTypeUnacceptable($this->getHeaderLine("Accept"));
Expand All @@ -87,7 +93,7 @@ public function validateAcceptHeader()
/**
* @throws \WoohooLabs\Yin\JsonApi\Exception\QueryParamUnrecognized
*/
public function validateQueryParams()
public function validateQueryParams(): void
{
foreach ($this->getQueryParams() as $queryParamName => $queryParamValue) {
if (preg_match("/^([a-z]+)$/", $queryParamName) &&
Expand All @@ -104,13 +110,14 @@ public function validateQueryParams()
* @param string $headerName
* @return bool
*/
protected function isValidMediaTypeHeader($headerName)
protected function isValidMediaTypeHeader(string $headerName): bool
{
$header = $this->getHeaderLine($headerName);
return (strpos($header, "application/vnd.api+json") === false || $header === "application/vnd.api+json");
}

protected function setIncludedFields()

protected function setIncludedFields(): void
{
$this->includedFields = [];
$fields = $this->getQueryParam("fields", []);
Expand All @@ -129,7 +136,7 @@ protected function setIncludedFields()
* @param string $resourceType
* @return array
*/
public function getIncludedFields($resourceType)
public function getIncludedFields(string $resourceType): array
{
if ($this->includedFields === null) {
$this->setIncludedFields();
Expand All @@ -143,7 +150,7 @@ public function getIncludedFields($resourceType)
* @param string $field
* @return bool
*/
public function isIncludedField($resourceType, $field)
public function isIncludedField(string $resourceType, string $field): bool
{
if ($this->includedFields === null) {
$this->setIncludedFields();
Expand All @@ -160,7 +167,7 @@ public function isIncludedField($resourceType, $field)
return isset($this->includedFields[$resourceType][$field]);
}

protected function setIncludedRelationships()
protected function setIncludedRelationships(): void
{
$this->includedRelationships = [];

Expand Down Expand Up @@ -193,7 +200,7 @@ protected function setIncludedRelationships()
/**
* @return bool
*/
public function hasIncludedRelationships()
public function hasIncludedRelationships(): bool
{
if ($this->includedRelationships === null) {
$this->setIncludedRelationships();
Expand All @@ -206,7 +213,7 @@ public function hasIncludedRelationships()
* @param string $baseRelationshipPath
* @return array
*/
public function getIncludedRelationships($baseRelationshipPath)
public function getIncludedRelationships(string $baseRelationshipPath): array
{
if ($this->includedRelationships === null) {
$this->setIncludedRelationships();
Expand All @@ -225,7 +232,11 @@ public function getIncludedRelationships($baseRelationshipPath)
* @param array $defaultRelationships
* @return bool
*/
public function isIncludedRelationship($baseRelationshipPath, $relationshipName, array $defaultRelationships)
public function isIncludedRelationship(
string $baseRelationshipPath,
string $relationshipName,
array $defaultRelationships
): bool
{
if ($this->includedRelationships === null) {
$this->setIncludedRelationships();
Expand All @@ -242,7 +253,7 @@ public function isIncludedRelationship($baseRelationshipPath, $relationshipName,
return isset($this->includedRelationships[$baseRelationshipPath][$relationshipName]);
}

protected function setSorting()
protected function setSorting(): void
{
$sortingQueryParam = $this->getQueryParam("sort", "");
if ($sortingQueryParam === "") {
Expand All @@ -257,7 +268,7 @@ protected function setSorting()
/**
* @return array
*/
public function getSorting()
public function getSorting(): array
{
if ($this->sorting === null) {
$this->setSorting();
Expand All @@ -266,7 +277,7 @@ public function getSorting()
return $this->sorting;
}

protected function setPagination()
protected function setPagination(): void
{
$pagination = $this->getQueryParam("page", null);
$this->pagination = is_array($pagination) ? $pagination : [];
Expand All @@ -275,7 +286,7 @@ protected function setPagination()
/**
* @return array
*/
public function getPagination()
public function getPagination(): array
{
if ($this->pagination === null) {
$this->setPagination();
Expand All @@ -285,30 +296,33 @@ public function getPagination()
}

/**
* @param mixed $defaultPage
* @return \WoohooLabs\Yin\JsonApi\Request\Pagination\FixedPagePagination
* @param int|null $defaultPage
* @return FixedPageBasedPagination
*/
public function getFixedPageBasedPagination($defaultPage = null)
public function getFixedPageBasedPagination(?int $defaultPage = null): FixedPageBasedPagination
{
return FixedPagePagination::fromPaginationQueryParams($this->getPagination(), $defaultPage);
}

/**
* @param mixed $defaultPage
* @param mixed $defaultSize
* @return \WoohooLabs\Yin\JsonApi\Request\Pagination\PagePagination
* @param int|null $defaultPage
* @param int|null $defaultSize
* @return PageBasedPagination
*/
public function getPageBasedPagination($defaultPage = null, $defaultSize = null)
public function getPageBasedPagination(?int $defaultPage = null, ?int $defaultSize = null): PageBasedPagination
{
return PagePagination::fromPaginationQueryParams($this->getPagination(), $defaultPage, $defaultSize);
}

/**
* @param mixed $defaultOffset
* @param mixed $defaultLimit
* @return \WoohooLabs\Yin\JsonApi\Request\Pagination\OffsetPagination
* @param int|null $defaultOffset
* @param int|null $defaultLimit
* @return OffsetBasedPagination
*/
public function getOffsetBasedPagination($defaultOffset = null, $defaultLimit = null)
public function getOffsetBasedPagination(
?int $defaultOffset = null,
?int $defaultLimit = null
): OffsetBasedPagination
{
return OffsetPagination::fromPaginationQueryParams($this->getPagination(), $defaultOffset, $defaultLimit);
}
Expand All @@ -317,21 +331,21 @@ public function getOffsetBasedPagination($defaultOffset = null, $defaultLimit =
* @param mixed $defaultCursor
* @return \WoohooLabs\Yin\JsonApi\Request\Pagination\CursorPagination
*/
public function getCursorBasedPagination($defaultCursor = null)
public function getCursorBasedPagination($defaultCursor = null): CursorBasedPagination
{
return CursorPagination::fromPaginationQueryParams($this->getPagination(), $defaultCursor);
}

protected function setFiltering()
protected function setFiltering(): void
{
$filtering = $this->getQueryParam("filter", []);
$this->filtering = is_array($filtering) ? $filtering : [];
}

/**
* @return array
* @inheritdoc
*/
public function getFiltering()
public function getFiltering(): array
{
if ($this->filtering === null) {
$this->setFiltering();
Expand All @@ -341,11 +355,9 @@ public function getFiltering()
}

/**
* @param string $param
* @param mixed $default
* @return mixed
* @inheritdoc
*/
public function getFilteringParam($param, $default = null)
public function getFilteringParam(string $param, $default = null)
{
$filtering = $this->getFiltering();

Expand All @@ -357,7 +369,7 @@ public function getFilteringParam($param, $default = null)
* @param mixed $default
* @return array|string|mixed
*/
public function getQueryParam($name, $default = null)
public function getQueryParam(string $name, $default = null)
{
$queryParams = $this->serverRequest->getQueryParams();

Expand All @@ -369,9 +381,9 @@ public function getQueryParam($name, $default = null)
*
* @param string $name
* @param mixed $value
* @return $this
* @return $this|Request
*/
public function withQueryParam($name, $value)
public function withQueryParam(string $name, $value)
{
$self = clone $this;
$queryParams = $this->serverRequest->getQueryParams();
Expand All @@ -381,7 +393,7 @@ public function withQueryParam($name, $value)
return $self;
}

protected function initializeParsedQueryParams()
protected function initializeParsedQueryParams(): void
{
$this->includedFields = null;
$this->includedRelationships = null;
Expand All @@ -391,8 +403,7 @@ protected function initializeParsedQueryParams()
}

/**
* @param mixed $default
* @return array|mixed
* @inheritdoc
*/
public function getResource($default = null)
{
Expand All @@ -401,8 +412,7 @@ public function getResource($default = null)
}

/**
* @param mixed $default
* @return string|mixed
* @inheritdoc
*/
public function getResourceType($default = null)
{
Expand All @@ -412,8 +422,7 @@ public function getResourceType($default = null)
}

/**
* @param mixed $default
* @return string|mixed
* @inheritdoc
*/
public function getResourceId($default = null)
{
Expand All @@ -425,7 +434,7 @@ public function getResourceId($default = null)
/**
* @return array
*/
public function getResourceAttributes()
public function getResourceAttributes(): array
{
$data = $this->getResource();

Expand All @@ -437,7 +446,7 @@ public function getResourceAttributes()
* @param mixed $default
* @return mixed
*/
public function getResourceAttribute($attribute, $default = null)
public function getResourceAttribute(string $attribute, $default = null)
{
$attributes = $this->getResourceAttributes();

Expand All @@ -448,7 +457,7 @@ public function getResourceAttribute($attribute, $default = null)
* @param string $relationship
* @return \WoohooLabs\Yin\JsonApi\Hydrator\Relationship\ToOneRelationship|null
*/
public function getToOneRelationship($relationship)
public function getToOneRelationship(string $relationship): ?ToOneRelationship
{
$data = $this->getResource();

Expand All @@ -472,7 +481,7 @@ public function getToOneRelationship($relationship)
* @param string $relationship
* @return \WoohooLabs\Yin\JsonApi\Hydrator\Relationship\ToManyRelationship|null
*/
public function getToManyRelationship($relationship)
public function getToManyRelationship(string $relationship): ?ToManyRelationship
{
$data = $this->getResource();

Expand Down
Loading

0 comments on commit ffd1405

Please sign in to comment.