Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Jun 10, 2015
1 parent c9042eb commit b74cf98
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 61 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"require": {
"php": ">=5.5.0",
"symfony/http-kernel": "~2.6.7|~2.7.0",
"neomerx/json-api": "~0.4.0"
"neomerx/json-api": "~0.5.0"
},
"require-dev": {
"phpunit/phpunit": "~4.6",
Expand Down
12 changes: 12 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ class Config

/** Default value for json_encode max depth */
const JSON_DEPTH_DEFAULT = 512;

/** If JSON API version should be shown in top-level 'jsonapi' section */
const JSON_IS_SHOW_VERSION = 'showVer';

/** Default value for 'show JSON API version' */
const JSON_IS_SHOW_VERSION_DEFAULT = false;

/** Config key for JSON API version meta information */
const JSON_VERSION_META = 'verMeta';

/** Config key for URL prefix that will be added to all document links which have $treatAsHref flag set to false */
const JSON_URL_PREFIX = 'urlPrefix';
}
18 changes: 18 additions & 0 deletions src/Errors/ExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
* limitations under the License.
*/

use \Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use \Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use \Neomerx\JsonApi\Contracts\Integration\ExceptionThrowerInterface;
use \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use \Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;

Expand All @@ -34,6 +36,14 @@ public function throwBadRequest()
throw new BadRequestHttpException();
}

/**
* @inheritdoc
*/
public function throwForbidden()
{
throw new AccessDeniedHttpException();
}

/**
* @inheritdoc
*/
Expand All @@ -42,6 +52,14 @@ public function throwNotAcceptable()
throw new NotAcceptableHttpException();
}

/**
* @inheritdoc
*/
public function throwConflict()
{
throw new ConflictHttpException();
}

/**
* @inheritdoc
*/
Expand Down
23 changes: 12 additions & 11 deletions src/Errors/JsonApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use \Exception;
use \RuntimeException;
use \Neomerx\JsonApi\Document\Error;
use \Neomerx\JsonApi\Contracts\Schema\LinkInterface;

/**
* @package Neomerx\Limoncello
Expand All @@ -31,21 +32,21 @@ class JsonApiException extends RuntimeException
private $error;

/**
* @param int|string|null $idx
* @param string|null $href
* @param string|null $status
* @param string|null $code
* @param string|null $title
* @param string|null $detail
* @param array|null $source
* @param array|null $meta
* @param Exception|null $previous
* @param int|string|null $idx
* @param LinkInterface|null $aboutLink
* @param string|null $status
* @param string|null $code
* @param string|null $title
* @param string|null $detail
* @param array|null $source
* @param array|null $meta
* @param Exception|null $previous
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
$idx = null,
$href = null,
LinkInterface $aboutLink = null,
$status = null,
$code = null,
$title = null,
Expand All @@ -55,7 +56,7 @@ public function __construct(
Exception $previous = null
) {
parent::__construct($title, 0, $previous);
$this->error = new Error($idx, $href, $status, $code, $title, $detail, $source, $meta);
$this->error = new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
}

/**
Expand Down
49 changes: 38 additions & 11 deletions src/Http/JsonApiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
use \Neomerx\JsonApi\Schema\SchemaFactory;
use \Neomerx\JsonApi\Decoders\ArrayDecoder;
use \Neomerx\Limoncello\Config\Config as C;
use \Neomerx\JsonApi\Encoder\EncoderOptions;
use \Neomerx\JsonApi\Document\DocumentFactory;
use \Neomerx\JsonApi\Encoder\JsonEncodeOptions;
use \Symfony\Component\HttpFoundation\Response;
use \Neomerx\Limoncello\Errors\ExceptionThrower;
use \Neomerx\JsonApi\Parameters\ParametersFactory;
use \Neomerx\JsonApi\Contracts\Schema\LinkInterface;
use \Neomerx\JsonApi\Encoder\Factory\EncoderFactory;
use \Neomerx\Limoncello\Contracts\IntegrationInterface;
use \Neomerx\JsonApi\Contracts\Schema\ContainerInterface;
Expand Down Expand Up @@ -231,6 +230,15 @@ protected function initCodecMatcher()
$depth = isset($config[C::JSON][C::JSON_DEPTH]) === true ?
$config[C::JSON][C::JSON_DEPTH] : C::JSON_DEPTH_DEFAULT;

$isShowVersion = isset($config[C::JSON][C::JSON_IS_SHOW_VERSION]) === true ?
$config[C::JSON][C::JSON_IS_SHOW_VERSION] : C::JSON_IS_SHOW_VERSION_DEFAULT;

$versionMeta = isset($config[C::JSON][C::JSON_VERSION_META]) === true ?
$config[C::JSON][C::JSON_VERSION_META] : null;

$urlPrefix = isset($config[C::JSON][C::JSON_URL_PREFIX]) === true ?
$config[C::JSON][C::JSON_URL_PREFIX] : null;

$encoderFactory = new EncoderFactory();
$parametersFactory = new ParametersFactory();
return new Encoder(
Expand All @@ -239,7 +247,7 @@ protected function initCodecMatcher()
$encoderFactory,
$parametersFactory,
$container,
new JsonEncodeOptions($options, $depth)
new EncoderOptions($options, $urlPrefix, $isShowVersion, $versionMeta, $depth)
);
};

Expand All @@ -248,6 +256,7 @@ protected function initCodecMatcher()
MediaTypeInterface::JSON_API_TYPE,
MediaTypeInterface::JSON_API_SUB_TYPE
);

$this->codecMatcher->registerEncoder($jsonApiType, $jsonApiEncoder);
$this->codecMatcher->registerDecoder($jsonApiType, function () {
return new ArrayDecoder();
Expand Down Expand Up @@ -307,6 +316,14 @@ protected function checkParameters()
$this->parametersChecked = true;
}

/**
* @return void
*/
protected function checkParametersEmpty()
{
$this->getParameters()->isEmpty() === true ?: $this->exceptionThrower->throwBadRequest();
}

/**
* @return ParametersInterface
*/
Expand Down Expand Up @@ -364,10 +381,10 @@ protected function getSupportedExtensions()
/**
* Get response with regular JSON API Document in body.
*
* @param object|array $data
* @param int $statusCode
* @param LinkInterface[]|null $links
* @param mixed $meta
* @param object|array $data
* @param int $statusCode
* @param array<string,\Neomerx\JsonApi\Contracts\Schema\LinkInterface>|null $links
* @param mixed $meta
*
* @return Response
*/
Expand All @@ -386,9 +403,9 @@ protected function getContentResponse(
}

/**
* @param object $resource
* @param LinkInterface[]|null $links
* @param mixed $meta
* @param object $resource
* @param array<string,\Neomerx\JsonApi\Contracts\Schema\LinkInterface>|null $links
* @param mixed $meta
*
* @return Response
*/
Expand All @@ -400,9 +417,11 @@ protected function getCreatedResponse(
$parameters = $this->getParameters();
$encoder = $this->codecMatcher->getEncoder();
$outputMediaType = $this->codecMatcher->getEncoderRegisteredMatchedType();
$location = $this->schemaContainer->getSchema($resource)->getSelfUrl($resource);
$content = $encoder->encode($resource, $links, $meta, $parameters);

$urlPrefix = $encoder->getEncoderOptions() === null ? null : $encoder->getEncoderOptions()->getUrlPrefix();
$location = $urlPrefix. $this->schemaContainer->getSchema($resource)->getSelfSubLink($resource)->getSubHref();

return $this->responses->getCreatedResponse($location, $outputMediaType, $content, $this->supportedExtensions);
}

Expand All @@ -415,4 +434,12 @@ protected function getCodecMatcher()
{
return $this->codecMatcher;
}

/**
* @return ExceptionThrowerInterface
*/
protected function getExceptionThrower()
{
return $this->exceptionThrower;
}
}
2 changes: 1 addition & 1 deletion tests/Data/FakeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FakeSchema extends SchemaProvider
/**
* @var string
*/
protected $baseSelfUrl = '/fake-items/';
protected $selfSubUrl = '/fake-items/';

/**
* @inheritdoc
Expand Down
20 changes: 20 additions & 0 deletions tests/Errors/ExceptionThrowerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,24 @@ public function testUnsupportedMediaTypeHttp()
{
$this->thrower->throwUnsupportedMediaType();
}

/**
* Test throw exception.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
public function testAccessDenied()
{
$this->thrower->throwForbidden();
}

/**
* Test throw exception.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\ConflictHttpException
*/
public function testConflict()
{
$this->thrower->throwConflict();
}
}
20 changes: 11 additions & 9 deletions tests/Errors/JsonApiExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
* limitations under the License.
*/

use \Neomerx\JsonApi\Schema\Link;
use \Neomerx\Tests\Limoncello\BaseTestCase;
use \Neomerx\Limoncello\Errors\JsonApiException;
use \Neomerx\JsonApi\Contracts\Document\DocumentInterface;

/**
* @package Neomerx\Tests\Limoncello
Expand All @@ -30,19 +32,19 @@ class JsonApiExceptionTest extends BaseTestCase
public function testExceptionProperties()
{
$exception = new JsonApiException(
$idx = 'some-id',
$href = 'some-href',
$status = 'some-status',
$code = 'some-code',
$title = 'some-title',
$detail = 'some-detail',
$source = ['source' => 'info'],
$meta = ['meta' => 'info']
$idx = 'some-id',
$aboutLink = new Link('about-link'),
$status = 'some-status',
$code = 'some-code',
$title = 'some-title',
$detail = 'some-detail',
$source = ['source' => 'info'],
$meta = ['meta' => 'info']
);

$this->assertNotNull($error = $exception->getError());
$this->assertEquals($idx, $error->getId());
$this->assertEquals($href, $error->getHref());
$this->assertEquals([DocumentInterface::KEYWORD_ERRORS_ABOUT => $aboutLink], $error->getLinks());
$this->assertEquals($status, $error->getStatus());
$this->assertEquals($code, $error->getCode());
$this->assertEquals($title, $error->getTitle());
Expand Down
Loading

0 comments on commit b74cf98

Please sign in to comment.