Skip to content

Instantly share code, notes, and snippets.

@kemo
Created June 11, 2011 13:01
Show Gist options
  • Save kemo/1020533 to your computer and use it in GitHub Desktop.
Save kemo/1020533 to your computer and use it in GitHub Desktop.
JSend View Model
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Strict JSend response formatting
*
* @author kemo <github.com/kemo>
* @see http://labs.omniti.com/labs/jsend
*/
class View_Ajax {
// Release version
const VERSION = '1.0.1';
/**
* @var Array of return data
*/
protected $_data = array(
// Must-set
'data' => array(),
'status' => 'success', // success, error
// Optional, on error
'code' => NULL,
'message' => NULL,
);
/**
* @param array initial array of data
*/
public function __construct(array $data = NULL)
{
if (NULL !== $data)
{
$this->set($data);
}
}
/**
* Magic getter method
*/
public function __get($key)
{
if (isset($this->_data[$key]))
{
return $this->_data[$key];
}
elseif (isset($this->_data['data'][$key]))
{
return $this->_data['data'][$key];
}
throw new Kohana_Exception('Nonexisting key requested: :key', array(
':key' => $key
));
}
/**
* Magic setter method
*/
public function __set($key, $value)
{
return $this->set($key, $value);
}
/**
* What happens when casted to string?
*/
public function __toString()
{
return $this->render();
}
/**
* Binds a param by reference
*
* @param string key
* @param mixed var
* @return object $this
*/
public function bind($key, & $value)
{
if (isset($this->_data[$key]))
{
$this->validate($key, $value);
$this->_data[$key] =& $value;
}
else
{
$this->_data['data'][$key] =& $value;
}
return $this;
}
/**
* @param int $code
* @return mixed $code on get / $this on set
*/
public function code($code = NULL)
{
if (NULL === $code)
return $this->_data['code'];
$this->_data['code'] = $code;
return $this;
}
/**
* @param string $message
* @return mixed $message on get // $this on set
*/
public function message($message = NULL)
{
if (NULL === $message)
return $this->_data['message'];
$this->_data['message'] = $message;
return $this;
}
/**
* Renders the current dataset
* @return string JSON encoded data
*/
public function render()
{
foreach ($this->_data as $key => $value)
{
// Unset empty params
if (NULL === $this->_data[$key])
{
unset($this->_data[$key]);
}
}
return json_encode($this->_data);
}
/**
* Sets a key => value pair or an array of values
*
* @chainable
* @param mixed string key or array of key => value pairs
* @param mixed value
* @return object $this
*/
public function set($key, $value = NULL)
{
if (is_array($key))
{
foreach ($key as $name => $value)
{
$this->set($name, $value);
}
return $this;
}
elseif (isset($this->_data[$key]))
{
$this->validate($key, $value);
$this->_data[$key] = $value;
}
else
{
$this->_data['data'][$key] = $value;
}
return $this;
}
/**
* @param string $status
* @return mixed $status on get // $this on set
*/
public function status($status = NULL)
{
if (NULL === $status)
return $this->_data['status'];
if ( ! in_array($status, array('error','success')))
throw new Kohana_Exception('Status must be error or success');
$this->_data['status'] = $status;
return $this;
}
/**
* Validates the data type of var being set
*
* @param string key being set
* @param mixed value being set
* @return bool TRUE if passed, exception thrown otherwise
* @throws Kohana_Exception
*/
public function validate($key, $value)
{
// Validate data types
switch ($key)
{
case 'data' :
if ( ! is_array($value))
throw new Kohana_Exception('`data` param must be an array');
break;
}
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment