162 lines
4.8 KiB
PHP
162 lines
4.8 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;
|
|
use App\Domain\Tags\Tag;
|
|
use App\Domain\Tags\TagRepository;
|
|
use App\Domain\Tags\UseTagsInterface;
|
|
|
|
/**
|
|
* Classe qui permet de faire le lien entre la BDD et le site pour les recettes.
|
|
* Les recettes sont en lien avec les ingrédients.
|
|
*/
|
|
class RecetteRepository extends Repository implements UseIngredientsInterface, UseTagsInterface {
|
|
|
|
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'obtenir une liste de toutes les recettes objet Recette.
|
|
*
|
|
* @return Recette[]|null
|
|
*/
|
|
public function getAll(): ?array {
|
|
$sqlQuery = "SELECT * FROM {$this->tableName};";
|
|
$results = $this->selectGetAll($sqlQuery);
|
|
if( $results === null )
|
|
return null;
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* Permet d'obtenir toutes les recettes paginées.
|
|
* Mise par défaut dans l'ordre croissant du titre et avec une pagination de 15 éléments.
|
|
*
|
|
* @param int $page
|
|
* @param int $pagination
|
|
*
|
|
* @return array|null
|
|
*/
|
|
public function getAllRecettesBrowse( int $page = 1, int $pagination = 15 ){
|
|
|
|
if( $page <= 0 )
|
|
$page = 1;
|
|
|
|
$offset = ( $page - 1 ) * $pagination;
|
|
|
|
$sqlQuery = "SELECT * FROM {$this->tableName} ORDER BY titre_recette ASC LIMIT {$pagination} OFFSET {$offset};";
|
|
$results = $this->selectGetAll($sqlQuery);
|
|
if( $results === null )
|
|
return null;
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
* 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 );
|
|
if( $response === null )
|
|
return null;
|
|
|
|
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', $entity, $ingredient );
|
|
}
|
|
|
|
public function removeAnIngredient(Ingredient $ingredient, Model $entity): bool
|
|
{
|
|
$ingredientRepo = new IngredientRepository();
|
|
return $ingredientRepo->removeLinkBetween( 'recettes', 'num_recette', $entity, $ingredient );
|
|
}
|
|
|
|
public function getAllLinkedTags(Model $entity): ?array
|
|
{
|
|
$tagRepo = new TagRepository();
|
|
$response = $tagRepo->getIdLinkedTo( 'recettes', 'num_recette', $entity );
|
|
if( $response === null )
|
|
return null;
|
|
|
|
return array_map( function($arr) use($tagRepo) {
|
|
return $tagRepo->getByID( $arr['num_tag'] );
|
|
}, $response );
|
|
}
|
|
|
|
public function addATag(Tag $tag, Model $entity): bool
|
|
{
|
|
$tagRepo = new TagRepository();
|
|
return $tagRepo->addLinkBetween( 'recettes', 'num_recette', $entity, $tag );
|
|
}
|
|
|
|
public function removeATag(Tag $tag, Model $entity): bool
|
|
{
|
|
$tagRepo = new TagRepository();
|
|
return $tagRepo->removeLinkBetween( 'recettes', 'num_recette', $entity, $tag );
|
|
}
|
|
|
|
} |