Advertisement
Guest User

Iezon Driver Wrapper

a guest
Dec 3rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2.  
  3. class Logger {
  4.    
  5.     private static $location;
  6.    
  7.     public function __construct() {
  8.         self::$location = 'some/dir/file.txt';
  9.     }
  10.    
  11.     public static function write($msg) {
  12.         $current = file_get_contents(self::$location);
  13.         file_put_contents($current . '\n' . $msg, $current);
  14.         return $this;
  15.     }
  16.    
  17. }
  18.  
  19. class Driver_Controller {
  20.  
  21.     const Api           = 'Pdo';
  22.     const Author        = 'Kyle';
  23.     const Description   = 'Container wrapper for Database Api';
  24.    
  25.     const Write         = 1;
  26.     const Read          = 2;
  27.     const Update        = 4;
  28.     const Delete        = 8;
  29.    
  30.     protected $_sql;
  31.     private $_api;
  32.    
  33.     public function __construct($dsn,$user,$pass) { try {
  34.         $this->_api = parent::__construct($dsn,$user,$pass);
  35.         $this->_api->setAttribute(PDO::ERR_MODE, PDO::ERR_EXCEPTION);
  36.     } catch(PDOException $e) { Logger::write($e->getMessage()); } }
  37.    
  38.     protected function getInstance() {
  39.         return $this->_api->Prepare($this->_sql);
  40.     }
  41.    
  42. }
  43.  
  44. class Iezon_Controller extends Driver_Controller {
  45.    
  46.     public function __construct($dsn,$user,$pass) {
  47.         parent::__construct($dsn,$user,$pass);
  48.     }
  49.    
  50.     public function simple_select($table, $cols = []) {
  51.         $this->_sql = 'SELECT ' . implode(',', $cols);
  52.         return $this;
  53.     }
  54.    
  55.     public function where($cols = []) {
  56.         $cs = '';
  57.         $i = 0;
  58.         $len = count($cols);
  59.         foreach($cols as $k => $v) {
  60.             $cs .= $v . ' = ? ' . ($i == $len -1) ? $k . ' ' : '';
  61.         }
  62.         $this->_sql .= " WHERE $cs";
  63.         return $this;
  64.     }
  65.    
  66.     public function bind($vals = []) {
  67.         return $this->getInstance()->execute($vals);
  68.     }
  69.    
  70. }
  71.  
  72. /**
  73.  * Wrapper only covers SELECT & WHERE
  74.  **/
  75.  
  76. $dsn = 'mysql:host=localhost;dbname=some_db';
  77. $usr = 'user';
  78. $pas = 'password';
  79.  
  80. $ic = new Iezon_Controller($dsn,$usr,$pas);
  81.  
  82. $ic->simple_select('table', ['*'])
  83.     ->where(['AND' => 'user_id', 'created_by'])
  84.     ->bind(['someId', 'someUser'])
  85.     ->fetch();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement