Guest User

Untitled

a guest
Dec 22nd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.55 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Database abstract class
  5. */
  6.  
  7. require_once('DatabaseInterface.php');
  8.  
  9. abstract class Database implements DatabaseInterface
  10. {
  11. /**
  12. * @var string
  13. */
  14. protected $charset;
  15.  
  16. /**
  17. * @var string
  18. */
  19. protected $databaseName;
  20.  
  21. /**
  22. * @var array[] [microtime(true), code, message]
  23. */
  24. protected $errors;
  25.  
  26. /**
  27. * @var string
  28. */
  29. protected $host;
  30.  
  31. /**
  32. * @var string
  33. */
  34. protected $password;
  35.  
  36. /**
  37. * @var PDO
  38. */
  39. protected $pdo;
  40.  
  41. /**
  42. * @var PDOStatement
  43. */
  44. protected $pdoStatement;
  45.  
  46. /**
  47. * @var int
  48. */
  49. protected $port;
  50.  
  51. /**
  52. * @var string
  53. */
  54. protected $username;
  55.  
  56. /**
  57. * @param array $args property => value
  58. * @throws InvalidArgumentException
  59. */
  60. public function __construct(array $args)
  61. {
  62. if (!is_array($args)) throw new InvalidArgumentException('!is_array($args)');
  63.  
  64. foreach ($args as $key => $value)
  65. {
  66. if (!is_string($key)) throw new InvalidArgumentException('!is_string($key)');
  67. if (property_exists(get_class($this), $key) && method_exists($this, $key)) $this->$key($value);
  68. }
  69. }
  70.  
  71. public function __destruct()
  72. {
  73. if ($this->connected()) $this->disconnect();
  74. }
  75.  
  76. /**
  77. * @param string $code
  78. * @param string $message
  79. * @return void
  80. * @throws InvalidArgumentException
  81. */
  82. protected function addError(string $code, string $message): void
  83. {
  84. if (!is_string($code)) throw new InvalidArgumentException('!is_string($code)');
  85. if (!is_string($message)) throw new InvalidArgumentException('!is_string($message)');
  86. $this->errors[] = [microtime(true), $code, $message];
  87. }
  88.  
  89. /**
  90. * @return bool
  91. */
  92. public function beginTransaction(): bool
  93. {
  94. return $this->pdo->beginTransaction();
  95. }
  96.  
  97. /**
  98. * @param int|string $parameter
  99. * @param mixed $value
  100. * @param int $dataType
  101. * @return bool
  102. * @throws InvalidArgumentException
  103. */
  104. public function bindValue($parameter, $value, int $dataType = null): bool
  105. {
  106. if (!is_int($parameter) && !is_string($parameter)) throw new InvalidArgumentException('!is_int($parameter) && !is_string($parameter)');
  107. if (!is_null($dataType) && !is_int($dataType)) throw new InvalidArgumentException('!is_null($dataType) && !is_int($dataType)');
  108.  
  109. if (is_null($dataType))
  110. {
  111. switch (true)
  112. {
  113. case is_bool($value):
  114. $dataType = PDO::PARAM_BOOL;
  115. break;
  116.  
  117. case is_int($value):
  118. $dataType = PDO::PARAM_INT;
  119. break;
  120.  
  121. case is_null($value):
  122. $dataType = PDO::PARAM_NULL;
  123. break;
  124.  
  125. default:
  126. $dataType = PDO::PARAM_STR;
  127. }
  128. }
  129.  
  130. return $this->pdoStatement->bindValue($parameter, $value, $dataType);
  131. }
  132.  
  133. /**
  134. * @param array $data parameter => value
  135. * @return bool
  136. * @throws InvalidArgumentException
  137. */
  138. public function bindValues(array $data): bool
  139. {
  140. if (!is_array($data)) throw new InvalidArgumentException('!is_array($data)');
  141.  
  142. foreach ($data as $parameter => $value)
  143. {
  144. if (!is_int($parameter) && !is_string($parameter)) throw new InvalidArgumentException('!is_int($parameter) && !is_string($parameter)');
  145. if (!$this->bindValue($parameter, $value)) return false;
  146. }
  147.  
  148. return true;
  149. }
  150.  
  151. /**
  152. * @param string $charset Optional - used for SETting, omitted when GETting.
  153. * @return bool|string
  154. * @throws InvalidArgumentException
  155. */
  156. public function charset(string $charset = null)
  157. {
  158. if (!is_null($charset) && !is_string($charset)) throw new InvalidArgumentException('!is_null($charset) && !is_string($charset)');
  159. return is_null($charset) ? $this->getCharset() : $this->setCharset($charset);
  160. }
  161.  
  162. /**
  163. * @return bool
  164. */
  165. public function commit(): bool
  166. {
  167. return $this->pdo->commit();
  168. }
  169.  
  170. /**
  171. * @return bool
  172. */
  173. public abstract function connect(): bool;
  174.  
  175. /**
  176. * @return bool
  177. */
  178. public function connected(): bool
  179. {
  180. return $this->pdo instanceof PDO;
  181. }
  182.  
  183. /**
  184. * @param string $databaseName Optional - used for SETting, omitted when GETting.
  185. * @return bool|string
  186. * @throws InvalidArgumentException
  187. */
  188. public function databaseName(string $databaseName = null)
  189. {
  190. if (!is_null($databaseName) && !is_string($databaseName)) throw new InvalidArgumentException('!is_null($databaseName) && !is_string($databaseName)');
  191. return is_null($databaseName) ? $this->getDatabaseName() : $this->setDatabaseName($databaseName);
  192. }
  193.  
  194. /**
  195. * @return void
  196. */
  197. public function debugDumpParams(): void
  198. {
  199. return $this->pdoStatement->debugDumpParams();
  200. }
  201.  
  202. /**
  203. * @return bool
  204. */
  205. public function disconnect(): bool
  206. {
  207. if ($this->connected())
  208. {
  209. $this->pdoStatement = null;
  210. $this->pdo = null;
  211.  
  212. return true;
  213. }
  214.  
  215. return false;
  216. }
  217.  
  218. /**
  219. * @return array[] [microtime(true), code, message]
  220. */
  221. public function errors(): array
  222. {
  223. return $this->getErrors();
  224. }
  225.  
  226. /**
  227. * @return bool
  228. */
  229. public function execute(): bool
  230. {
  231. return $this->pdoStatement->execute();
  232. }
  233.  
  234. /**
  235. * @param string $returnType Default is 'array'.
  236. * @param bool $all Default is FALSE.
  237. * @return mixed
  238. * @throws InvalidArgumentException
  239. */
  240. public function fetch(string $returnType = 'array', bool $all = false)
  241. {
  242. if (!is_string($returnType)) throw new InvalidArgumentException('!is_string($returnType)');
  243. if (!is_bool($all)) throw new InvalidArgumentException('!is_bool($all)');
  244.  
  245. $this->execute();
  246.  
  247. switch ($returnType)
  248. {
  249. case 'json':
  250. return $all ?
  251. json_encode($this->pdoStatement->fetchAll(PDO::FETCH_ASSOC)) :
  252. json_encode($this->pdoStatement->fetch(PDO::FETCH_ASSOC));
  253.  
  254. case 'object':
  255. return $all ?
  256. $this->pdoStatement->fetchAll(PDO::FETCH_OBJ) :
  257. $this->pdoStatement->fetch(PDO::FETCH_OBJ);
  258.  
  259. default:
  260. return $all ?
  261. $this->pdoStatement->fetchAll(PDO::FETCH_ASSOC) :
  262. $this->pdoStatement->fetch(PDO::FETCH_ASSOC);
  263. }
  264. }
  265.  
  266. /**
  267. * @param string $returnType Default is 'array'.
  268. * @return mixed
  269. * @throws InvalidArgumentException
  270. */
  271. public function fetchAll(string $returnType = 'array'): array
  272. {
  273. if (!is_string($returnType)) throw new InvalidArgumentException('!is_string($returnType)');
  274.  
  275. $this->execute();
  276. $this->fetch($returnType, true);
  277. }
  278.  
  279. /**
  280. * @return string
  281. */
  282. protected function getCharset(): string
  283. {
  284. $copy = $this->charset;
  285. return $copy;
  286. }
  287.  
  288. /**
  289. * @param int $attribute
  290. * @return mixed
  291. * @throws InvalidArgumentException
  292. */
  293. public function getConnectionAttribute(int $attribute)
  294. {
  295. if (!is_int($attribute)) throw new InvalidArgumentException('!is_int($attribute)');
  296. return $this->pdo->getAttribute($attribute);
  297. }
  298.  
  299. /**
  300. * @return string
  301. */
  302. protected function getDatabaseName(): string
  303. {
  304. $copy = $this->databaseName;
  305. return $copy;
  306. }
  307.  
  308. /**
  309. * @return array[] [microtime(true), code, message]
  310. */
  311. protected function getErrors(): array
  312. {
  313. $copy = $this->errors;
  314. return $copy;
  315. }
  316.  
  317. /**
  318. * @return string
  319. */
  320. protected function getHost(): string
  321. {
  322. $copy = $this->host;
  323. return $copy;
  324. }
  325.  
  326. /**
  327. * @return int
  328. */
  329. protected function getPort(): int
  330. {
  331. $copy = $this->port;
  332. return $copy;
  333. }
  334.  
  335. /**
  336. * @param int $attribute
  337. * @return mixed
  338. * @throws InvalidArgumentException
  339. */
  340. public function getStatementAttribute(int $attribute)
  341. {
  342. if (!is_int($attribute)) throw new InvalidArgumentException('!is_int($attribute)');
  343. return $this->pdoStatement->getAttribute($attribute);
  344. }
  345.  
  346. /**
  347. * @return string
  348. */
  349. protected function getUsername(): string
  350. {
  351. $copy = $this->username;
  352. return $copy;
  353. }
  354.  
  355. /**
  356. * @param string $host Optional - used for SETting, omitted when GETting.
  357. * @return bool|string
  358. * @throws InvalidArgumentException
  359. */
  360. public function host(string $host = null)
  361. {
  362. if (!is_null($host) && !is_string($host)) throw new InvalidArgumentException('!is_null($host) && !is_string($host)');
  363. return is_null($host) ? $this->getHost() : $this->setHost($host);
  364. }
  365.  
  366. /**
  367. * @return string
  368. */
  369. public function lastInsertId(): string
  370. {
  371. return $this->pdo->lastInsertId();
  372. }
  373.  
  374. /**
  375. * @param int|string $port Optional - used for SETting, omitted when GETting.
  376. * @return bool|int
  377. * @throws InvalidArgumentException
  378. */
  379. public function port($port = null)
  380. {
  381. if ((!is_int($port) && !is_string($port)) || (is_string($port) && !ctype_digit($port))) throw new InvalidArgumentException('(!is_int($port) && !is_string($port)) || (is_string($port) && !ctype_digit($port))');
  382. return is_null($port) ? $this->getPort() : $this->setPort($port);
  383. }
  384.  
  385. /**
  386. * @param string $query
  387. * @param array $options
  388. * @return bool
  389. * @throws InvalidArgumentException
  390. */
  391. public function prepare(string $query, array $options = []): bool
  392. {
  393. if (!is_string($query)) throw new InvalidArgumentException('!is_string($query)');
  394. if (!is_array($options)) throw new InvalidArgumentException('!is_array($options)');
  395.  
  396. try
  397. {
  398. $this->pdoStatement = $this->pdo->prepare($query, $options);
  399. return true;
  400. }
  401. catch (PDOException $exception)
  402. {
  403. $this->addError($exception->getCode(), $exception->getMessage());
  404. return false;
  405. }
  406. }
  407.  
  408. /**
  409. * @return bool
  410. */
  411. public function rollBack(): bool
  412. {
  413. return $this->pdo->rollBack();
  414. }
  415.  
  416. /**
  417. * @return int
  418. */
  419. public function rowCount(): int
  420. {
  421. return $this->pdoStatement->rowCount();
  422. }
  423.  
  424. /**
  425. * @param string $charset
  426. * @return bool
  427. * @throws InvalidArgumentException
  428. */
  429. protected function setCharset(string $charset): bool
  430. {
  431. if (!is_string($charset)) throw new InvalidArgumentException('!is_string($charset)');
  432. $this->charset = $charset;
  433. return true;
  434. }
  435.  
  436. /**
  437. * @param int $attribute
  438. * @param mixed $value
  439. * @return bool
  440. * @throws InvalidArgumentException
  441. */
  442. public function setConnectionAttribute(int $attribute, $value): bool
  443. {
  444. if (!is_int($attribute)) throw new InvalidArgumentException('!is_int($attribute)');
  445. return $this->pdo->setAttribute($attribute, $value);
  446. }
  447.  
  448. /**
  449. * @param string $databaseName
  450. * @return bool
  451. * @throws InvalidArgumentException
  452. */
  453. protected function setDatabaseName(string $databaseName): bool
  454. {
  455. if (!is_string($databaseName)) throw new InvalidArgumentException('!is_string($databaseName)');
  456. $this->databaseName = $databaseName;
  457. return true;
  458. }
  459.  
  460. /**
  461. * @param string $host
  462. * @return bool
  463. * @throws InvalidArgumentException
  464. */
  465. protected function setHost(string $host): bool
  466. {
  467. if (!is_string($host)) throw new InvalidArgumentException('!is_string($host)');
  468. $this->host = $host;
  469. return true;
  470. }
  471.  
  472. /**
  473. * @param string $password
  474. * @return bool
  475. * @throws InvalidArgumentException
  476. */
  477. protected function setPassword(string $password): bool
  478. {
  479. if (!is_string($password)) throw new InvalidArgumentException('!is_string($password)');
  480. $this->password = $password;
  481. return true;
  482. }
  483.  
  484. /**
  485. * @param int|string $port
  486. * @return bool
  487. * @throws InvalidArgumentException
  488. */
  489. protected function setPort($port): bool
  490. {
  491. if ((!is_int($port) && !is_string($port)) || (is_string($port) && !ctype_digit($port))) throw new InvalidArgumentException('(!is_int($port) && !is_string($port)) || (is_string($port) && !ctype_digit($port))');
  492. $this->port = intval($port);
  493. return true;
  494. }
  495.  
  496. /**
  497. * @param int $attribute
  498. * @param mixed $value
  499. * @return bool
  500. * @throws InvalidArgumentException
  501. */
  502. public function setStatementAttribute(int $attribute, $value): bool
  503. {
  504. if (!is_int($attribute)) throw new InvalidArgumentException('!is_int($attribute)');
  505. return $this->pdoStatement->setAttribute($attribute, $value);
  506. }
  507.  
  508. /**
  509. * @param string $username
  510. * @return bool
  511. * @throws InvalidArgumentException
  512. */
  513. protected function setUsername(string $username): bool
  514. {
  515. if (!is_string($username)) throw new InvalidArgumentException('!is_string($username)');
  516. $this->username = $username;
  517. return true;
  518. }
  519.  
  520. /**
  521. * @param string $username Optional - used for SETting, omitted when GETting.
  522. * @return bool|string
  523. */
  524. public function username(string $username = null)
  525. {
  526. if (!is_null($username) && !is_string($username)) throw new InvalidArgumentException('!is_null($username) && !is_string($username)');
  527. return is_null($username) ? $this->getUsername() : $this->setUsername($username);
  528. }
  529. }
  530.  
  531. ?>
Add Comment
Please, Sign In to add comment