parent
59bd40aea2
commit
4dfa84006f
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace gateway;
|
||||
|
||||
use Exception;
|
||||
use model\Translation;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class TranslationGateway extends AbsGateway
|
||||
{
|
||||
public function addWord(string $word): void {
|
||||
try {
|
||||
$query = "INSERT INTO Vocabulary VALUES (:word) ON DUPLICATE KEY UPDATE word=:word";
|
||||
$args = array(':word' => array($word, PDO::PARAM_STR));
|
||||
$this->con->executeQuery($query, $args);
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function add(array $parameters): int // require 3 elements
|
||||
{
|
||||
try {
|
||||
$this->addWord($parameters[0]);
|
||||
$this->addWord($parameters[1]);
|
||||
$query = "INSERT INTO Translate VALUES(:word1, :word2, :id)";
|
||||
$args = array(':word1' => array($parameters[0], PDO::PARAM_STR),
|
||||
':word2' => array($parameters[1], PDO::PARAM_STR),
|
||||
':id' => array($parameters[2], PDO::PARAM_INT));
|
||||
$this->con->executeQuery($query, $args);
|
||||
return $this->con->lastInsertId();
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
throw new Exception($e->getMessage().+$e->getLine());
|
||||
}
|
||||
}
|
||||
|
||||
public function remove(string $word1, string $word2, int $id): void {
|
||||
try {
|
||||
$query = "DELETE FROM Translate WHERE firstWord=:word1 AND secondWord=:word2 AND listVoc=:id";
|
||||
$args = array(':word1' => array($word1, PDO::PARAM_STR),
|
||||
':word2' => array($word2, PDO::PARAM_STR),
|
||||
':id' => array($id, PDO::PARAM_INT));
|
||||
$this->con->executeQuery($query, $args);
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
throw new Exception($e->getMessage().+$e->getLine());
|
||||
}
|
||||
}
|
||||
|
||||
public function findAll(): array
|
||||
{
|
||||
try {
|
||||
$query = "SELECT * FROM Translate";
|
||||
$this->con->executeQuery($query);
|
||||
$results = $this->con->getResults();
|
||||
$tab = array();
|
||||
foreach ($results as $row) $tab[] = new Translation($row['firstWord'], $row['secondWord'], $row['listVoc']);
|
||||
|
||||
return $tab;
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function findById(int $id)
|
||||
{
|
||||
try {
|
||||
$query = "SELECT * FROM Translate WHERE listVoc=:id";
|
||||
$args = array(':id' => array($id, PDO::PARAM_INT));
|
||||
$this->con->executeQuery($query, $args);
|
||||
$results = $this->con->getResults();
|
||||
$tab = array();
|
||||
foreach ($results as $row) $tab[] = new Translation($row['firstWord'], $row['secondWord'], $row['listVoc']);
|
||||
|
||||
return $tab;
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace model;
|
||||
|
||||
class Translation
|
||||
{
|
||||
private string $word1;
|
||||
private string $word2;
|
||||
private int $listVocab;
|
||||
|
||||
/**
|
||||
* @param string $word1
|
||||
* @param string $word2
|
||||
* @param int $listVocab
|
||||
*/
|
||||
public function __construct(string $word1, string $word2, int $listVocab)
|
||||
{
|
||||
$this->word1 = $word1;
|
||||
$this->word2 = $word2;
|
||||
$this->listVocab = $listVocab;
|
||||
}
|
||||
|
||||
public function getWord1(): string
|
||||
{
|
||||
return $this->word1;
|
||||
}
|
||||
|
||||
public function getWord2(): string
|
||||
{
|
||||
return $this->word2;
|
||||
}
|
||||
|
||||
public function getListVocab(): int
|
||||
{
|
||||
return $this->listVocab;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue