NxTIC/build/database/repo/parentRepo.php

117 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace database\repo;
use \database\core\Database;
class parentRepo{
// mise à jour du nom de la table (pour les enfants)
protected static function table_name(){ static $table_name = null; return $table_name; }
/* GESTION DES GETTERS dynamiques
*
* @method<String> Nom du getter du type 'getAll' ou 'getX' avec 'X' une colonne de la table en question
* @args<Array> Liste des arguments, $args[0] est la valeur du getter (sauf pour 'getAll')
*
* @return lines<Array> Retourne le résultat du fetchAll()
*
*/
public static function __callStatic($method, $args){
// Si static::table_name() NULL
if( is_null(static::table_name()) ) return false;
/* [1] On vérifie que la méthode est 'getX', avec X une chaine
=========================================================*/
// Si c'est pas le bon format, on retourne une erreur
if( !preg_match('/^get(?:By(\w+)|(All))$/', $method, $matches) ) return false;
/* [2] On charge la liste des colonnes de la table
=========================================================*/
$getColumns = Database::getPDO()->query('SHOW COLUMNS FROM '.static::table_name());
$cols = Database::delNumeric( $getColumns->fetchAll() );
$table_columns = [
'_PRIMARY_' => [] // Contiendra les champs de la clé primaire
];
// On ajoute la liste des colonnes
foreach($cols as $column){
// On enregistre la clé primaire (si elle l'est)
if( $column['Key'] == 'PRI' ) array_push($table_columns['_PRIMARY_'], $column['Field']);
array_push($table_columns, $column['Field']);
}
/* [3] On vérifie que la valeur après 'get' est dans $table_columns
=========================================================*/
$columnName = strtolower($matches[1]);
$getAll = count($matches) > 2; // Si 'getAll'
$getById = $columnName == 'id';
$getSomething = count($args) > 0 && in_array($columnName, $table_columns); // Si 'getX', et 'X' dans la liste des colonnes
// Si ni 'getAll' ni 'getSomething' -> erreur
if( !$getById && !$getAll && !$getSomething ) return false;
/* [4] On rédige la requête
=========================================================*/
$getRequestString = 'SELECT * FROM '.static::table_name();
// Si c'est 'getById', on ajoute une condition (clé primaire)
if( $getById ){
// S'il manque un paramètre, on retourne une erreur
if( count($args) < count($table_columns['_PRIMARY_']) )
return false;
// Pour chaque clé primaire (si elle est composée)
foreach($table_columns['_PRIMARY_'] as $i=>$primary_column)
// Première ligne
if( $i == 0 ) $getRequestString .= ' WHERE '.$table_columns['_PRIMARY_'][$i].' = :primary'.$i;
// Lignes suivantes
else $getRequestString .= ' AND '.$table_columns['_PRIMARY_'][$i].' = :primary'.$i;
// Si c'est 'getSomething', on ajoute une condition
}else if( $getSomething )
$getRequestString .= ' WHERE '.$columnName.' = :value';
$getRequestString .= ' ORDER BY 1 ASC';
// On prépare la requête
$getRequest = Database::getPDO()->prepare($getRequestString);
/* [5] On exécute la requête
=========================================================*/
// Si 'getById', on compose la clé primaire
if( $getById ){
$pdo_vars = [];
foreach($table_columns['_PRIMARY_'] as $i=>$primary_column)
$pdo_vars[':primary'.$i] = $args[$i];
$getRequest->execute( $pdo_vars );
// Si 'getSomething', on ajoute le champ
}else
$getRequest->execute(array(
':value' => ($getSomething||$getById) ? $args[0] : null
));
/* [6] On récupère le résultat
=========================================================*/
return Database::delNumeric( $getRequest->fetchAll() );
}
}
?>