Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/hal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
<tag name="serializer.normalizer" />
</service>

<service id="api_platform.hal.encoder" class="ApiPlatform\Core\Hal\Serializer\JsonHalEncoder" public="false">
<service id="api_platform.hal.encoder" class="ApiPlatform\Core\Serializer\JsonEncoder" public="false">
<argument>jsonhal</argument>

<tag name="serializer.encoder" />
</service>

Expand Down
4 changes: 3 additions & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/jsonld.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
<tag name="serializer.normalizer" />
</service>

<service id="api_platform.jsonld.encoder" class="ApiPlatform\Core\JsonLd\Serializer\JsonLdEncoder" public="false">
<service id="api_platform.jsonld.encoder" class="ApiPlatform\Core\Serializer\JsonEncoder" public="false">
<argument>jsonld</argument>

<tag name="serializer.encoder" />
</service>

Expand Down
71 changes: 0 additions & 71 deletions src/JsonLd/Serializer/JsonLdEncoder.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 <dunglas@gmail.com>
*/
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)
);
}
Expand All @@ -42,7 +43,7 @@ public function __construct(JsonEncoder $jsonEncoder = null)
*/
public function supportsEncoding($format)
{
return self::FORMAT === $format;
return $this->format === $format;
}

/**
Expand All @@ -58,7 +59,7 @@ public function encode($data, $format, array $context = [])
*/
public function supportsDecoding($format)
{
return self::FORMAT === $format;
return $this->format === $format;
}

/**
Expand Down
55 changes: 0 additions & 55 deletions tests/Hal/Serializer/JsonHalEncoderTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,46 @@
* 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 <dunglas@gmail.com>
*/
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'));
}

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'));
}
}