From eb18e95932da8ab27746843329a07b9ba486b5b3 Mon Sep 17 00:00:00 2001 From: Dominik Zogg Date: Wed, 23 Oct 2019 14:55:46 +0200 Subject: [PATCH] add application/jsonx+xml --- src/Decoder/JsonxTypeDecoder.php | 22 +++++++++++++++- src/Provider/DeserializationProvider.php | 1 + tests/Unit/Decoder/JsonxTypeDecoderTest.php | 7 ++++++ .../Provider/DeserializationProviderTest.php | 25 +++++++++++++++---- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/Decoder/JsonxTypeDecoder.php b/src/Decoder/JsonxTypeDecoder.php index be83686..cef2364 100644 --- a/src/Decoder/JsonxTypeDecoder.php +++ b/src/Decoder/JsonxTypeDecoder.php @@ -18,12 +18,32 @@ final class JsonxTypeDecoder implements TypeDecoderInterface const DATATYPE_NUMBER = 'number'; const DATATYPE_NULL = 'null'; + /** + * @var string + */ + private $contentType; + + /** + * @param string $contentType + */ + public function __construct(string $contentType = 'application/x-jsonx') + { + if ('application/x-jsonx' === $contentType) { + @trigger_error( + 'Use "application/jsonx+xml" instead of "application/x-jsonx", cause jsonx is a xml variant.', + E_USER_DEPRECATED + ); + } + + $this->contentType = $contentType; + } + /** * @return string */ public function getContentType(): string { - return 'application/x-jsonx'; + return $this->contentType; } /** diff --git a/src/Provider/DeserializationProvider.php b/src/Provider/DeserializationProvider.php index f58a20d..14b058d 100644 --- a/src/Provider/DeserializationProvider.php +++ b/src/Provider/DeserializationProvider.php @@ -37,6 +37,7 @@ public function register(Container $container): void $decoderTypes[] = new JsonTypeDecoder(); $decoderTypes[] = new JsonxTypeDecoder(); + $decoderTypes[] = new JsonxTypeDecoder('application/jsonx+xml'); $decoderTypes[] = new UrlEncodedTypeDecoder(); $decoderTypes[] = new XmlTypeDecoder(); diff --git a/tests/Unit/Decoder/JsonxTypeDecoderTest.php b/tests/Unit/Decoder/JsonxTypeDecoderTest.php index 58dac97..43f93ff 100644 --- a/tests/Unit/Decoder/JsonxTypeDecoderTest.php +++ b/tests/Unit/Decoder/JsonxTypeDecoderTest.php @@ -21,6 +21,13 @@ public function testGetContentType(): void self::assertSame('application/x-jsonx', $decoder->getContentType()); } + public function testGetContentTypeWithFixedContentType(): void + { + $decoder = new JsonxTypeDecoder('application/jsonx+xml'); + + self::assertSame('application/jsonx+xml', $decoder->getContentType()); + } + /** * @dataProvider getExpectedData * diff --git a/tests/Unit/Provider/DeserializationProviderTest.php b/tests/Unit/Provider/DeserializationProviderTest.php index 5a855f3..a235d17 100644 --- a/tests/Unit/Provider/DeserializationProviderTest.php +++ b/tests/Unit/Provider/DeserializationProviderTest.php @@ -7,6 +7,7 @@ use Chubbyphp\Deserialization\Decoder\Decoder; use Chubbyphp\Deserialization\Decoder\JsonTypeDecoder; use Chubbyphp\Deserialization\Decoder\JsonxTypeDecoder; +use Chubbyphp\Deserialization\Decoder\TypeDecoderInterface; use Chubbyphp\Deserialization\Decoder\UrlEncodedTypeDecoder; use Chubbyphp\Deserialization\Decoder\XmlTypeDecoder; use Chubbyphp\Deserialization\Decoder\YamlTypeDecoder; @@ -48,11 +49,25 @@ public function testRegister(): void self::assertInstanceOf(Decoder::class, $container['deserializer.decoder']); self::assertIsArray($container['deserializer.decodertypes']); - self::assertInstanceOf(JsonTypeDecoder::class, $container['deserializer.decodertypes'][0]); - self::assertInstanceOf(JsonxTypeDecoder::class, $container['deserializer.decodertypes'][1]); - self::assertInstanceOf(UrlEncodedTypeDecoder::class, $container['deserializer.decodertypes'][2]); - self::assertInstanceOf(XmlTypeDecoder::class, $container['deserializer.decodertypes'][3]); - self::assertInstanceOf(YamlTypeDecoder::class, $container['deserializer.decodertypes'][4]); + + /** @var array $decoderTypes */ + $decoderTypes = $container['deserializer.decodertypes']; + + self::assertInstanceOf(JsonTypeDecoder::class, array_shift($decoderTypes)); + + $jsonxTypeDecoder1 = array_shift($decoderTypes); + self::assertInstanceOf(JsonxTypeDecoder::class, $jsonxTypeDecoder1); + + self::assertSame('application/x-jsonx', $jsonxTypeDecoder1->getContentType()); + + $jsonxTypeDecoder2 = array_shift($decoderTypes); + self::assertInstanceOf(JsonxTypeDecoder::class, $jsonxTypeDecoder2); + + self::assertSame('application/jsonx+xml', $jsonxTypeDecoder2->getContentType()); + + self::assertInstanceOf(UrlEncodedTypeDecoder::class, array_shift($decoderTypes)); + self::assertInstanceOf(XmlTypeDecoder::class, array_shift($decoderTypes)); + self::assertInstanceOf(YamlTypeDecoder::class, array_shift($decoderTypes)); self::assertInstanceOf( DenormalizerObjectMappingRegistry::class,