Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Logger {
- private static $location;
- public function __construct() {
- self::$location = 'some/dir/file.txt';
- }
- public static function write($msg) {
- $current = file_get_contents(self::$location);
- file_put_contents($current . '\n' . $msg, $current);
- return $this;
- }
- }
- class Driver_Controller {
- const Api = 'Pdo';
- const Author = 'Kyle';
- const Description = 'Container wrapper for Database Api';
- const Write = 1;
- const Read = 2;
- const Update = 4;
- const Delete = 8;
- protected $_sql;
- private $_api;
- public function __construct($dsn,$user,$pass) { try {
- $this->_api = parent::__construct($dsn,$user,$pass);
- $this->_api->setAttribute(PDO::ERR_MODE, PDO::ERR_EXCEPTION);
- } catch(PDOException $e) { Logger::write($e->getMessage()); } }
- protected function getInstance() {
- return $this->_api->Prepare($this->_sql);
- }
- }
- class Iezon_Controller extends Driver_Controller {
- public function __construct($dsn,$user,$pass) {
- parent::__construct($dsn,$user,$pass);
- }
- public function simple_select($table, $cols = []) {
- $this->_sql = 'SELECT ' . implode(',', $cols);
- return $this;
- }
- public function where($cols = []) {
- $cs = '';
- $i = 0;
- $len = count($cols);
- foreach($cols as $k => $v) {
- $cs .= $v . ' = ? ' . ($i == $len -1) ? $k . ' ' : '';
- }
- $this->_sql .= " WHERE $cs";
- return $this;
- }
- public function bind($vals = []) {
- return $this->getInstance()->execute($vals);
- }
- }
- /**
- * Wrapper only covers SELECT & WHERE
- **/
- $dsn = 'mysql:host=localhost;dbname=some_db';
- $usr = 'user';
- $pas = 'password';
- $ic = new Iezon_Controller($dsn,$usr,$pas);
- $ic->simple_select('table', ['*'])
- ->where(['AND' => 'user_id', 'created_by'])
- ->bind(['someId', 'someUser'])
- ->fetch();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement