Skip to content

Commit

Permalink
add application/jsonx+xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Oct 23, 2019
1 parent 9b89832 commit f7462cb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/Decoder/JsonxTypeDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ($contentType === 'application/x-jsonx') {
@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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Provider/DeserializationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function register(Container $container): void
$decoderTypes = [];

$decoderTypes[] = new JsonTypeDecoder();
$decoderTypes[] = new JsonxTypeDecoder('application/jsonx+xml');
$decoderTypes[] = new JsonxTypeDecoder();
$decoderTypes[] = new UrlEncodedTypeDecoder();
$decoderTypes[] = new XmlTypeDecoder();
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Decoder/JsonxTypeDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
16 changes: 11 additions & 5 deletions tests/Unit/Provider/DeserializationProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,11 +49,16 @@ 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<int, TypeDecoderInterface> $decoderTypes */
$decoderTypes = $container['deserializer.decodertypes'];

self::assertInstanceOf(JsonTypeDecoder::class, array_shift($decoderTypes));
self::assertInstanceOf(JsonxTypeDecoder::class, array_shift($decoderTypes));
self::assertInstanceOf(JsonxTypeDecoder::class, array_shift($decoderTypes));
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,
Expand Down

0 comments on commit f7462cb

Please sign in to comment.