65 lines
2.1 KiB
PHP
65 lines
2.1 KiB
PHP
<?php
|
|
namespace Module\Agefoddx\Classe\GestionBD;
|
|
use Exception;
|
|
use PDO;
|
|
|
|
class bdExterneInteraction {
|
|
private ?int $id;
|
|
private string $nom;
|
|
private string $lien;
|
|
private string $user;
|
|
private ?string $mdp;
|
|
private int $port;
|
|
private ?PDO $pdo;
|
|
|
|
|
|
public function __construct($id, $nom, $lien, $user, $port) {
|
|
$this->id = $id;
|
|
$this->nom = $nom;
|
|
$this->lien = $lien;
|
|
$this->user = $user;
|
|
$this->mdp = "";
|
|
$this->port = $port;
|
|
$this->pdo = null;
|
|
}
|
|
|
|
/**
|
|
* @return bool retourne la valeur de la connection avec la base de donnée et altère la variable pdo
|
|
*/
|
|
public function connectionBD(): bool {
|
|
try {
|
|
$this->pdo = new PDO("mysql:host=$this->lien;port=$this->port;dbname=$this->nom", $this->user, $this->mdp,
|
|
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"));
|
|
// On active le mode d'affichage des erreurs, et le lancement d'exception en cas d'erreur
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (Exception $ex) { return false; }
|
|
return true;
|
|
}
|
|
|
|
public function setMdp($mdp) {$this->mdp = $mdp;}
|
|
|
|
/**
|
|
* @return PDO|null si pdo est null essaye d'activer pdo sinon le return pdo
|
|
*/
|
|
public function getPdo(): ?PDO {
|
|
if ($this->pdo == null){
|
|
$this->connectionBD();
|
|
} return $this->pdo;
|
|
}
|
|
|
|
/**
|
|
* @param array $listeInfo liste des infos nécessaires à la connection bd
|
|
* @param string $mdp mot de passe de la bd entré par l'utilisateur
|
|
* @return bdExterneInteraction renvoie un objet de lui-même
|
|
*/
|
|
public static function construireDepuisSQL(array $listeInfo,string $mdp = ""){
|
|
$objet = (new bdExterneInteraction($listeInfo['idBD'], $listeInfo['nomBD'], $listeInfo['lienBD'], $listeInfo['user'], $listeInfo['port']));
|
|
$objet->setMdp($mdp);
|
|
return $objet;
|
|
}
|
|
|
|
/**
|
|
* @return string renvoie le nom de la bd
|
|
*/
|
|
public function getNomBd(){return $this->nom;}
|
|
} |