| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class JsonResponse extends Response |
| 26 |
{ |
| 27 |
protected mixed $data; |
| 28 |
protected ?string $callback = null; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public const DEFAULT_ENCODING_OPTIONS = 15; |
| 33 |
|
| 34 |
protected int $encodingOptions = self::DEFAULT_ENCODING_OPTIONS; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct(mixed $data = null, int $status = 200, array $headers = [], bool $json = false) |
| 40 |
{ |
| 41 |
parent::__construct('', $status, $headers); |
| 42 |
|
| 43 |
if ($json && !\is_string($data) && !is_numeric($data) && !$data instanceof \Stringable) { |
| 44 |
throw new \TypeError(\sprintf('"%s": If $json is set to true, argument $data must be a string or object implementing __toString(), "%s" given.', __METHOD__, get_debug_type($data))); |
| 45 |
} |
| 46 |
|
| 47 |
$data ??= new \ArrayObject(); |
| 48 |
|
| 49 |
$json ? $this->setJson($data) : $this->setData($data); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static function fromJsonString(string $data, int $status = 200, array $headers = []): static |
| 65 |
{ |
| 66 |
return new static($data, $status, $headers, true); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function setCallback(?string $callback): static |
| 79 |
{ |
| 80 |
if (null !== $callback) { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
$pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u'; |
| 86 |
$reserved = [ |
| 87 |
'break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while', |
| 88 |
'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super', 'const', 'export', |
| 89 |
'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false', |
| 90 |
]; |
| 91 |
$parts = explode('.', $callback); |
| 92 |
foreach ($parts as $part) { |
| 93 |
if (!preg_match($pattern, $part) || \in_array($part, $reserved, true)) { |
| 94 |
throw new \InvalidArgumentException('The callback name is not valid.'); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$this->callback = $callback; |
| 100 |
|
| 101 |
return $this->update(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function setJson(string $json): static |
| 110 |
{ |
| 111 |
$this->data = $json; |
| 112 |
|
| 113 |
return $this->update(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function setData(mixed $data = []): static |
| 124 |
{ |
| 125 |
try { |
| 126 |
$data = json_encode($data, $this->encodingOptions); |
| 127 |
} catch (\Exception $e) { |
| 128 |
if ('Exception' === $e::class && str_starts_with($e->getMessage(), 'Failed calling ')) { |
| 129 |
throw $e->getPrevious() ?: $e; |
| 130 |
} |
| 131 |
throw $e; |
| 132 |
} |
| 133 |
|
| 134 |
if (\JSON_THROW_ON_ERROR & $this->encodingOptions) { |
| 135 |
return $this->setJson($data); |
| 136 |
} |
| 137 |
|
| 138 |
if (\JSON_ERROR_NONE !== json_last_error()) { |
| 139 |
throw new \InvalidArgumentException(json_last_error_msg()); |
| 140 |
} |
| 141 |
|
| 142 |
return $this->setJson($data); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function getEncodingOptions(): int |
| 149 |
{ |
| 150 |
return $this->encodingOptions; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function setEncodingOptions(int $encodingOptions): static |
| 159 |
{ |
| 160 |
$this->encodingOptions = $encodingOptions; |
| 161 |
|
| 162 |
return $this->setData(json_decode($this->data)); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
protected function update(): static |
| 171 |
{ |
| 172 |
if (null !== $this->callback) { |
| 173 |
|
| 174 |
$this->headers->set('Content-Type', 'text/javascript'); |
| 175 |
|
| 176 |
return $this->setContent(\sprintf('/**/%s(%s);', $this->callback, $this->data)); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) { |
| 182 |
$this->headers->set('Content-Type', 'application/json'); |
| 183 |
} |
| 184 |
|
| 185 |
return $this->setContent($this->data); |
| 186 |
} |
| 187 |
} |