KSA-ORACLE/app/Repositories/CardRepository.php
2025-09-05 17:28:33 +03:00

85 lines
2.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Repositories;
use App\Models\Card;
use Illuminate\Database\Eloquent\Collection;
use App\Repositories\CardRepositoryInterface;
class CardRepository implements CardRepositoryInterface
{
public function all(): Collection
{
return Card::all();
}
public function find(int $id): ?Card
{
return Card::find($id);
}
public function create(array $data): Card
{
return Card::create($data);
}
public function update(int $id, array $data): ?Card
{
$card = Card::find($id);
if (! $card) {
return null;
}
$card->update($data);
return $card;
}
public function delete(int $id): bool
{
$card = Card::find($id);
if (! $card) {
return false;
}
return (bool) $card->delete();
}
/**
* Draw oracle cards
*
* @param int $count Number of cards to draw (1, 6, 18, 21, etc.)
* @return array
*/
public function draw(int $count = 1): array
{
// Récupère toutes les cartes (80 dans la DB)
$cards = Card::all();
// Mélange avec shuffle (FisherYates est fait par Laravel via ->shuffle())
$shuffled = $cards->shuffle();
// Prend les $count premières cartes
$selected = $shuffled->take($count);
// Pour chaque carte, ajoute orientation + description
$results = $selected->map(function ($card) {
$isReversed = (bool) random_int(0, 1); // 50% upright / 50% reversed
return [
'id' => $card->id,
'name' => $card->name,
'image_url' => $card->image_url,
'orientation' => $isReversed ? 'reversed' : 'upright',
'description' => $isReversed ? $card->description_reversed : $card->description_upright,
'symbolism' => $card->symbolism,
'created_at' => now(),
];
});
return $results->toArray();
}
}