Files
LesRecettesDePapis/src/Domain/Ingredients/IngredientRepository.php

175 lines
5.2 KiB
PHP
Raw Normal View History

2026-04-02 16:33:32 +02:00
<?php
namespace App\Domain\Ingredients;
2026-04-02 18:10:47 +02:00
use App\Domain\LinkableInterface;
use App\Domain\Recettes\Recette;
2026-04-02 16:33:32 +02:00
use App\Domain\Repository;
use App\Domain\Model;
use App\Domain\Tags\Tag;
2026-04-02 18:10:47 +02:00
use App\Kernel;
2026-04-02 16:33:32 +02:00
/**
* 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.
*/
2026-04-02 18:10:47 +02:00
class IngredientRepository extends Repository implements LinkableInterface {
2026-04-02 16:33:32 +02:00
/**
* Permet d'avoir le nom de l'entité dont dépend ce repo.
* @return string
*/
2026-04-02 16:33:32 +02:00
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
*/
2026-04-02 16:33:32 +02:00
public static function getStructure(): array
{
return [
'table' => 'Ingredient',
'columns' => [
'num_ingredient', 'nom_ingredient'
2026-04-02 18:10:47 +02:00
],
'link_recettes' => 'Listeingredient'
2026-04-02 16:33:32 +02:00
];
}
/**
* 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;
}
2026-04-02 18:10:47 +02:00
/**
* 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
2026-04-02 18:10:47 +02:00
* @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
*/
2026-04-02 18:10:47 +02:00
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
*/
2026-04-02 18:10:47 +02:00
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
*/
2026-04-02 16:33:32 +02:00
public function add( Model $ingredient ): bool {
2026-04-02 18:10:47 +02:00
return $this->addEntity( $ingredient );
}
2026-04-02 16:33:32 +02:00
/**
* Met à jour un ingrédient dans la BDD.
*
* @param Model $ingredient
*
* @return bool
*/
2026-04-02 18:10:47 +02:00
public function update( Model $ingredient ): bool {
return $this->updateEntity( $ingredient, 'num_ingredient' );
2026-04-02 16:33:32 +02:00
}
2026-04-02 18:10:47 +02:00
/**
* Supprime un ingrédient de la BDD.
*
* @param Model $ingredient
*
* @return bool
*/
2026-04-02 18:10:47 +02:00
public function delete( Model $ingredient ): bool {
return $this->deleteEntity( $ingredient, 'num_ingredient' );
}
2026-04-02 16:33:32 +02:00
}