Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Database abstract class
- */
- require_once('DatabaseInterface.php');
- abstract class Database implements DatabaseInterface
- {
- /**
- * @var string
- */
- protected $charset;
- /**
- * @var string
- */
- protected $databaseName;
- /**
- * @var array[] [microtime(true), code, message]
- */
- protected $errors;
- /**
- * @var string
- */
- protected $host;
- /**
- * @var string
- */
- protected $password;
- /**
- * @var PDO
- */
- protected $pdo;
- /**
- * @var PDOStatement
- */
- protected $pdoStatement;
- /**
- * @var int
- */
- protected $port;
- /**
- * @var string
- */
- protected $username;
- /**
- * @param array $args property => value
- * @throws InvalidArgumentException
- */
- public function __construct(array $args)
- {
- if (!is_array($args)) throw new InvalidArgumentException('!is_array($args)');
- foreach ($args as $key => $value)
- {
- if (!is_string($key)) throw new InvalidArgumentException('!is_string($key)');
- if (property_exists(get_class($this), $key) && method_exists($this, $key)) $this->$key($value);
- }
- }
- public function __destruct()
- {
- if ($this->connected()) $this->disconnect();
- }
- /**
- * @param string $code
- * @param string $message
- * @return void
- * @throws InvalidArgumentException
- */
- protected function addError(string $code, string $message): void
- {
- if (!is_string($code)) throw new InvalidArgumentException('!is_string($code)');
- if (!is_string($message)) throw new InvalidArgumentException('!is_string($message)');
- $this->errors[] = [microtime(true), $code, $message];
- }
- /**
- * @return bool
- */
- public function beginTransaction(): bool
- {
- return $this->pdo->beginTransaction();
- }
- /**
- * @param int|string $parameter
- * @param mixed $value
- * @param int $dataType
- * @return bool
- * @throws InvalidArgumentException
- */
- public function bindValue($parameter, $value, int $dataType = null): bool
- {
- if (!is_int($parameter) && !is_string($parameter)) throw new InvalidArgumentException('!is_int($parameter) && !is_string($parameter)');
- if (!is_null($dataType) && !is_int($dataType)) throw new InvalidArgumentException('!is_null($dataType) && !is_int($dataType)');
- if (is_null($dataType))
- {
- switch (true)
- {
- case is_bool($value):
- $dataType = PDO::PARAM_BOOL;
- break;
- case is_int($value):
- $dataType = PDO::PARAM_INT;
- break;
- case is_null($value):
- $dataType = PDO::PARAM_NULL;
- break;
- default:
- $dataType = PDO::PARAM_STR;
- }
- }
- return $this->pdoStatement->bindValue($parameter, $value, $dataType);
- }
- /**
- * @param array $data parameter => value
- * @return bool
- * @throws InvalidArgumentException
- */
- public function bindValues(array $data): bool
- {
- if (!is_array($data)) throw new InvalidArgumentException('!is_array($data)');
- foreach ($data as $parameter => $value)
- {
- if (!is_int($parameter) && !is_string($parameter)) throw new InvalidArgumentException('!is_int($parameter) && !is_string($parameter)');
- if (!$this->bindValue($parameter, $value)) return false;
- }
- return true;
- }
- /**
- * @param string $charset Optional - used for SETting, omitted when GETting.
- * @return bool|string
- * @throws InvalidArgumentException
- */
- public function charset(string $charset = null)
- {
- if (!is_null($charset) && !is_string($charset)) throw new InvalidArgumentException('!is_null($charset) && !is_string($charset)');
- return is_null($charset) ? $this->getCharset() : $this->setCharset($charset);
- }
- /**
- * @return bool
- */
- public function commit(): bool
- {
- return $this->pdo->commit();
- }
- /**
- * @return bool
- */
- public abstract function connect(): bool;
- /**
- * @return bool
- */
- public function connected(): bool
- {
- return $this->pdo instanceof PDO;
- }
- /**
- * @param string $databaseName Optional - used for SETting, omitted when GETting.
- * @return bool|string
- * @throws InvalidArgumentException
- */
- public function databaseName(string $databaseName = null)
- {
- if (!is_null($databaseName) && !is_string($databaseName)) throw new InvalidArgumentException('!is_null($databaseName) && !is_string($databaseName)');
- return is_null($databaseName) ? $this->getDatabaseName() : $this->setDatabaseName($databaseName);
- }
- /**
- * @return void
- */
- public function debugDumpParams(): void
- {
- return $this->pdoStatement->debugDumpParams();
- }
- /**
- * @return bool
- */
- public function disconnect(): bool
- {
- if ($this->connected())
- {
- $this->pdoStatement = null;
- $this->pdo = null;
- return true;
- }
- return false;
- }
- /**
- * @return array[] [microtime(true), code, message]
- */
- public function errors(): array
- {
- return $this->getErrors();
- }
- /**
- * @return bool
- */
- public function execute(): bool
- {
- return $this->pdoStatement->execute();
- }
- /**
- * @param string $returnType Default is 'array'.
- * @param bool $all Default is FALSE.
- * @return mixed
- * @throws InvalidArgumentException
- */
- public function fetch(string $returnType = 'array', bool $all = false)
- {
- if (!is_string($returnType)) throw new InvalidArgumentException('!is_string($returnType)');
- if (!is_bool($all)) throw new InvalidArgumentException('!is_bool($all)');
- $this->execute();
- switch ($returnType)
- {
- case 'json':
- return $all ?
- json_encode($this->pdoStatement->fetchAll(PDO::FETCH_ASSOC)) :
- json_encode($this->pdoStatement->fetch(PDO::FETCH_ASSOC));
- case 'object':
- return $all ?
- $this->pdoStatement->fetchAll(PDO::FETCH_OBJ) :
- $this->pdoStatement->fetch(PDO::FETCH_OBJ);
- default:
- return $all ?
- $this->pdoStatement->fetchAll(PDO::FETCH_ASSOC) :
- $this->pdoStatement->fetch(PDO::FETCH_ASSOC);
- }
- }
- /**
- * @param string $returnType Default is 'array'.
- * @return mixed
- * @throws InvalidArgumentException
- */
- public function fetchAll(string $returnType = 'array'): array
- {
- if (!is_string($returnType)) throw new InvalidArgumentException('!is_string($returnType)');
- $this->execute();
- $this->fetch($returnType, true);
- }
- /**
- * @return string
- */
- protected function getCharset(): string
- {
- $copy = $this->charset;
- return $copy;
- }
- /**
- * @param int $attribute
- * @return mixed
- * @throws InvalidArgumentException
- */
- public function getConnectionAttribute(int $attribute)
- {
- if (!is_int($attribute)) throw new InvalidArgumentException('!is_int($attribute)');
- return $this->pdo->getAttribute($attribute);
- }
- /**
- * @return string
- */
- protected function getDatabaseName(): string
- {
- $copy = $this->databaseName;
- return $copy;
- }
- /**
- * @return array[] [microtime(true), code, message]
- */
- protected function getErrors(): array
- {
- $copy = $this->errors;
- return $copy;
- }
- /**
- * @return string
- */
- protected function getHost(): string
- {
- $copy = $this->host;
- return $copy;
- }
- /**
- * @return int
- */
- protected function getPort(): int
- {
- $copy = $this->port;
- return $copy;
- }
- /**
- * @param int $attribute
- * @return mixed
- * @throws InvalidArgumentException
- */
- public function getStatementAttribute(int $attribute)
- {
- if (!is_int($attribute)) throw new InvalidArgumentException('!is_int($attribute)');
- return $this->pdoStatement->getAttribute($attribute);
- }
- /**
- * @return string
- */
- protected function getUsername(): string
- {
- $copy = $this->username;
- return $copy;
- }
- /**
- * @param string $host Optional - used for SETting, omitted when GETting.
- * @return bool|string
- * @throws InvalidArgumentException
- */
- public function host(string $host = null)
- {
- if (!is_null($host) && !is_string($host)) throw new InvalidArgumentException('!is_null($host) && !is_string($host)');
- return is_null($host) ? $this->getHost() : $this->setHost($host);
- }
- /**
- * @return string
- */
- public function lastInsertId(): string
- {
- return $this->pdo->lastInsertId();
- }
- /**
- * @param int|string $port Optional - used for SETting, omitted when GETting.
- * @return bool|int
- * @throws InvalidArgumentException
- */
- public function port($port = null)
- {
- 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))');
- return is_null($port) ? $this->getPort() : $this->setPort($port);
- }
- /**
- * @param string $query
- * @param array $options
- * @return bool
- * @throws InvalidArgumentException
- */
- public function prepare(string $query, array $options = []): bool
- {
- if (!is_string($query)) throw new InvalidArgumentException('!is_string($query)');
- if (!is_array($options)) throw new InvalidArgumentException('!is_array($options)');
- try
- {
- $this->pdoStatement = $this->pdo->prepare($query, $options);
- return true;
- }
- catch (PDOException $exception)
- {
- $this->addError($exception->getCode(), $exception->getMessage());
- return false;
- }
- }
- /**
- * @return bool
- */
- public function rollBack(): bool
- {
- return $this->pdo->rollBack();
- }
- /**
- * @return int
- */
- public function rowCount(): int
- {
- return $this->pdoStatement->rowCount();
- }
- /**
- * @param string $charset
- * @return bool
- * @throws InvalidArgumentException
- */
- protected function setCharset(string $charset): bool
- {
- if (!is_string($charset)) throw new InvalidArgumentException('!is_string($charset)');
- $this->charset = $charset;
- return true;
- }
- /**
- * @param int $attribute
- * @param mixed $value
- * @return bool
- * @throws InvalidArgumentException
- */
- public function setConnectionAttribute(int $attribute, $value): bool
- {
- if (!is_int($attribute)) throw new InvalidArgumentException('!is_int($attribute)');
- return $this->pdo->setAttribute($attribute, $value);
- }
- /**
- * @param string $databaseName
- * @return bool
- * @throws InvalidArgumentException
- */
- protected function setDatabaseName(string $databaseName): bool
- {
- if (!is_string($databaseName)) throw new InvalidArgumentException('!is_string($databaseName)');
- $this->databaseName = $databaseName;
- return true;
- }
- /**
- * @param string $host
- * @return bool
- * @throws InvalidArgumentException
- */
- protected function setHost(string $host): bool
- {
- if (!is_string($host)) throw new InvalidArgumentException('!is_string($host)');
- $this->host = $host;
- return true;
- }
- /**
- * @param string $password
- * @return bool
- * @throws InvalidArgumentException
- */
- protected function setPassword(string $password): bool
- {
- if (!is_string($password)) throw new InvalidArgumentException('!is_string($password)');
- $this->password = $password;
- return true;
- }
- /**
- * @param int|string $port
- * @return bool
- * @throws InvalidArgumentException
- */
- protected function setPort($port): bool
- {
- 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))');
- $this->port = intval($port);
- return true;
- }
- /**
- * @param int $attribute
- * @param mixed $value
- * @return bool
- * @throws InvalidArgumentException
- */
- public function setStatementAttribute(int $attribute, $value): bool
- {
- if (!is_int($attribute)) throw new InvalidArgumentException('!is_int($attribute)');
- return $this->pdoStatement->setAttribute($attribute, $value);
- }
- /**
- * @param string $username
- * @return bool
- * @throws InvalidArgumentException
- */
- protected function setUsername(string $username): bool
- {
- if (!is_string($username)) throw new InvalidArgumentException('!is_string($username)');
- $this->username = $username;
- return true;
- }
- /**
- * @param string $username Optional - used for SETting, omitted when GETting.
- * @return bool|string
- */
- public function username(string $username = null)
- {
- if (!is_null($username) && !is_string($username)) throw new InvalidArgumentException('!is_null($username) && !is_string($username)');
- return is_null($username) ? $this->getUsername() : $this->setUsername($username);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment