diff --git a/public/index.php b/public/index.php index 813bf9e..b3e6bb8 100644 --- a/public/index.php +++ b/public/index.php @@ -4,6 +4,7 @@ require "../vendor/autoload.php"; require "../config.php"; require "../sql/database.php"; +use App\Connexion; use App\Controller\SampleFormController; use App\Gateway\FormResultGateway; @@ -23,7 +24,7 @@ function get_base_path() { } $basePath = get_base_path(); -$pdo = get_database(); +$pdo = new Connexion(get_database()); // routes initialization $router = new AltoRouter(); diff --git a/src/Connexion.php b/src/Connexion.php new file mode 100644 index 0000000..20526a3 --- /dev/null +++ b/src/Connexion.php @@ -0,0 +1,35 @@ +pdo = $pdo; + } + + public function exec(string $query, array $args) { + $stmnt = $this->pdo->prepare($query); + foreach ($args as $name => $value) { + $stmnt->bindValue($name, $value[0], $value[1]); + } + $stmnt->execute(); + } + + public function fetch(string $query, array $args): array { + $stmnt = $this->pdo->prepare($query); + foreach ($args as $name => $value) { + $stmnt->bindValue($name, $value[0], $value[1]); + } + $stmnt->execute(); + return $stmnt->fetchAll(); + } + +} \ No newline at end of file diff --git a/src/Gateway/FormResultGateway.php b/src/Gateway/FormResultGateway.php index f34ca3a..fe0c601 100644 --- a/src/Gateway/FormResultGateway.php +++ b/src/Gateway/FormResultGateway.php @@ -3,32 +3,31 @@ namespace App\Gateway; use PDO; -use PDOStatement; +use App\Connexion; /** * A sample gateway, that stores the sample form's result. */ class FormResultGateway { - private PDOStatement $insertion_stmnt; - private PDOStatement $list_stmnt; + private Connexion $con; - public function __construct(PDO $pdo) - { - $this->insertion_stmnt = $pdo->prepare("INSERT INTO FormEntries VALUES (:name, :description)"); - $this->list_stmnt = $pdo->prepare("SELECT * FROM FormEntries"); + public function __construct(Connexion $con) { + $this->con = $con; } function insert(string $username, string $description) { - $this->insertion_stmnt->bindValue(":name", $username, PDO::PARAM_STR); - $this->insertion_stmnt->bindValue(":description", $description, PDO::PARAM_STR); - - $this->insertion_stmnt->execute(); + $this->con->exec( + "INSERT INTO FormEntries VALUES (:name, :description)", + [ + ":name" => [$username, PDO::PARAM_STR], + "description" => [$description, PDO::PARAM_STR] + ] + ); } function listResults(): array { - $this->list_stmnt->execute(); - return $this->list_stmnt->fetchAll(); + return $this->con->fetch("SELECT * FROM FormEntries", []); } } \ No newline at end of file