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