2015-12-02 10:19:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class StaticRepo{
|
|
|
|
|
|
|
|
//contiens l'instance de la Connexion PDO
|
|
|
|
private static $connexion;
|
|
|
|
|
|
|
|
//contiens les informations de connexion a la BDD
|
|
|
|
private static $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return PDO instance de la connexion a la BDD
|
|
|
|
*/
|
|
|
|
public static function getConnexion(){
|
|
|
|
if(static::$config == null){
|
|
|
|
static::$config = json_decode(file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'config.json'),true);
|
|
|
|
}
|
|
|
|
if(static::$connexion == null){
|
|
|
|
static::$connexion = new PDO('mysql:host='.static::$config['host'].';dbname='.static::$config['database'], static::$config['login'], static::$config['password']);
|
|
|
|
}
|
|
|
|
return static::$connexion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool test de la Connexion
|
|
|
|
*/
|
|
|
|
public static function testConnexion(){
|
|
|
|
return static::getConnexion() instanceof PDO;
|
|
|
|
}
|
2015-12-02 10:44:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME LES VALEURS À CLÉS NUMÉRIQUES DANS UN FETCH D'UNE TABLE DE LA BDD
|
|
|
|
*
|
|
|
|
* @fetchData<Array> le résultat d'une $requeteSQL->fetchAll() / $requeteSQL->fetch()
|
|
|
|
*
|
|
|
|
* @return newFetchData<Array> retourne le tableau donné en paramètre mais sans les valeurs à clés numériques
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function delNumeric($fetchData, $oneDimension=false){
|
2015-12-03 09:43:27 +00:00
|
|
|
|
|
|
|
// cas où fetch renvoie FALSE
|
|
|
|
if( $fetchData === false ) return false;
|
2015-12-02 10:44:39 +00:00
|
|
|
|
|
|
|
/* [1] 2 dimensions
|
|
|
|
===============================================*/
|
|
|
|
if( !$oneDimension ){
|
|
|
|
|
|
|
|
// 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 l'indice est un entier
|
|
|
|
unset( $fetchData[$i][$col] ); // on le supprime
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [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 l'indice est un entier
|
|
|
|
unset( $fetchData[$i] ); // on le supprime
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fetchData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// _ _____ ___ _ _ ___ ____
|
|
|
|
// / \ | ___|_ _| \ | |_ _| _ \
|
|
|
|
// / _ \ | |_ | || \| || || |_) |
|
|
|
|
// / ___ \ | _| | || |\ || || _ <
|
|
|
|
// /_/ \_\ |_| |___|_| \_|___|_| \_\
|
|
|
|
|
|
|
|
|
|
|
|
/* Vérifie le type d'une variable
|
|
|
|
*
|
|
|
|
* @variable<mixed> la variable à vérifier
|
|
|
|
* @dbtype<String> le type correspondant à la vérification
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return correct<Boolean> TRUE si le type est bon / FALSE si le type ne match pas
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function checkParam($variable, $dbtype){
|
|
|
|
/* [1] on vérifie que $dbtype est un String
|
|
|
|
=============================================================*/
|
|
|
|
if( !is_string($dbtype) ) return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Vérifications
|
|
|
|
=============================================================*/
|
|
|
|
$checker = true; // contiendra VRAI si la vérification s'avère correcte
|
2015-12-10 08:04:19 +00:00
|
|
|
$matches = [];
|
2015-12-15 08:19:59 +00:00
|
|
|
$len = 8;
|
|
|
|
|
2015-12-10 08:04:19 +00:00
|
|
|
//si on a un type scalairexlongueur, on traite
|
2015-12-15 08:19:59 +00:00
|
|
|
if(preg_match_all('/([A-Z][a-z]+)(\d+)/s', $dbtype,$matches)){
|
2015-12-10 08:04:19 +00:00
|
|
|
$dbtype = $matches[1][0];
|
2015-12-15 08:19:59 +00:00
|
|
|
$len = $matches[2][0];
|
2015-12-10 08:04:19 +00:00
|
|
|
}
|
2015-12-02 10:44:39 +00:00
|
|
|
|
|
|
|
switch($dbtype){
|
|
|
|
// [1] 'M' / 'F'
|
2015-12-10 08:04:19 +00:00
|
|
|
case 'Civilite':
|
2015-12-14 10:01:35 +00:00
|
|
|
$checker = $checker && is_string($variable) && in_array($variable, ['M','F']);
|
2015-12-10 08:04:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// [2] Chaine de caractère (longueur variable)
|
|
|
|
case 'String':
|
2015-12-15 08:19:59 +00:00
|
|
|
$checker = $checker && is_string($variable) && strlen($variable) <= $len;
|
2015-12-02 10:44:39 +00:00
|
|
|
break;
|
|
|
|
|
2015-12-10 08:04:19 +00:00
|
|
|
case 'Integer':
|
2015-12-15 08:19:59 +00:00
|
|
|
$checker = $checker && is_int($variable) && $variable<pow(2, 32);
|
2015-12-02 10:44:39 +00:00
|
|
|
break;
|
|
|
|
|
2015-12-10 08:04:19 +00:00
|
|
|
case 'SmallInteger':
|
2015-12-15 08:19:59 +00:00
|
|
|
$checker = $checker && is_int($variable) && $variable<pow(2, 16);
|
2015-12-10 08:04:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'TinyInteger':
|
2015-12-15 08:19:59 +00:00
|
|
|
$checker = $checker && is_int($variable) && $variable<pow(2, 8);
|
2015-12-10 08:04:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'BigInteger':
|
2015-12-15 08:19:59 +00:00
|
|
|
$checker = $checker && is_int($variable) && $variable<pow(2, 64);
|
2015-12-10 08:04:19 +00:00
|
|
|
break;
|
2015-12-02 10:44:39 +00:00
|
|
|
|
2015-12-10 10:46:29 +00:00
|
|
|
case 'Date':
|
2015-12-15 08:19:59 +00:00
|
|
|
$checker = $checker && is_string($variable) && ( preg_match('/\d{2}\/\d{2}\/\d{4}/', $variable) || preg_match('/\d{4}-\d{2}-\d{2}/', $variable) );
|
2015-12-10 10:46:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Heure':
|
|
|
|
$checker = $checker && is_string($variable) && preg_match('/(\d+):(\d+)/is',$variable);
|
|
|
|
break;
|
|
|
|
|
2015-12-02 10:44:39 +00:00
|
|
|
// [N] Type inconnu
|
|
|
|
default: $checker = false; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On retourne le résultat de la vérif
|
|
|
|
=============================================================*/
|
|
|
|
return $checker;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-02 10:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|