Created
June 5, 2013 01:03
-
-
Save eminetto/5710898 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 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