Created
June 5, 2013 01:04
-
-
Save eminetto/5710908 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Api\PostProcessor; | |
use JMS\Serializer\SerializerBuilder; | |
use JMS\Serializer\SerializationContext; | |
/** | |
* Classe concreta que retorna JSON | |
* | |
* @category Api | |
* @package PostProcessor | |
* @author Elton Minetto<eminetto@coderockr.com> | |
*/ | |
class Json extends AbstractPostProcessor | |
{ | |
/** | |
* Retorna os cabeçalhos e conteúdo no formato JSON | |
*/ | |
public function process($class = null) | |
{ | |
$serializer = SerializerBuilder::create()->build(); | |
$content = null; | |
if (isset($this->_vars['error-message'])) { | |
$content = $serializer->serialize($this->_vars, 'json'); | |
} | |
if (!$content) { | |
try { | |
$content = array(); | |
if ($class) { | |
$content = $serializer->serialize($this->_vars, 'json', SerializationContext::create()->setGroups(array($class))); | |
} | |
else { | |
$content = $serializer->serialize($this->_vars, 'json'); | |
} | |
} catch (\Exception $e) { | |
throw new \Exception($e->getMessage()); | |
} | |
} | |
$this->_response->setContent($content); | |
$headers = $this->_response->getHeaders(); | |
$headers->addHeaderLine('Content-Type', 'application/json'); | |
$this->_response->setHeaders($headers); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment