/** * @inheritDoc */ public function body(HTTPProblem $problem) { $data = ['status' => $problem->status()]; if ($problem->title()) { $data['title'] = $problem->title(); } if (!in_array($problem->type(), [null, 'about:blank'], true)) { $data['type'] = $problem->type(); } if ($problem->detail()) { $data['detail'] = $problem->detail(); } if ($problem->instance()) { $data['instance'] = $problem->instance(); } $data += $problem->extensions(); return $this->json->encode($data); }
public function testEncodingWithCustomEncodingOption() { $input = new stdClass(); $input->test1 = 'abcd1'; $input->test2 = 'abcd2'; $expected = <<<FORMATTED { "test1": "abcd1", "test2": "abcd2" } FORMATTED; $json = new JSON(); $json->setEncodingOptions(JSON_PRETTY_PRINT); $output = $json->encode($input); $this->assertSame($expected, $output); }
/** * @param ResponseInterface $response * @param mixed $jsonable * * @return ResponseInterface */ private function withJSON(ResponseInterface $response, $jsonable) { $jsoned = $this->json->encode($jsonable); return $this->withNewBody($response, $jsoned)->withHeader('Content-Type', 'application/json'); }