/** * {@inheritdoc} */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { // Example from HAL Hypermedia spec at http://stateless.co/hal_specification.html $data = ['_links' => ['self' => ['href' => '/api-example'], "admin" => [['href' => '/admins/2', 'title' => 'Fred'], ['href' => '/admins/5', 'title' => 'Kate']]], '_embedded' => ['order' => [['_links' => ['self' => ['href' => '/orders/123'], 'customer' => ['href' => '/customers/21']], 'total' => 30.0, 'currency' => 'USD', 'status' => 'shipped'], ['_links' => ['self' => ['href' => '/orders/456'], 'customer' => ['href' => '/customers/22']], 'total' => 20.0, 'currency' => 'USD', 'status' => 'processing']]], 'currentlyProcessing' => 14, 'shippedToday' => 20]; $rendered = $this->json->encode($data); $response->getBody()->write($rendered); $response = $response->withHeader('Content-Type', 'application/hal+json'); return $response; }
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); }