From 9a2c768404c7d9cc57c457271f1462b977140a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 11 Jul 2016 18:08:06 +0200 Subject: [PATCH] Factorize the JSON encoder --- .../Symfony/Bundle/Resources/config/hal.xml | 4 +- .../Bundle/Resources/config/jsonld.xml | 4 +- src/JsonLd/Serializer/JsonLdEncoder.php | 71 ------------------- .../JsonEncoder.php} | 19 ++--- tests/Hal/Serializer/JsonHalEncoderTest.php | 55 -------------- .../JsonEncoderTest.php} | 18 ++--- 6 files changed, 25 insertions(+), 146 deletions(-) delete mode 100644 src/JsonLd/Serializer/JsonLdEncoder.php rename src/{Hal/Serializer/JsonHalEncoder.php => Serializer/JsonEncoder.php} (74%) delete mode 100644 tests/Hal/Serializer/JsonHalEncoderTest.php rename tests/{JsonLd/Serializer/JsonLdEncoderTest.php => Serializer/JsonEncoderTest.php} (64%) diff --git a/src/Bridge/Symfony/Bundle/Resources/config/hal.xml b/src/Bridge/Symfony/Bundle/Resources/config/hal.xml index 30fb69fb835..fecfca49df5 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/hal.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/hal.xml @@ -52,7 +52,9 @@ - + + jsonhal + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/jsonld.xml b/src/Bridge/Symfony/Bundle/Resources/config/jsonld.xml index 0fac8924055..3bce669946e 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/jsonld.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/jsonld.xml @@ -29,7 +29,9 @@ - + + jsonld + diff --git a/src/JsonLd/Serializer/JsonLdEncoder.php b/src/JsonLd/Serializer/JsonLdEncoder.php deleted file mode 100644 index ba33f495bf9..00000000000 --- a/src/JsonLd/Serializer/JsonLdEncoder.php +++ /dev/null @@ -1,71 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ApiPlatform\Core\JsonLd\Serializer; - -use Symfony\Component\HttpFoundation\JsonResponse; -use Symfony\Component\Serializer\Encoder\DecoderInterface; -use Symfony\Component\Serializer\Encoder\EncoderInterface; -use Symfony\Component\Serializer\Encoder\JsonDecode; -use Symfony\Component\Serializer\Encoder\JsonEncode; -use Symfony\Component\Serializer\Encoder\JsonEncoder; - -/** - * JSON-LD Encoder. - * - * @author Kévin Dunglas - */ -final class JsonLdEncoder implements EncoderInterface, DecoderInterface -{ - const FORMAT = 'jsonld'; - - private $jsonEncoder; - - public function __construct(JsonEncoder $jsonEncoder = null) - { - // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML. - $this->jsonEncoder = $jsonEncoder ?: new JsonEncoder( - new JsonEncode(JsonResponse::DEFAULT_ENCODING_OPTIONS), new JsonDecode(true) - ); - } - - /** - * {@inheritdoc} - */ - public function supportsEncoding($format) - { - return self::FORMAT === $format; - } - - /** - * {@inheritdoc} - */ - public function encode($data, $format, array $context = []) - { - return $this->jsonEncoder->encode($data, $format, $context); - } - - /** - * {@inheritdoc} - */ - public function supportsDecoding($format) - { - return self::FORMAT === $format; - } - - /** - * {@inheritdoc} - */ - public function decode($data, $format, array $context = []) - { - return $this->jsonEncoder->decode($data, $format, $context); - } -} diff --git a/src/Hal/Serializer/JsonHalEncoder.php b/src/Serializer/JsonEncoder.php similarity index 74% rename from src/Hal/Serializer/JsonHalEncoder.php rename to src/Serializer/JsonEncoder.php index cc1ef2cfac8..53a474e0529 100644 --- a/src/Hal/Serializer/JsonHalEncoder.php +++ b/src/Serializer/JsonEncoder.php @@ -9,30 +9,31 @@ * file that was distributed with this source code. */ -namespace ApiPlatform\Core\Hal\Serializer; +namespace ApiPlatform\Core\Serializer; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Serializer\Encoder\DecoderInterface; use Symfony\Component\Serializer\Encoder\EncoderInterface; use Symfony\Component\Serializer\Encoder\JsonDecode; use Symfony\Component\Serializer\Encoder\JsonEncode; -use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder; /** * JSON-LD Encoder. * * @author Kévin Dunglas */ -final class JsonHalEncoder implements EncoderInterface, DecoderInterface +final class JsonEncoder implements EncoderInterface, DecoderInterface { - const FORMAT = 'jsonhal'; - + private $format; private $jsonEncoder; - public function __construct(JsonEncoder $jsonEncoder = null) + public function __construct(string $format, BaseJsonEncoder $jsonEncoder = null) { + $this->format = $format; + // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML. - $this->jsonEncoder = $jsonEncoder ?: new JsonEncoder( + $this->jsonEncoder = $jsonEncoder ?: new BaseJsonEncoder( new JsonEncode(JsonResponse::DEFAULT_ENCODING_OPTIONS), new JsonDecode(true) ); } @@ -42,7 +43,7 @@ public function __construct(JsonEncoder $jsonEncoder = null) */ public function supportsEncoding($format) { - return self::FORMAT === $format; + return $this->format === $format; } /** @@ -58,7 +59,7 @@ public function encode($data, $format, array $context = []) */ public function supportsDecoding($format) { - return self::FORMAT === $format; + return $this->format === $format; } /** diff --git a/tests/Hal/Serializer/JsonHalEncoderTest.php b/tests/Hal/Serializer/JsonHalEncoderTest.php deleted file mode 100644 index 3fef2d6683b..00000000000 --- a/tests/Hal/Serializer/JsonHalEncoderTest.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ApiPlatform\Core\Tests\Hal\Serializer; - -use ApiPlatform\Core\Hal\Serializer\JsonHalEncoder; - -/** - * @author Kévin Dunglas - * @author Amrouche Hamza - */ -class JsonHalEncoderTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var JsonHalEncoder - */ - private $encoder; - - public function setUp() - { - $this->encoder = new JsonHalEncoder(); - } - - public function testSupportEncoding() - { - $this->assertTrue($this->encoder->supportsEncoding(JsonHalEncoder::FORMAT)); - $this->assertFalse($this->encoder->supportsEncoding('csv')); - } - - public function testEncode() - { - $data = ['foo' => 'bar']; - - $this->assertEquals('{"foo":"bar"}', $this->encoder->encode($data, JsonHalEncoder::FORMAT)); - } - - public function testSupportDecoding() - { - $this->assertTrue($this->encoder->supportsDecoding(JsonHalEncoder::FORMAT)); - $this->assertFalse($this->encoder->supportsDecoding('csv')); - } - - public function testDecode() - { - $this->assertEquals(['foo' => 'bar'], $this->encoder->decode('{"foo":"bar"}', JsonHalEncoder::FORMAT)); - } -} diff --git a/tests/JsonLd/Serializer/JsonLdEncoderTest.php b/tests/Serializer/JsonEncoderTest.php similarity index 64% rename from tests/JsonLd/Serializer/JsonLdEncoderTest.php rename to tests/Serializer/JsonEncoderTest.php index bd667a19215..04a2fb7e02e 100644 --- a/tests/JsonLd/Serializer/JsonLdEncoderTest.php +++ b/tests/Serializer/JsonEncoderTest.php @@ -9,28 +9,28 @@ * file that was distributed with this source code. */ -namespace ApiPlatform\Core\Tests\JsonLd\Serializer; +namespace ApiPlatform\Core\Tests\Serializer; -use ApiPlatform\Core\JsonLd\Serializer\JsonLdEncoder; +use ApiPlatform\Core\Serializer\JsonEncoder; /** * @author Kévin Dunglas */ -class JsonLdEncoderTest extends \PHPUnit_Framework_TestCase +class JsonEncoderTest extends \PHPUnit_Framework_TestCase { /** - * @var JsonLdEncoder + * @var JsonEncoder */ private $encoder; public function setUp() { - $this->encoder = new JsonLdEncoder(); + $this->encoder = new JsonEncoder('json'); } public function testSupportEncoding() { - $this->assertTrue($this->encoder->supportsEncoding(JsonLdEncoder::FORMAT)); + $this->assertTrue($this->encoder->supportsEncoding('json')); $this->assertFalse($this->encoder->supportsEncoding('csv')); } @@ -38,17 +38,17 @@ public function testEncode() { $data = ['foo' => 'bar']; - $this->assertEquals('{"foo":"bar"}', $this->encoder->encode($data, JsonLdEncoder::FORMAT)); + $this->assertEquals('{"foo":"bar"}', $this->encoder->encode($data, 'json')); } public function testSupportDecoding() { - $this->assertTrue($this->encoder->supportsDecoding(JsonLdEncoder::FORMAT)); + $this->assertTrue($this->encoder->supportsDecoding('json')); $this->assertFalse($this->encoder->supportsDecoding('csv')); } public function testDecode() { - $this->assertEquals(['foo' => 'bar'], $this->encoder->decode('{"foo":"bar"}', JsonLdEncoder::FORMAT)); + $this->assertEquals(['foo' => 'bar'], $this->encoder->decode('{"foo":"bar"}', 'json')); } }