175 lines
5.2 KiB
PHP
175 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Ingredients;
|
|
|
|
use App\Domain\LinkableInterface;
|
|
use App\Domain\Recettes\Recette;
|
|
use App\Domain\Repository;
|
|
use App\Domain\Model;
|
|
use App\Domain\Tags\Tag;
|
|
use App\Kernel;
|
|
|
|
/**
|
|
* Classe qui va permettre de gérer les requêtes BDD en lien avec les ingrédients.
|
|
* Les ingrédients sont liables à d'autres entités comme les Recettes.
|
|
*/
|
|
class IngredientRepository extends Repository implements LinkableInterface {
|
|
|
|
/**
|
|
* Permet d'avoir le nom de l'entité dont dépend ce repo.
|
|
* @return string
|
|
*/
|
|
public static function getEntity(): string
|
|
{
|
|
return Ingredient::class;
|
|
}
|
|
|
|
/**
|
|
* La structure de notre table dans la BDD.
|
|
* Un champ link_recettes a été ajouté pour donner le nom de la table de lien entre nos recettes.
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getStructure(): array
|
|
{
|
|
return [
|
|
'table' => 'Ingredient',
|
|
'columns' => [
|
|
'num_ingredient', 'nom_ingredient', 'photo_ingredient',
|
|
],
|
|
'link_recettes' => 'Listeingredient'
|
|
];
|
|
|
|
}
|
|
|
|
/**
|
|
* Permet d'obtenir une liste de tous les ingrédients objet Ingreident.
|
|
*
|
|
* @return Ingredient[]|null
|
|
*/
|
|
public function getAll(): ?array {
|
|
$sqlQuery = "SELECT * FROM {$this->tableName};";
|
|
$results = $this->selectGetAll($sqlQuery);
|
|
if( $results === null )
|
|
return null;
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* Permet d'obtenir un ingrédient spécifique par son ID.
|
|
*
|
|
* @param int $id
|
|
*
|
|
* @return Ingredient|null
|
|
*/
|
|
public function getByID( int $id ): ?Ingredient {
|
|
$sqlQuery = "SELECT * FROM {$this->tableName} WHERE num_ingredient = {$id}";
|
|
$results = $this->selectGetAll($sqlQuery);
|
|
if( $results === null || count( $results ) > 1 )
|
|
return null;
|
|
return $results[0];
|
|
}
|
|
|
|
/**
|
|
* Permet d'obtenir, sous forme de liste, toutes les entrées qui lient des ingrédients.
|
|
*
|
|
* @param string $linkedTo La table qui permet de faire la liaison des ingrédients avec une autre entité.
|
|
* @param string $linkingField Le champ qui permet de faire la liaison des ingrédients avec une autre entité.
|
|
* @param Model $linkedEntity L'autre entité.
|
|
*
|
|
* @see LinkableInterface
|
|
* @return array|null
|
|
*/
|
|
public function getIdLinkedTo( string $linkedTo, string $linkingField, Model $linkedEntity ): ?array {
|
|
|
|
$linkName = 'link_' . $linkedTo;
|
|
if( !isset( $this->globalStructure[$linkName]))
|
|
return null;
|
|
|
|
$sqlQuery = "SELECT * FROM {$this->globalStructure[$linkName]} WHERE {$linkingField} = {$linkedEntity->getId()};";
|
|
$results = $this->selectGetAll($sqlQuery, true);
|
|
if( $results === null )
|
|
return null;
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* Permet d'ajouter un lien entre un ingrédient et une autre entité comme Recette.
|
|
*
|
|
* @param string $linkedTo
|
|
* @param string $linkingField
|
|
* @param Model $linkedEntity
|
|
* @param Model $ingredientEntity
|
|
*
|
|
* @see LinkableInterface
|
|
* @return bool
|
|
*/
|
|
public function addLinkBetween( string $linkedTo, string $linkingField, Model $linkedEntity, Model $ingredientEntity ): bool {
|
|
|
|
$linkName = 'link_' . $linkedTo;
|
|
if( !isset( $this->globalStructure[$linkName]))
|
|
return false;
|
|
|
|
$query = "INSERT INTO {$this->globalStructure[$linkName]} ({$linkingField},num_ingredient) VALUES ({$linkedEntity->getId()}, {$ingredientEntity->getID()});";
|
|
$statement = Kernel::$DB->pdo->prepare( $query );
|
|
|
|
return $statement->execute();
|
|
|
|
}
|
|
|
|
/**
|
|
* Retire un lien dans la BDD entre un ingrédient et une autre entité.
|
|
*
|
|
* @param string $linkedTo
|
|
* @param string $linkingField
|
|
* @param Model $linkedEntity
|
|
* @param Model $ingredientEntity
|
|
*
|
|
* @see LinkableInterface
|
|
* @return bool
|
|
*/
|
|
public function removeLinkBetween(string $linkedTo, string $linkingField, Model $linkedEntity, Model $ingredientEntity ): bool
|
|
{
|
|
$linkName = 'link_' . $linkedTo;
|
|
if( !isset( $this->globalStructure[$linkName]))
|
|
return false;
|
|
|
|
$query = "DELETE FROM {$this->globalStructure[$linkName]} WHERE {$linkingField} = {$linkedEntity->getId()} AND num_ingredient = {$ingredientEntity->getId()};";
|
|
$statement = Kernel::$DB->pdo->prepare( $query );
|
|
return $statement->execute();
|
|
}
|
|
|
|
/**
|
|
* Ajouter un ingrédient dans la BDD.
|
|
*
|
|
* @param Model $ingredient
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function add( Model $ingredient ): bool {
|
|
return $this->addEntity( $ingredient );
|
|
}
|
|
|
|
/**
|
|
* Met à jour un ingrédient dans la BDD.
|
|
*
|
|
* @param Model $ingredient
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function update( Model $ingredient ): bool {
|
|
return $this->updateEntity( $ingredient, 'num_ingredient' );
|
|
}
|
|
|
|
/**
|
|
* Supprime un ingrédient de la BDD.
|
|
*
|
|
* @param Model $ingredient
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function delete( Model $ingredient ): bool {
|
|
return $this->deleteEntity( $ingredient, 'num_ingredient' );
|
|
}
|
|
|
|
} |