comparison core/modules/serialization/src/Encoder/JsonEncoder.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
26 26
27 /** 27 /**
28 * {@inheritdoc} 28 * {@inheritdoc}
29 */ 29 */
30 public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) { 30 public function __construct(JsonEncode $encodingImpl = NULL, JsonDecode $decodingImpl = NULL) {
31 $this->encodingImpl = $encodingImpl ?: $this->getJsonEncode();
32 $this->decodingImpl = $decodingImpl ?: $this->getJsonDecode();
33 }
34
35 /**
36 * Instantiates a JsonEncode instance.
37 *
38 * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
39 * once Drupal requires Symfony 4.2 or higher.
40 */
41 private function getJsonEncode() {
31 // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be 42 // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be
32 // embedded into HTML. 43 // embedded into HTML.
33 // @see \Symfony\Component\HttpFoundation\JsonResponse 44 // @see \Symfony\Component\HttpFoundation\JsonResponse
34 $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT; 45 $json_encoding_options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
35 $this->encodingImpl = $encodingImpl ?: new JsonEncode($json_encoding_options); 46 $reflection = new \ReflectionClass(JsonEncode::class);
36 $this->decodingImpl = $decodingImpl ?: new JsonDecode(TRUE); 47 if (array_key_exists('OPTIONS', $reflection->getConstants())) {
48 return new JsonEncode([JsonEncode::OPTIONS => $json_encoding_options]);
49 }
50 return new JsonEncode($json_encoding_options);
51 }
52
53 /**
54 * Instantiates a JsonDecode instance.
55 *
56 * @internal this exists to bridge Symfony 3 to Symfony 4, and can be removed
57 * once Drupal requires Symfony 4.2 or higher.
58 */
59 private function getJsonDecode() {
60 $reflection = new \ReflectionClass(JsonDecode::class);
61 if (array_key_exists('ASSOCIATIVE', $reflection->getConstants())) {
62 return new JsonDecode([JsonDecode::ASSOCIATIVE => TRUE]);
63 }
64 return new JsonDecode(TRUE);
37 } 65 }
38 66
39 /** 67 /**
40 * {@inheritdoc} 68 * {@inheritdoc}
41 */ 69 */