/** * Morph the API response to the appropriate format. * * @param string $format * * @return \Dingo\Api\Http\Response */ public function morph($format = 'json') { $content = $this->getOriginalContent(); if (isset(static::$transformer) && static::$transformer->transformableResponse($content)) { $content = static::$transformer->transform($content); } $formatter = static::getFormatter($format); $defaultContentType = $this->headers->get('content-type'); $this->headers->set('content-type', $formatter->getContentType()); if ($content instanceof EloquentModel) { $content = $formatter->formatEloquentModel($content); } elseif ($content instanceof EloquentCollection) { $content = $formatter->formatEloquentCollection($content); } elseif (is_array($content) || $content instanceof ArrayObject || $content instanceof ArrayableInterface) { $content = $formatter->formatArray($content); } else { $this->headers->set('content-type', $defaultContentType); } $this->content = $content; return $this; }