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