| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Encoder; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class JsonEncoder implements EncoderInterface, DecoderInterface |
| 20 |
{ |
| 21 |
public const FORMAT = 'json'; |
| 22 |
|
| 23 |
protected JsonEncode $encodingImpl; |
| 24 |
protected JsonDecode $decodingImpl; |
| 25 |
|
| 26 |
private array $defaultContext = [ |
| 27 |
JsonDecode::ASSOCIATIVE => true, |
| 28 |
]; |
| 29 |
|
| 30 |
public function __construct(?JsonEncode $encodingImpl = null, ?JsonDecode $decodingImpl = null, array $defaultContext = []) |
| 31 |
{ |
| 32 |
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); |
| 33 |
$this->encodingImpl = $encodingImpl ?? new JsonEncode($this->defaultContext); |
| 34 |
$this->decodingImpl = $decodingImpl ?? new JsonDecode($this->defaultContext); |
| 35 |
} |
| 36 |
|
| 37 |
public function encode(mixed $data, string $format, array $context = []): string |
| 38 |
{ |
| 39 |
$context = array_merge($this->defaultContext, $context); |
| 40 |
|
| 41 |
return $this->encodingImpl->encode($data, self::FORMAT, $context); |
| 42 |
} |
| 43 |
|
| 44 |
public function decode(string $data, string $format, array $context = []): mixed |
| 45 |
{ |
| 46 |
$context = array_merge($this->defaultContext, $context); |
| 47 |
|
| 48 |
return $this->decodingImpl->decode($data, self::FORMAT, $context); |
| 49 |
} |
| 50 |
|
| 51 |
public function supportsEncoding(string $format): bool |
| 52 |
{ |
| 53 |
return self::FORMAT === $format; |
| 54 |
} |
| 55 |
|
| 56 |
public function supportsDecoding(string $format): bool |
| 57 |
{ |
| 58 |
return self::FORMAT === $format; |
| 59 |
} |
| 60 |
} |