‪TYPO3CMS  ‪main
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 
80  public function ‪setPayload(array $data = [], int $encodingOptions = self::DEFAULT_JSON_FLAGS): ‪JsonResponse
81  {
82  $this->body->write($this->‪jsonEncode($data, $encodingOptions));
83  $this->body->rewind();
84  return $this;
85  }
86 
92  private function ‪jsonEncode(array $data, int $encodingOptions): string
93  {
94  // Clear json_last_error()
95  json_encode(null);
96  $json = json_encode($data, $encodingOptions);
97  if (json_last_error() !== JSON_ERROR_NONE) {
98  throw new \InvalidArgumentException(sprintf(
99  'Unable to encode data to JSON in %s: %s',
100  __CLASS__,
101  json_last_error_msg()
102  ), 1504972434);
103  }
104  return $json;
105  }
106 }
‪TYPO3\CMS\Core\Http\JsonResponse\setPayload
‪setPayload(array $data=[], int $encodingOptions=self::DEFAULT_JSON_FLAGS)
Definition: JsonResponse.php:80
‪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:92
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:32
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:31
‪TYPO3\CMS\Core\Http\Message\$body
‪StreamInterface $body
Definition: Message.php:53
‪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\hasHeader
‪bool hasHeader(string $name)
Definition: Message.php:127
‪TYPO3\CMS\Core\Http\Message\$headers
‪array $headers
Definition: Message.php:42
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18