‪TYPO3CMS  11.5
JsonResponse.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
28 {
38  public const ‪DEFAULT_JSON_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES;
39 
57  public function ‪__construct(
58  ?array $data = [],
59  int $status = 200,
60  array ‪$headers = [],
61  int $encodingOptions = self::DEFAULT_JSON_FLAGS
62  ) {
63  ‪$body = new ‪Stream('php://temp', 'wb+');
64  parent::__construct(‪$body, $status, ‪$headers);
65 
66  if ($data !== null) {
67  $this->‪setPayload($data, $encodingOptions);
68  }
69 
70  // Ensure that application/json header is set, if Content-Type was not set before
71  if (!$this->‪hasHeader('Content-Type')) {
72  $this->headers['Content-Type'][] = 'application/json; charset=utf-8';
73  $this->lowercasedHeaderNames['content-type'] = 'Content-Type';
74  }
75  }
76 
84  public function ‪setPayload(array $data = [], int $encodingOptions = self::DEFAULT_JSON_FLAGS): ‪JsonResponse
85  {
86  $this->body->write($this->‪jsonEncode($data, $encodingOptions));
87  $this->body->rewind();
88  return $this;
89  }
90 
96  private function ‪jsonEncode(array $data, int $encodingOptions): string
97  {
98  // Clear json_last_error()
99  json_encode(null);
100  $json = json_encode($data, $encodingOptions);
101  if (json_last_error() !== JSON_ERROR_NONE) {
102  throw new \InvalidArgumentException(sprintf(
103  'Unable to encode data to JSON in %s: %s',
104  __CLASS__,
105  json_last_error_msg()
106  ), 1504972434);
107  }
108  return $json;
109  }
110 }
‪TYPO3\CMS\Core\Http\JsonResponse\setPayload
‪$this setPayload(array $data=[], int $encodingOptions=self::DEFAULT_JSON_FLAGS)
Definition: JsonResponse.php:84
‪TYPO3\CMS\Core\Http\JsonResponse\__construct
‪__construct(?array $data=[], int $status=200, array $headers=[], int $encodingOptions=self::DEFAULT_JSON_FLAGS)
Definition: JsonResponse.php:57
‪TYPO3\CMS\Core\Http\JsonResponse\jsonEncode
‪jsonEncode(array $data, int $encodingOptions)
Definition: JsonResponse.php:96
‪TYPO3\CMS\Core\Http\Message\hasHeader
‪bool hasHeader($name)
Definition: Message.php:123
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:30
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:29
‪TYPO3\CMS\Core\Http\JsonResponse\DEFAULT_JSON_FLAGS
‪const DEFAULT_JSON_FLAGS
Definition: JsonResponse.php:38
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪TYPO3\CMS\Core\Http\Message\$body
‪StreamInterface null $body
Definition: Message.php:51
‪TYPO3\CMS\Core\Http\Message\$headers
‪array $headers
Definition: Message.php:40
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18