/** * {@inheritDoc} */ public function render($model, $values = null) { $apiResponse = $model->getApiResponse(); if ($apiResponse->isError()) { $errors = $apiResponse->getErrors(); if ($e = $model->getException()) { $errors[$apiResponse->getStatus()] = $e->getMessage(); } $payload = ['errors' => $errors]; } else { $payload = $apiResponse->getContent(); } if (null === $payload) { return null; } $jsonpCallback = $model->getOption('callback'); if (null !== $jsonpCallback) { // Wrap the JSON in a JSONP callback. $this->setJsonpCallback($jsonpCallback); } $output = parent::render($payload); if (null !== $model->getOption('pretty_print')) { // Pretty print the JSON. $output = Json::prettyPrint($output); } return $output; }
/** * Inject the response with the JSON payload and appropriate Content-Type header * * @param ViewEvent $e * @return void */ public function injectResponse(ViewEvent $e) { $renderer = $e->getRenderer(); if ($renderer !== $this->renderer) { // Discovered renderer is not ours; do nothing return; } $result = $e->getResult(); if (!is_string($result)) { // We don't have a string, and thus, no JSON return; } // Populate response $response = $e->getResponse(); $response->setContent($result); $headers = $response->getHeaders(); if ($this->renderer->hasJsonpCallback()) { $contentType = 'application/javascript'; } else { $contentType = 'application/json'; } $contentType .= '; charset=' . $this->charset; $headers->addHeaderLine('content-type', $contentType); if (in_array(strtoupper($this->charset), $this->multibyteCharsets)) { $headers->addHeaderLine('content-transfer-encoding', 'BINARY'); } }
/** * Render an API Problem representation * * Also sets the $apiProblem member to the passed object. * * @param ApiProblem $apiProblem * @return string */ protected function renderApiProblem(ApiProblem $apiProblem) { $this->apiProblem = $apiProblem; if ($this->displayExceptions) { $apiProblem->setDetailIncludesStackTrace(true); } return parent::render($apiProblem->toArray()); }
public function testRendersNonTraversableNonJsonSerializableObjectsAsJsonObjectsWithJsonpCallback() { $model = new stdClass(); $model->foo = 'bar'; $model->bar = 'baz'; $expected = 'callback(' . json_encode(get_object_vars($model)) . ');'; $this->renderer->setJsonpCallback('callback'); $test = $this->renderer->render($model); $this->assertEquals($expected, $test); }
/** * @param string|\Zend\View\Model\ModelInterface $nameOrModel * @param array|null $values * @return string */ public function render($nameOrModel, $values = null) { if (!$nameOrModel instanceof ApiProblemModel) { return ''; } $apiProblem = $nameOrModel->getApiProblem(); if ($this->displayExceptions) { $apiProblem->setDetailIncludesStackTrace(true); } return parent::render($apiProblem->toArray()); }
/** * @group 2463 */ public function testRecursesJsonModelChildrenWhenRendering() { $root = new JsonModel(array('foo' => 'bar')); $child1 = new JsonModel(array('foo' => 'bar')); $child2 = new JsonModel(array('foo' => 'bar')); $child1->setCaptureTo('child1'); $child2->setCaptureTo('child2'); $root->addChild($child1)->addChild($child2); $expected = array('foo' => 'bar', 'child1' => array('foo' => 'bar'), 'child2' => array('foo' => 'bar')); $test = $this->renderer->render($root); $this->assertEquals(json_encode($expected), $test); }
/** * @param ViewEvent $e The ViewEvent instance * @return RendererInterface */ public function selectRenderer($e) { $request = $e->getRequest(); $headers = $request->getHeaders(); $model = $e->getModel(); // No Accept header? return PhpRenderer if (!$headers->has('accept')) { return $this->phpRenderer; } $accept = $headers->get('accept'); /* @var $mediaType \Zend\Http\Header\Accept\FieldValuePart\AcceptFieldValuePart */ foreach ($accept->getPrioritized() as $mediaType) { $mediaSubtype = $mediaType->getSubtype(); if ($mediaSubtype === 'json') { if (!$model instanceof JsonModel && ($children = $model->getChildrenByCaptureTo('content', false))) { $this->jsonRenderer->setMergeUnnamedChildren(true); foreach ($children as $child) { if (!$child instanceof JsonModel) { $child->setCaptureTo(null); } } } return $this->jsonRenderer; } if ($mediaSubtype === 'rss+xml' || $mediaSubtype === 'atom+xml') { $this->feedRenderer->setFeedType(substr($mediaSubtype, 0, strpos($mediaSubtype, '+'))); if (!$model instanceof FeedModel && ($children = $model->getChildrenByCaptureTo('content', false))) { foreach ($children as $child) { if (!$child instanceof FeedModel) { $child->setCaptureTo(null); } } } return $this->feedRenderer; } } // Nothing matched; return PhpRenderer. Technically, we should probably // return an HTTP 415 Unsupported response. return $this->phpRenderer; }
/** * Render a Resource JSON View Model * * If View Model is a ResourceJsonModel, determines if it represents a Collection * or Resource and if renders it accordingly * * @param string|\Zend\View\Model\ModelInterface $nameOrModel * @param null $values * @return string */ public function render($nameOrModel, $values = null) { if (!$nameOrModel instanceof ResourceJsonModel) { return parent::render($nameOrModel, $values); } if ($nameOrModel->isResource()) { $resource = $nameOrModel->getPayload(); return !is_null($this->getPlugin()) ? $this->getPlugin()->renderResource($resource) : parent::render($resource); } if ($nameOrModel->isCollection()) { $collection = $nameOrModel->getPayload(); return $this->getPlugin()->renderCollection($collection); } return parent::render($nameOrModel, $values); }
/** * Inject the response with the JSON payload and appropriate Content-Type header * * @param ViewEvent $e * @return void */ public function injectResponse(ViewEvent $e) { $renderer = $e->getRenderer(); if ($renderer !== $this->renderer) { // Discovered renderer is not ours; do nothing return; } $result = $e->getResult(); if (!is_string($result)) { // We don't have a string, and thus, no JSON return; } // Populate response $response = $e->getResponse(); $response->setContent($result); $headers = $response->getHeaders(); if ($this->renderer->hasJsonpCallback()) { $headers->addHeaderLine('content-type', 'application/javascript'); } else { $headers->addHeaderLine('content-type', 'application/json'); } }
public function render($nameOrModel, $values = null) { if (!$nameOrModel instanceof JsonModel) { return parent::render($nameOrModel, $values); } if ($nameOrModel->isEntity()) { /** @var \ZF\Hal\Entity $halEntity */ $halEntity = $nameOrModel->getPayload(); $entity = $halEntity->entity; $normalizationGroups = $this->getNormalizationGroups($halEntity); $payload = $this->getNormalizer()->normalize($entity, $normalizationGroups); return parent::render($payload); } if ($nameOrModel->isCollection()) { /** @var HalCollection $collection */ $collection = $nameOrModel->getPayload(); $payload = $this->getCollectionPayload($collection); // if ($payload instanceof ApiProblem) { // return $this->renderApiProblem($payload); // } return parent::render($payload); } return parent::render($nameOrModel, $values); }
/** * Render a view model * * If the view model is a HalJsonRenderer, determines if it represents * a Collection or Entity, and, if so, creates a custom * representation appropriate to the type. * * If not, it passes control to the parent to render. * * @param mixed $nameOrModel * @param mixed $values * @return string */ public function render($nameOrModel, $values = null) { if (!$nameOrModel instanceof HalJsonModel) { return parent::render($nameOrModel, $values); } if ($nameOrModel->isEntity()) { $helper = $this->helpers->get('Hal'); $payload = $helper->renderEntity($nameOrModel->getPayload()); return parent::render($payload); } if ($nameOrModel->isCollection()) { $helper = $this->helpers->get('Hal'); $payload = $helper->renderCollection($nameOrModel->getPayload()); if ($payload instanceof ApiProblem) { return $this->renderApiProblem($payload); } return parent::render($payload); } return parent::render($nameOrModel, $values); }