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

103 lines
3.4 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;
2026-04-02 18:10:47 +02:00
use App\Kernel;
2026-04-02 16:33:32 +02:00
2026-04-02 18:10:47 +02:00
class IngredientRepository extends Repository implements LinkableInterface {
2026-04-02 16:33:32 +02:00
public static function getEntity(): string
{
return Ingredient::class;
}
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
];
}
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é.
*
* @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;
}
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();
}
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();
}
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
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
public function delete( Model $ingredient ): bool {
return $this->deleteEntity( $ingredient, 'num_ingredient' );
}
2026-04-02 16:33:32 +02:00
}