Skip to content

Instantly share code, notes, and snippets.

@RWOverdijk
Forked from basz/JsonModel
Created May 22, 2012 19:24
Show Gist options
  • Save RWOverdijk/2771090 to your computer and use it in GitHub Desktop.
Save RWOverdijk/2771090 to your computer and use it in GitHub Desktop.
Inside my controller, as long as result is Traversable or is an object with public props it works...
public function getTransactionDataAction()
{
try {
if ($this->transaction == false || $this->transaction->getStatus() != Transaction::STATUS_COMPLETED) {
return $this->envelopeResult(false, sprintf(self::ERR_INCORRECT_TRANSACTION, $this->_transaction));
}
return $this->envelopeResult(true, $this->transaction->toArray());
} catch (\Exception $e) {
return $this->envelopeResult(false, $e->getMessage());
}
}
/**
* Envelopes the result in an succes or error json
*
* @param type $success
* @param type $result
* @return \Zend\View\Model\JsonModel
*/
protected function envelopeResult($success, $result)
{
if ($success) {
$result = new JsonModel(new \ArrayObject(array('result' => $result)));
} else {
$result = new JsonModel(new \ArrayObject(array('error' => $result)));
}
$result->setTerminal(true);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment