| @@ 26-71 (lines=46) @@ | ||
| 23 | * |
|
| 24 | * @author Kévin Dunglas <[email protected]> |
|
| 25 | */ |
|
| 26 | final class JsonHalEncoder implements EncoderInterface, DecoderInterface |
|
| 27 | { |
|
| 28 | const FORMAT = 'jsonhal'; |
|
| 29 | ||
| 30 | private $jsonEncoder; |
|
| 31 | ||
| 32 | public function __construct(JsonEncoder $jsonEncoder = null) |
|
| 33 | { |
|
| 34 | // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML. |
|
| 35 | $this->jsonEncoder = $jsonEncoder ?: new JsonEncoder( |
|
| 36 | new JsonEncode(JsonResponse::DEFAULT_ENCODING_OPTIONS), new JsonDecode(true) |
|
| 37 | ); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * {@inheritdoc} |
|
| 42 | */ |
|
| 43 | public function supportsEncoding($format) |
|
| 44 | { |
|
| 45 | return self::FORMAT === $format; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * {@inheritdoc} |
|
| 50 | */ |
|
| 51 | public function encode($data, $format, array $context = []) |
|
| 52 | { |
|
| 53 | return $this->jsonEncoder->encode($data, $format, $context); |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * {@inheritdoc} |
|
| 58 | */ |
|
| 59 | public function supportsDecoding($format) |
|
| 60 | { |
|
| 61 | return self::FORMAT === $format; |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * {@inheritdoc} |
|
| 66 | */ |
|
| 67 | public function decode($data, $format, array $context = []) |
|
| 68 | { |
|
| 69 | return $this->jsonEncoder->decode($data, $format, $context); |
|
| 70 | } |
|
| 71 | } |
|
| 72 | ||
| @@ 26-71 (lines=46) @@ | ||
| 23 | * |
|
| 24 | * @author Kévin Dunglas <[email protected]> |
|
| 25 | */ |
|
| 26 | final class JsonLdEncoder implements EncoderInterface, DecoderInterface |
|
| 27 | { |
|
| 28 | const FORMAT = 'jsonld'; |
|
| 29 | ||
| 30 | private $jsonEncoder; |
|
| 31 | ||
| 32 | public function __construct(JsonEncoder $jsonEncoder = null) |
|
| 33 | { |
|
| 34 | // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML. |
|
| 35 | $this->jsonEncoder = $jsonEncoder ?: new JsonEncoder( |
|
| 36 | new JsonEncode(JsonResponse::DEFAULT_ENCODING_OPTIONS), new JsonDecode(true) |
|
| 37 | ); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * {@inheritdoc} |
|
| 42 | */ |
|
| 43 | public function supportsEncoding($format) |
|
| 44 | { |
|
| 45 | return self::FORMAT === $format; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * {@inheritdoc} |
|
| 50 | */ |
|
| 51 | public function encode($data, $format, array $context = []) |
|
| 52 | { |
|
| 53 | return $this->jsonEncoder->encode($data, $format, $context); |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * {@inheritdoc} |
|
| 58 | */ |
|
| 59 | public function supportsDecoding($format) |
|
| 60 | { |
|
| 61 | return self::FORMAT === $format; |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * {@inheritdoc} |
|
| 66 | */ |
|
| 67 | public function decode($data, $format, array $context = []) |
|
| 68 | { |
|
| 69 | return $this->jsonEncoder->decode($data, $format, $context); |
|
| 70 | } |
|
| 71 | } |
|
| 72 | ||