Advertisement
Omnikron13

Draft diskaro Request system

Aug 19th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.02 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Request;
  4.  
  5. require_once('Track.php');
  6.  
  7. abstract class Request {
  8.     protected $class = null;
  9.     protected $response = null;
  10.  
  11.     //
  12.     protected function __construct($data) {
  13.         $this->class = $data['class'];
  14.     }
  15.  
  16.     public function getResponse() {
  17.         if($this->response === null)
  18.             $this->process();
  19.         return $this->response;
  20.     }
  21.  
  22.     protected abstract function process();
  23.  
  24.     //
  25.     public function log($f = 'foo') {
  26.         $s = print_r($this, true);
  27.         //file_put_contents($f, $s);
  28.         echo "<pre>$s</pre>";
  29.     }
  30.  
  31.     //
  32.     public static function parse($data) {
  33.         switch($data['type']) {
  34.             case 'get':
  35.                 return new Get($data);
  36.             case 'update':
  37.                 return new Update($data);
  38.             case 'add':
  39.                 return new Add($data);
  40.             case 'delete':
  41.                 return new Delete($data);
  42.             default:
  43.                 throw new Exception('Unknown/Unsupported request type: '.$data['type']);
  44.         }
  45.     }
  46. }
  47.  
  48. //
  49. class Get extends Request {
  50.     protected $constraint = null;
  51.  
  52.     //
  53.     public function __construct($data) {
  54.         parent::__construct($data);
  55.         $this->constraint = $data['constraint'];
  56.     }
  57.  
  58.     //
  59.     protected function process() {
  60.         $this->response = \DataCore::jsonRequest($this->class, $this->constraint);
  61.         return $this;
  62.     }
  63. }
  64.  
  65. //
  66. class Update extends Request {
  67.     protected $json = null;
  68.  
  69.     //
  70.     public function __construct($data) {
  71.         parent::__construct($data);
  72.         $this->json = $data['data'];
  73.     }
  74.  
  75.     protected function process() {
  76.         $data = json_decode($this->json);
  77.         $target = new $this->class($data->id);
  78.         $target->update($data);
  79.         $this->response = 'Update applied';
  80.         return $this;
  81.     }
  82. }
  83.  
  84. //
  85. class Add extends Request {
  86.     protected $json = null;
  87.     protected $obj  = null;
  88.  
  89.     //
  90.     public function __construct($data) {
  91.         parent::__construct($data);
  92.         $this->json = $data['data'];
  93.     }
  94.  
  95.     protected function process() {
  96.         $class = $this->class;
  97.         try {
  98.             $this->obj = $class::addJSON($this->json);
  99.             $this->response = json_encode($this->obj);
  100.         }
  101.         catch(\PDOException $e) {
  102.             $this->response = json_encode([
  103.                 'success' => false,
  104.                 'error'   => [
  105.                     'code'    => $e->errorInfo[0],
  106.                     'message' => $e->errorInfo[2],
  107.                 ],
  108.             ]);
  109.         }
  110.         return $this;
  111.     }
  112. }
  113.  
  114. //
  115. class Delete extends Request {
  116.     //
  117.     protected $id = null;
  118.  
  119.     //
  120.     public function __construct($data) {
  121.         parent::__construct($data);
  122.         $this->id = $data['id'];
  123.     }
  124.  
  125.     //
  126.     protected function process() {
  127.         $class = $this->class;
  128.         $class::remove($this->id);
  129.         $this->response = 'Record removed from DB';
  130.         return $this;
  131.     }
  132. }
  133.  
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement