Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created June 5, 2013 01:03
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/5710898 to your computer and use it in GitHub Desktop.
Save eminetto/5710898 to your computer and use it in GitHub Desktop.
<?php
namespace Api\PostProcessor;
use Zend\Mvc\MvcEvent;
/**
* Responsável por fazer o pós-processamento das requisições da APi
*
* @category Api
* @package PostProcessor
* @author Elton Minetto<eminetto@coderockr.com>
*/
class PostProcessor
{
/**
* Executado no pós-processamento, após qualquer action
* Verifica o formato requisitado (json ou xml) e gera a saída correspondente
*
* @param MvcEvent $e
* @return null|\Zend\Http\PhpEnvironment\Response
*/
public function process(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
$formatter = $routeMatch->getParam('formatter', false);
$serviceLocator = $e->getTarget()->getServiceLocator();
if ($formatter !== false) {
if ($e->getResult() instanceof \Zend\View\Model\ViewModel) {
$vars = null;
if (is_array($e->getResult()->getVariables())) {
$vars = $e->getResult()->getVariables();
}
} else {
$vars = $e->getResult();
}
switch ($formatter) {
case 'json':
$postProcessor = new Json;
break;
case 'xml':
$postProcessor = new Xml;
break;
}
$postProcessor->setResponse($e->getResponse());
$postProcessor->setVars($vars);
$postProcessor->process();
return $postProcessor->getResponse();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment