Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php require_once 'vendor/autoload.php';
  2.  
  3. class FirestoreDB {
  4.  
  5.   protected $database;
  6.   protected $usermame;
  7.   protected $password;
  8.  
  9.   protected $connection = null;
  10.  
  11.   /**
  12.    * @param $database database path
  13.    * @param $username username
  14.    * @param $password password
  15.    */
  16.   public function __construct (string $database = '', string $username = '', string $password = '') {
  17.     if (preg_match('/[^a-z_\-0-9]/i', $database)) {
  18.       throw new InvalidArgumentException('$database (' . $database . ') has invalid character, allowed: a-z A-Z 0-9');
  19.     }
  20.  
  21.     if (preg_match('/[^a-z_\-0-9]/i', $username)) {
  22.       throw new InvalidArgumentException('$username (' . $username . ') has invalid character, allowed: a-z A-Z 0-9');
  23.     }
  24.  
  25.     if (preg_match('/[^a-z_\-0-9]/i', $password)) {
  26.       throw new InvalidArgumentException('$password (' . $password . ') has invalid character, allowed: a-z A-Z 0-9');
  27.     }
  28.   }
  29.  
  30.   /**
  31.    * connect database
  32.    *
  33.    * @throws RuntimeException if cannot connect to database
  34.    */
  35.   public function connect () : void {
  36.     try {
  37.       $this->connection = ibase_connect (
  38.         $this->database,
  39.         $this->username,
  40.         $this->password
  41.       );
  42.     } catch (Exception $e) {
  43.       throw new RuntimeException('cannot connect to database: ' . $e->getMessage());
  44.     }
  45.   }
  46.  
  47.  
  48.   /**
  49.    * disconnect database
  50.    */
  51.   public function disconnect () : void {
  52.     ibase_close($this->connection);
  53.   }
  54.  
  55.  
  56.   /**
  57.    * check if connection is open and connected
  58.    *
  59.    * @return bool - if database is connected
  60.    */
  61.   public function is_connected () : bool {
  62.     return ($this->connection === null) ? false : true;
  63.   }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement