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 * * @return status If added successfully * */ private static function add($label=null){ $conf = self::conf(); /* [1] Default values =========================================================*/ /* (1) If label isn't given */ is_null($label) && ($label = 'default'); /* (2) If label and no path */ if( $label !== 'default' && !isset($conf[$label]) ) return false; /* [3] Instanciates the driver =========================================================*/ try{ /* (1) If local -> instanciates with local configuration */ if( !checkdnsrr($_SERVER['SERVER_NAME'], 'NS') ) self::$instance[$label] = new DatabaseDriver($conf[$label]['local']['host'], $conf[$label]['local']['dbname'], $conf[$label]['local']['user'], $conf[$label]['local']['password']); /* (2) If Remote -> instanciates with Remote configuration */ else self::$instance[$label] = new DatabaseDriver($conf[$label]['remote']['host'], $conf[$label]['remote']['dbname'], $conf[$label]['remote']['user'], $conf[$label]['remote']['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){ $conf = self::conf(); /* [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]) ){ /* (2.1) Try to add the configuration if exists */ if( isset($conf[$label]) ){ self::add($label); return self::get($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, 'dbname' => $this->dbname, '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); else $fetchData[$i][$col] = utf8_encode(utf8_decode($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); else $fetchData[$i] = utf8_encode(utf8_decode($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); } } ?>