Files
LesRecettesDePapis/src/Domain/Recettes/RecetteRepository.php
2026-04-02 18:10:47 +02:00

93 lines
2.7 KiB
PHP

<?php
namespace App\Domain\Recettes;
use App\Domain\Ingredients\Ingredient;
use App\Domain\Ingredients\IngredientRepository;
use App\Domain\Ingredients\UseIngredientsInterface;
use App\Domain\Model;
use App\Domain\Repository;
class RecetteRepository extends Repository implements UseIngredientsInterface {
public static function getEntity(): string
{
return Recette::class;
}
public static function getStructure(): array
{
return [
'table' => 'Recette',
'columns' => [
'num_recette', 'titre_recette', 'slug', 'description_recette', 'photo', 'publication_date', 'temps_de_preparation'
]
];
}
/**
* Permet d'avoir une recette par un ID.
*
* @param int $id
* @return Recette|null
*/
public function getByID( int $id ): ?Recette {
$sqlQuery = "SELECT * FROM {$this->tableName} WHERE num_recette = {$id}";
$results = $this->selectGetAll($sqlQuery);
if( $results === null || count( $results ) > 1 )
return null;
return $results[0];
}
/**
* Permet d'avoir une recette par un slug.
*
* @param string $slug
* @return Recette|null
*/
public function getBySlug( string $slug ): ?Recette {
$sqlQuery = "SELECT * FROM {$this->tableName} WHERE slug = {$slug}";
$results = $this->selectGetAll($sqlQuery);
if( $results === null || count( $results ) > 1 )
return null;
return $results[0];
}
public function add( Model $recette ): bool {
return $this->addEntity( $recette );
}
public function update( Model $recette ): bool {
return $this->updateEntity( $recette, 'num_recette' );
}
public function delete( Model $recette ): bool {
return $this->deleteEntity( $recette, 'num_recette' );
}
public function getAllLinkedIngredients(Model $entity): ?array
{
$ingredientRepo = new IngredientRepository();
$response = $ingredientRepo->getIdLinkedTo( 'recettes', 'num_recette', $entity );
return array_map( function($arr) use($ingredientRepo) {
return $ingredientRepo->getByID( $arr['num_ingredient'] );
}, $response );
}
public function addAnIngredient(Ingredient $ingredient, Model $entity): bool
{
$ingredientRepo = new IngredientRepository();
return $ingredientRepo->addLinkBetween( 'recettes', 'num_recette', $ingredient, $entity );
}
public function removeAnIngredient(Ingredient $ingredient, Model $entity): bool
{
$ingredientRepo = new IngredientRepository();
return $ingredientRepo->removeLinkBetween( 'recettes', 'num_recette', $ingredient, $entity );
}
}