__CONFIG__.'/database-local.json', 'remote' => __CONFIG__.'/database.json' ]; } private static $path; // Databases configurations files private static $config; // PDO configurations private static $instance = []; // Database driver instance list public $error; /* ATTRIBUTES */ private $host; private $dbname; private $username; private $password; private $pdo; /* CONSTRUCTOR OF A DATABASE DRIVER * * @host Database Server's host * @dbname Database name * @username Database username * @password Database password * */ private function __construct($host, $dbname, $username, $password){ /* (2) Stores configuration */ $this->host = $host; $this->dbname = $dbname; $this->username = $username; $this->password = $password; try{ $this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname, $this->username, $this->password); // On signale que tout s'est bien passe $this->error = ManagerError::Success; }catch(Exception $e){ // On signale qu'il y a une erreur $this->error = ManagerError::PDOConnection; } } /************************************************ **** Multiton Management (static) **** ************************************************/ /* ADDS A NEW CONNECTION * * @label [optional] Database Label * @local [optional] Local configuration path * @remote [optional] Remote configuration path * * @return status If added successfully * */ public static function add($label=null, $local=null, $remote=null){ /* [1] Default values =========================================================*/ /* (1) If label isn't given */ is_null($label) && ($label = 'default'); /* (2) If label and no path */ if( $label !== 'default' && (is_null($local) || is_null($remote)) ) return false; /* [2] Stores paths =========================================================*/ /* (1) If not default, stores new values */ if( $label !== 'default' ) self::$path[$label] = [ 'local' => $local, 'remote' => $remote ]; /* (2) If default, fetch default path */ else self::$path[$label] = self::default_path(); /* [3] Test configuration paths =========================================================*/ /* (1) If paths aren't files */ if( !is_file(self::$path[$label]['local']) || !is_file(self::$path[$label]['remote']) ) return false; /* (2) If paths aren't JSON */ $jLocal = json_decode( file_get_contents(self::$path[$label]['local']), true ); $jRemote = json_decode( file_get_contents(self::$path[$label]['remote']), true ); if( !is_array($jLocal) || !is_array($jRemote) ) return false; /* [4] Instanciates the driver =========================================================*/ try{ /* (1) If local -> instanciates with local configuration */ if( !checkdnsrr($_SERVER['SERVER_NAME'], 'NS') ) self::$instance[$label] = new DatabaseDriver($jLocal['host'], $jLocal['dbname'], $jLocal['user'], $jLocal['password']); /* (2) If Remote -> instanciates with Remote configuration */ else self::$instance[$label] = new DatabaseDriver($jRemote['host'], $jRemote['dbname'], $jRemote['user'], $jRemote['password']); return true; }catch(\Exception $e){ /* (3) If fails */ return false; } } /* GET A DATABASE DRIVER INSTANCE * * @label [optional] Driver's label * * @return driver Multiton * */ public static function get($label=null){ /* [1] Checks arguments =========================================================*/ /* (1) Label default value */ is_null($label) && ($label = 'default'); /* (2) If no label, or unknown label */ if( is_null($label) || !isset(self::$instance[$label]) ) throw new \Exception('Database @label is incorrect.'); /* [2] Returns instance =========================================================*/ return self::$instance[$label]; } /* retourne la connection statique */ public static function getPDO($label=null){ $instance = self::get($label); return $instance->pdo; } public function getConfig(){ return [ 'host' => $this->host, 'username' => $this->username ]; } /*************************************************************/ /* _____ ______ _ _ ______ _____ _ */ /* / ____| ____| \ | | ____| __ \ /\ | | */ /* | | __| |__ | \| | |__ | |__) | / \ | | */ /* | | |_ | __| | . ` | __| | _ / / /\ \ | | */ /* | |__| | |____| |\ | |____| | \ \ / ____ \| |____ */ /* \_____|______|_| \_|______|_| \_\/_/ \_\______| */ /* */ /*************************************************************/ /* SUPPRIME LES VALEURS À CLÉS NUMÉRIQUES DANS UN FETCH D'UNE TABLE DE LA BDD * * @fetchData le résultat d'une $requeteSQL->fetchAll() * @oneDimension FAUX <=> fetchAll ; VRAI <=> fetch * * @return newFetchData retourne le tableau donné en paramètre mais sans les valeurs à clés numériques * */ public static function delNumeric($fetchData, $oneDimension=false){ // On quitte si ce n'est pas un tableau if( !is_array($fetchData) ) return []; $nextEquivalent = false; // Vaut VRAI si le prochain est peut-etre un equivalent numerique /* [1] 2 dimensions ===============================================*/ if( !$oneDimension && isset($fetchData[0]) && is_array($fetchData[0]) ){ // on supprime les doublons des entrées (indice numérique) for( $i = 0 ; $i < count($fetchData) ; $i++ ) // pour tout les utilisateurs foreach($fetchData[$i] as $col => $val){ // pour toutes les entrées if( !\mb_detect_encoding($val, 'UTF-8') ) $fetchData[$i][$col] = utf8_encode($val); if( is_int($col) ){ // Si indice numerique if( $nextEquivalent ) // Si suit un indice textuel unset( $fetchData[$i][$col] ); // on supprime l'indice $nextEquivalent = false; // Dans tous les cas, on dit que le prochain ne pourra pas etre supprime si numerique }else // Si l'indice n'est pas un entier $nextEquivalent = true; // On signale qu'il y aura peut etre un indice numerique suivant } /* [2] 1 dimensions ===============================================*/ }else{ // on supprime les doublons des entrées (indice numérique) foreach($fetchData as $i=>$val){ // pour toutes les entrées if( !\mb_detect_encoding($val, 'UTF-8') ) $fetchData[$i] = utf8_encode($val); if( is_int($i) ){ // Si indice numerique if( $nextEquivalent ) // Si suit un indice textuel unset( $fetchData[$i] ); // on supprime l'indice $nextEquivalent = false; // Dans tous les cas, on dit que le prochain ne pourra pas etre supprime si numerique }else // Si l'indice n'est pas un entier $nextEquivalent = true; // On signale qu'il y aura peut etre un indice numerique suivant } } return $fetchData; } /* FONCTION QUI FORMATTE UN NUMÉRO DE TÉLÉPHONE * * @number Numéro de téléphone en +336/336/06/0336/00336 * * @return formatted Numéro formatté (06), on FALSE si erreur * */ public static function formatNumber($number){ // On met en quel que soit le type $number = (string) $number; // On supprime tous les espaces $number = str_replace(' ', '', $number); // On formatte le numéro if( preg_match("/^(?:\+33|0?0?33|0)(.+)/", $number, $m) ) $number = '0'.$m[1]; // On retourne le numéro formatté return $number; } public static function readableNumber($number){ /* (1) On formatte le numéro si c'est pas fait */ $formatted = self::formatNumber($number); for( $i = 1 ; $i < strlen($formatted) ; $i++ ) if( ($i-2) % 3 == 0 ) $formatted = substr($formatted, 0, $i).' '.substr($formatted, $i); return $formatted; } //////////////////////////////////// // _ _ // __| | __ _| |_ ___ ___ // / _` |/ _` | __/ _ \/ __| // | (_| | (_| | || __/\__ \ // \__,_|\__,_|\__\___||___/ // //////////////////////////////////// // 1) Convertis une date en en francais explicite public static function frDate($date){ /* [1] On definit les traductions =========================================================*/ // Jours de la semaine $days = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"]; // Mois de l'annee $months = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"]; /* [2] On recupere le timestamp et les indices =========================================================*/ $time = strtotime($date); // timestamp $daynum = intval( date('N', $time)-1 ); // jour dans la semaine $monthnum = intval( date('n', $time)-1 ); // numero du mois dans l'annee /* [3] On recupere les infos independemment =========================================================*/ $result = [ $days[$daynum], // nom de jour date('j', $time), // jour du mois $months[$monthnum], // nom du mois date('Y', $time), // annee ]; return implode(" ", $result); } } ?>