Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. interface DatabaseInterface{
  2.  
  3. /**
  4. * @return bool
  5. */
  6. function connect();
  7.  
  8. /**
  9. * @return void
  10. */
  11. function disconnect();
  12.  
  13. /**
  14. * @param string $tableName
  15. * @param array $columns
  16. * @param array $values
  17. * @return mixed
  18. */
  19. function insert($tableName, $columns, $values);
  20.  
  21. /**
  22. * @param string $tableName
  23. * @param array $conditions
  24. * @param array $columns
  25. * @param array $values
  26. * @return mixed
  27. */
  28. function update($tableName, $columns, $values, $conditions);
  29.  
  30. /**
  31. * @param string $tableName
  32. * @param string $columns
  33. * @param array $conditions
  34. * @param int $limit
  35. * @param int $offset
  36. * @return mixed
  37. */
  38. function select($tableName, $columns, $conditions, $limit, $offset);
  39.  
  40. /**
  41. * @param string $tableName
  42. * @param array $conditions
  43. * @return mixed
  44. */
  45. function delete($tableName, $conditions);
  46.  
  47. /**
  48. * @param string $tableName
  49. * @return array
  50. */
  51. function fetchFields($tableName);
  52. }
  53.  
  54. interface MapperInterface{
  55.  
  56. function findById($id);
  57.  
  58. function save();
  59.  
  60. function loadClassProperties();
  61. }
  62.  
  63. class MysqlAdapter implements DatabaseInterface
  64. {
  65. private $host = '';
  66.  
  67. private $username = '';
  68.  
  69. private $password = '';
  70.  
  71. private $dbName = '';
  72.  
  73. private $port = '';
  74.  
  75. private $socket = '';
  76.  
  77. private $_mysqli;
  78.  
  79. function __construct($host, $username, $password, $dbName, $port = null, $socket = null)
  80. {
  81. $this->host = $host;
  82. $this->username = $username;
  83. $this->password = $password;
  84. $this->dbName = $dbName;
  85. $this->port = $port;
  86. $this->socket = $socket;
  87. }
  88.  
  89. function connect()
  90. {
  91. $this->_mysqli = new mysqli($this->host, $this->username, $this->password, $this->dbName, $this->socket);
  92. if ($this->_mysqli->connect_error) {
  93. return false;
  94. }
  95. return true;
  96. }
  97.  
  98. function disconnect()
  99. {
  100. if (isset($this->_mysqli)) {
  101. $this->_mysqli->close();
  102. }
  103. }
  104.  
  105. /**
  106. * @param string $tableName
  107. * @param array $columns
  108. * @param array $values
  109. * @return mixed
  110. */
  111. function insert($tableName, $columns, $values)
  112. {
  113. $query = "INSERT INTO $tableName $columns VALUES $values";
  114. return $this->_mysqli->query($query);
  115. }
  116.  
  117. /**
  118. * @param string $tableName
  119. * @param array $conditions structure -> column => (operator, value, logical_operator) e.g id => (>, 5, AND)
  120. * @param array $columns
  121. * @param array $values
  122. * @return mixed
  123. */
  124. function update($tableName, $columns, $values, $conditions)
  125. {
  126. $updateString = $this->generateUpdateString($columns, $values);
  127. $whereString = $this->generateWhereString($conditions);
  128. $query = "UPDATE $tableName SET $updateString WHERE $whereString";
  129. $result = $this->_mysqli->query($query);
  130. return $result;
  131. }
  132.  
  133. /**
  134. * @param string $tableName
  135. * @param string $columns
  136. * @param array $conditions
  137. * @param int $limit
  138. * @param int $offset
  139. * @return mixed
  140. */
  141. function select($tableName, $columns, $conditions, $limit = null, $offset = null)
  142. {
  143. $query = "SELECT $columns FROM $tableName";
  144. if (!empty($conditions)) {
  145. $whereString = $this->generateWhereString($conditions);
  146. $query .= " WHERE $whereString";
  147. }
  148. if (isset($limit) && isset($offset)) {
  149. $query .= "LIMIT $limit OFFSET $offset";
  150. }
  151. $result = $this->_mysqli->query($query);
  152. $response = [];
  153. if($result){
  154. $response['fields'] = $this->fetchFields($result);
  155. $response['values'] = mysqli_fetch_all($result);
  156. }
  157. return $response;
  158. }
  159.  
  160. /**
  161. * @param string $tableName
  162. * @param array $conditions
  163. * @return mixed
  164. */
  165. function delete($tableName, $conditions)
  166. {
  167. $whereString = $this->generateWhereString($conditions);
  168. $query = "DELETE FROM $tableName WHERE $whereString";
  169. return $query;
  170. }
  171.  
  172. /**
  173. * @param array $keys
  174. * @param array $values
  175. * @return string
  176. */
  177. function generateUpdateString($keys, $values)
  178. {
  179. $len = count($keys);
  180. $buildString = '';
  181. for ($i = 0; $i < $len - 1; $i++) {
  182. $buildString .= $keys[$i] . '=' . $values[$i] . ',';
  183. }
  184. $buildString .= $keys[$len - 1] . '=' . $values[$len - 1];
  185. return $buildString;
  186. }
  187.  
  188. /**
  189. * @param array $arrayValues
  190. * @return string
  191. */
  192. public function generateWhereString($arrayValues)
  193. {
  194. $buildString = '';
  195. foreach ($arrayValues as $key => $arrayValue) {
  196. $buildString .= $key . $arrayValue[0] . $arrayValue[1] . " " . $arrayValue[2];
  197. }
  198. return $buildString;
  199. }
  200.  
  201. /**
  202. * @param string $queryResult
  203. * @return array
  204. */
  205. function fetchFields($queryResult)
  206. {
  207. if ($queryResult) {
  208. $fieldsData = $queryResult->fetch_fields();
  209. $fields = [];
  210. foreach ($fieldsData as $fieldData) {
  211. $fields[] = $fieldData->name;
  212. }
  213. return $fields;
  214. }
  215. return [];
  216. }
  217. }
  218.  
  219. class ORMMapper implements MapperInterface
  220. {
  221.  
  222. private $_tableName = '';
  223.  
  224. private $_adapter;
  225.  
  226. function __construct()
  227. {
  228. $this->_adapter = new MysqlAdapter('dbhost', 'username', 'password', 'test');
  229. if (!$this->_adapter->connect()) {
  230. echo "Something Terribly went wrong";
  231. return;
  232. }
  233. $this->loadClassProperties();
  234. }
  235.  
  236. /**
  237. * @return object
  238. */
  239. function findAll()
  240. {
  241. $result = $this->_adapter->select($this->_tableName, '*', []);
  242. return $this->buildResponseObject($result);
  243. }
  244.  
  245. /**
  246. * @param $id
  247. * @return object
  248. */
  249. function findById($id)
  250. {
  251. $result = $this->_adapter->select($this->_tableName, '*', ['id' => ['=', $id, '']]);
  252. $result = $this->buildResponseObject($result);
  253. if ($result){
  254. return $result[0];
  255. }
  256. return (object)[];
  257. }
  258.  
  259.  
  260. /**
  261. * @return mixed
  262. */
  263. function save()
  264. {
  265. // todo: Complete the Implementation of this method
  266. $fields = $this->_adapter->fetchFields($this->_tableName);
  267. if (isset($this->id)) {
  268. return $this->_adapter->update($this->_tableName, $fields, (array)$this, ['id' => ['=', $this->id, '']]);
  269. }
  270. return $this->_adapter->insert($this->_tableName, $fields, (array)$this);
  271. }
  272.  
  273. function loadClassProperties()
  274. {
  275. $fields = $this->_adapter->fetchFields($this->_tableName);
  276. foreach ($fields as $field) {
  277. $this->$field = null;
  278. }
  279. }
  280.  
  281. /**
  282. * @param $result
  283. * @return object
  284. */
  285. function buildResponseObject($result)
  286. {
  287. $response = [];
  288. if ($result) {
  289. $fields = $result['fields'];
  290. $values = $result['values'];
  291. $num_of_rows = count($result['values']);
  292. $num_of_fields = count($result['values'][0]);
  293.  
  294. $buildResponse = [];
  295. for ($i = 0; $i < $num_of_rows; $i++) {
  296. for ($j = 0; $j < $num_of_fields; $j++) {
  297. $buildResponse[$fields[$j]] = $values[$i][$j];
  298. }
  299. $response[] = $buildResponse;
  300. }
  301. }
  302. return json_decode(json_encode($response));
  303. }
  304.  
  305.  
  306. /**
  307. * @param $tableName
  308. */
  309. public function setTableName($tableName)
  310. {
  311. $this->_tableName = $tableName;
  312. }
  313. }
  314.  
  315. class TestModel extends ORMMapper{
  316. private $tableName = 'users';
  317. function __construct()
  318. {
  319. parent::__construct();
  320. parent::setTableName($this->tableName);
  321. }
  322. }
  323.  
  324. $users = new TestModel();
  325. $users = $users->findAll();
  326.  
  327. var_dump($users);
  328.  
  329. 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