Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created June 5, 2013 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eminetto/5710915 to your computer and use it in GitHub Desktop.
Save eminetto/5710915 to your computer and use it in GitHub Desktop.
<?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 Xml 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, 'xml');
}
if (!$content) {
try {
$content = array();
if ($class) {
$content = $serializer->serialize($this->_vars, 'xml', SerializationContext::create()->setGroups(array($class)));
}
else {
$content = $serializer->serialize($this->_vars, 'xml');
}
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
$this->_response->setContent($content);
$headers = $this->_response->getHeaders();
$headers->addHeaderLine('Content-Type', 'application/xml');
$this->_response->setHeaders($headers);
}
}
@penyaskito
Copy link

This gist helped me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment