Guest User

Untitled

a guest
Apr 17th, 2010
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package nxcMootools
  4.  * @class   nxcMootoolsAJAXResponse
  5.  * @author  Serhey Dolgushev <[email protected]>
  6.  * @date    05 Mar 2010
  7.  *
  8.  **/
  9.  
  10. class nxcMootoolsAJAXResponse {
  11.  
  12.     const STATUS_ERROR   = 0;
  13.     const STATUS_SUCCESS = 1;
  14.  
  15.     private $status;
  16.     private $message;
  17.     private $errors;
  18.     private $data;
  19.  
  20.     public function __construct() {
  21.         $this->status  = self::STATUS_ERROR;
  22.         $this->message = null;
  23.         $this->errors  = array();
  24.         $this->data    = array();
  25.     }
  26.  
  27.     public function setStatus( $status ) {
  28.         $this->status = $status;
  29.     }
  30.  
  31.     public function getStatus() {
  32.         return $this->status;
  33.     }
  34.  
  35.     public function setMessage( $message ) {
  36.         $this->message = $message;
  37.     }
  38.  
  39.     public function getMessage() {
  40.         return $this->message;
  41.     }
  42.  
  43.     public function addError( $error, $key = null ) {
  44.         if( $key === null ) {
  45.             $key = 'error_' . count( $this->errors );
  46.         }
  47.  
  48.         $this->errors[ $key ] = $error;
  49.     }
  50.  
  51.     public function getErrors() {
  52.         return $this->errors;
  53.     }
  54.  
  55.     public function __toString() {
  56.         return json_encode(
  57.             array(
  58.                 'status'  => $this->status,
  59.                 'message' => $this->message,
  60.                 'errors'  => $this->errors,
  61.                 'data'    => $this->data
  62.             )
  63.         );
  64.     }
  65.  
  66.     public function __set( $name, $value ) {
  67.         $this->data[ $name ] = $value;
  68.     }
  69.  
  70.     public function __get( $name ) {
  71.         return ( isset( $this->data[ $name ] ) ) ? $this->data[ $name ] : null;
  72.     }
  73.  
  74.     public function output() {
  75.         header( 'Content-Type: application/json' );
  76.         echo $this;
  77.         eZExecution::cleanExit();
  78.     }
  79. }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment