mediamonks /
php-rest-api
| 1 | <?php |
||
| 2 | |||
| 3 | namespace MediaMonks\RestApi\Response; |
||
| 4 | |||
| 5 | use Symfony\Component\HttpFoundation\JsonResponse as BaseJsonResponse; |
||
| 6 | |||
| 7 | class JsonResponse extends BaseJsonResponse |
||
| 8 | implements ExtendedResponseInterface |
||
| 9 | { |
||
| 10 | protected mixed $customContent; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Constructor. |
||
| 14 | * |
||
| 15 | * @param mixed $data The response data |
||
| 16 | 4 | * @param int $status The response status code |
|
| 17 | * @param array $headers An array of response headers |
||
| 18 | 4 | */ |
|
| 19 | public function __construct($data = null, int $status = 200, array $headers = []) |
||
| 20 | 4 | { |
|
| 21 | 2 | parent::__construct('', $status, $headers); |
|
| 22 | 2 | ||
| 23 | if (null === $data) { |
||
| 24 | 4 | $data = new \ArrayObject(); |
|
| 25 | 4 | } |
|
| 26 | |||
| 27 | $this->setCustomContent($data); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * We need this because setData() does json encoding already and |
||
| 32 | * this messes up the jsonp callback. |
||
| 33 | * It is a performance hit to let it decode/encode a second time |
||
| 34 | * |
||
| 35 | 4 | * @param mixed $content |
|
| 36 | * @return $this |
||
| 37 | 4 | */ |
|
| 38 | public function setCustomContent(mixed $content): static |
||
| 39 | 4 | { |
|
| 40 | $this->customContent = $content; |
||
| 41 | |||
| 42 | return $this; |
||
| 43 | } |
||
| 44 | |||
| 45 | 1 | public function getCustomContent(): mixed |
|
| 46 | { |
||
| 47 | 1 | return $this->customContent; |
|
| 48 | } |
||
| 49 | |||
| 50 | public function getContent(): string|false |
||
| 51 | { |
||
| 52 | return is_string($this->customContent) ? $this->customContent : json_encode($this->customContent); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function setData(mixed $data = []): static |
||
| 56 | { |
||
| 57 | $this->setCustomContent($data); |
||
| 58 | return parent::setData($data); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return mixed |
||
| 63 | */ |
||
| 64 | public function getCallback() |
||
| 65 | { |
||
| 66 | return $this->callback; |
||
| 67 | } |
||
| 68 | } |
||
| 69 |