Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface DatabaseInterface{
- /**
- * @return bool
- */
- function connect();
- /**
- * @return void
- */
- function disconnect();
- /**
- * @param string $tableName
- * @param array $columns
- * @param array $values
- * @return mixed
- */
- function insert($tableName, $columns, $values);
- /**
- * @param string $tableName
- * @param array $conditions
- * @param array $columns
- * @param array $values
- * @return mixed
- */
- function update($tableName, $columns, $values, $conditions);
- /**
- * @param string $tableName
- * @param string $columns
- * @param array $conditions
- * @param int $limit
- * @param int $offset
- * @return mixed
- */
- function select($tableName, $columns, $conditions, $limit, $offset);
- /**
- * @param string $tableName
- * @param array $conditions
- * @return mixed
- */
- function delete($tableName, $conditions);
- /**
- * @param string $tableName
- * @return array
- */
- function fetchFields($tableName);
- }
- interface MapperInterface{
- function findById($id);
- function save();
- function loadClassProperties();
- }
- class MysqlAdapter implements DatabaseInterface
- {
- private $host = '';
- private $username = '';
- private $password = '';
- private $dbName = '';
- private $port = '';
- private $socket = '';
- private $_mysqli;
- function __construct($host, $username, $password, $dbName, $port = null, $socket = null)
- {
- $this->host = $host;
- $this->username = $username;
- $this->password = $password;
- $this->dbName = $dbName;
- $this->port = $port;
- $this->socket = $socket;
- }
- function connect()
- {
- $this->_mysqli = new mysqli($this->host, $this->username, $this->password, $this->dbName, $this->socket);
- if ($this->_mysqli->connect_error) {
- return false;
- }
- return true;
- }
- function disconnect()
- {
- if (isset($this->_mysqli)) {
- $this->_mysqli->close();
- }
- }
- /**
- * @param string $tableName
- * @param array $columns
- * @param array $values
- * @return mixed
- */
- function insert($tableName, $columns, $values)
- {
- $query = "INSERT INTO $tableName $columns VALUES $values";
- return $this->_mysqli->query($query);
- }
- /**
- * @param string $tableName
- * @param array $conditions structure -> column => (operator, value, logical_operator) e.g id => (>, 5, AND)
- * @param array $columns
- * @param array $values
- * @return mixed
- */
- function update($tableName, $columns, $values, $conditions)
- {
- $updateString = $this->generateUpdateString($columns, $values);
- $whereString = $this->generateWhereString($conditions);
- $query = "UPDATE $tableName SET $updateString WHERE $whereString";
- $result = $this->_mysqli->query($query);
- return $result;
- }
- /**
- * @param string $tableName
- * @param string $columns
- * @param array $conditions
- * @param int $limit
- * @param int $offset
- * @return mixed
- */
- function select($tableName, $columns, $conditions, $limit = null, $offset = null)
- {
- $query = "SELECT $columns FROM $tableName";
- if (!empty($conditions)) {
- $whereString = $this->generateWhereString($conditions);
- $query .= " WHERE $whereString";
- }
- if (isset($limit) && isset($offset)) {
- $query .= "LIMIT $limit OFFSET $offset";
- }
- $result = $this->_mysqli->query($query);
- $response = [];
- if($result){
- $response['fields'] = $this->fetchFields($result);
- $response['values'] = mysqli_fetch_all($result);
- }
- return $response;
- }
- /**
- * @param string $tableName
- * @param array $conditions
- * @return mixed
- */
- function delete($tableName, $conditions)
- {
- $whereString = $this->generateWhereString($conditions);
- $query = "DELETE FROM $tableName WHERE $whereString";
- return $query;
- }
- /**
- * @param array $keys
- * @param array $values
- * @return string
- */
- function generateUpdateString($keys, $values)
- {
- $len = count($keys);
- $buildString = '';
- for ($i = 0; $i < $len - 1; $i++) {
- $buildString .= $keys[$i] . '=' . $values[$i] . ',';
- }
- $buildString .= $keys[$len - 1] . '=' . $values[$len - 1];
- return $buildString;
- }
- /**
- * @param array $arrayValues
- * @return string
- */
- public function generateWhereString($arrayValues)
- {
- $buildString = '';
- foreach ($arrayValues as $key => $arrayValue) {
- $buildString .= $key . $arrayValue[0] . $arrayValue[1] . " " . $arrayValue[2];
- }
- return $buildString;
- }
- /**
- * @param string $queryResult
- * @return array
- */
- function fetchFields($queryResult)
- {
- if ($queryResult) {
- $fieldsData = $queryResult->fetch_fields();
- $fields = [];
- foreach ($fieldsData as $fieldData) {
- $fields[] = $fieldData->name;
- }
- return $fields;
- }
- return [];
- }
- }
- class ORMMapper implements MapperInterface
- {
- private $_tableName = '';
- private $_adapter;
- function __construct()
- {
- $this->_adapter = new MysqlAdapter('dbhost', 'username', 'password', 'test');
- if (!$this->_adapter->connect()) {
- echo "Something Terribly went wrong";
- return;
- }
- $this->loadClassProperties();
- }
- /**
- * @return object
- */
- function findAll()
- {
- $result = $this->_adapter->select($this->_tableName, '*', []);
- return $this->buildResponseObject($result);
- }
- /**
- * @param $id
- * @return object
- */
- function findById($id)
- {
- $result = $this->_adapter->select($this->_tableName, '*', ['id' => ['=', $id, '']]);
- $result = $this->buildResponseObject($result);
- if ($result){
- return $result[0];
- }
- return (object)[];
- }
- /**
- * @return mixed
- */
- function save()
- {
- // todo: Complete the Implementation of this method
- $fields = $this->_adapter->fetchFields($this->_tableName);
- if (isset($this->id)) {
- return $this->_adapter->update($this->_tableName, $fields, (array)$this, ['id' => ['=', $this->id, '']]);
- }
- return $this->_adapter->insert($this->_tableName, $fields, (array)$this);
- }
- function loadClassProperties()
- {
- $fields = $this->_adapter->fetchFields($this->_tableName);
- foreach ($fields as $field) {
- $this->$field = null;
- }
- }
- /**
- * @param $result
- * @return object
- */
- function buildResponseObject($result)
- {
- $response = [];
- if ($result) {
- $fields = $result['fields'];
- $values = $result['values'];
- $num_of_rows = count($result['values']);
- $num_of_fields = count($result['values'][0]);
- $buildResponse = [];
- for ($i = 0; $i < $num_of_rows; $i++) {
- for ($j = 0; $j < $num_of_fields; $j++) {
- $buildResponse[$fields[$j]] = $values[$i][$j];
- }
- $response[] = $buildResponse;
- }
- }
- return json_decode(json_encode($response));
- }
- /**
- * @param $tableName
- */
- public function setTableName($tableName)
- {
- $this->_tableName = $tableName;
- }
- }
- class TestModel extends ORMMapper{
- private $tableName = 'users';
- function __construct()
- {
- parent::__construct();
- parent::setTableName($this->tableName);
- }
- }
- $users = new TestModel();
- $users = $users->findAll();
- var_dump($users);
- array(3) { [0]=> object(stdClass)#4 (3) { ["id"]=> string(1) "1" ["i"]=> NULL ["second"]=> NULL } [1]=> object(stdClass)#7 (3) { ["id"]=> string(1) "2" ["i"]=> NULL ["second"]=> NULL } [2]=> object(stdClass)#6 (3) { ["id"]=> string(1) "3" ["i"]=> string(1) "1" ["second"]=> string(1) "2" } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement