bourdeau/handevaluator-bundle
Install via Composer:
composer require bourdeau/handevaluator-bundle
Ensure your config/bundles.php includes:
return [
// ...
Bourdeau\HandEvaluatorBundle\BourdeauHandEvaluatorBundle::class => ['all' => true],
];
Service Injection:
The bundle provides a HandEvaluator service. Inject it into your controller/service:
use Bourdeau\HandEvaluatorBundle\Evaluator\HandEvaluator;
class PokerController extends Controller
{
public function __construct(private HandEvaluator $evaluator) {}
public function evaluateHand(Request $request)
{
$hand = $this->evaluator->evaluate($request->input('cards'));
return response()->json($hand);
}
}
First Use Case:
Evaluate a 5-card hand (e.g., ['2h', '3d', '5s', '9c', 'kd']):
$result = $this->evaluator->evaluate(['2h', '3d', '5s', '9c', 'kd']);
// Returns: ['rank' => 'high_card', 'value' => 13, 'cards' => [...]]
Evaluating Hands:
evaluate() with an array of 5 card strings (e.g., ['Ah', 'Ks', 'Qh', 'Jd', 'Tc']).$hand1 = $this->evaluator->evaluate($hand1Cards);
$hand2 = $this->evaluator->evaluate($hand2Cards);
$winner = $hand1['value'] > $hand2['value'] ? 'Player 1' : 'Player 2';
Texas Hold'em Integration: Combine 2 player cards + 5 community cards:
$playerHand = array_merge($playerCards, $communityCards);
$evaluation = $this->evaluator->evaluate(array_slice($playerHand, 0, 5));
Rank-Based Logic:
Use the rank key to implement game rules (e.g., payouts, tiebreakers):
$ranks = ['high_card', 'pair', 'two_pair', 'three_of_a_kind', ..., 'royal_flush'];
if ($result['rank'] === 'flush') {
// Handle flush-specific logic
}
/\b[A-Ka-k2-9][sdch]\b/).$cache = Cache::remember("hand_{$handHash}", 60, function() use ($hand) {
return $this->evaluator->evaluate($hand);
});
HandEvaluator service in unit tests:
$this->app->instance(HandEvaluator::class, $mockEvaluator);
Card Format Strictness:
Ah, not A♥ or 10d).$normalized = array_map(function($card) {
return strtoupper(substr($card, 0, 1)) . substr($card, 1);
}, $rawCards);
Hand Size Assumption:
// ❌ Wrong: Pass 7 cards directly
// ✅ Correct: Evaluate best 5-card subset
$bestHand = $this->evaluator->evaluate($this->getBestFiveCards($allCards));
Rank Naming Quirks:
straight_flush, not sf or straightflush.$rankLabels = [
'high_card' => 'High Card',
'pair' => 'Pair',
'straight_flush' => 'Straight Flush',
// ...
];
InvalidArgumentException. Catch and log:
try {
$result = $this->evaluator->evaluate($cards);
} catch (\InvalidArgumentException $e) {
Log::error("Invalid card in hand: " . $e->getMessage());
}
['2h', '2d']).['2x'] → will fail).Custom Ranks:
Override the evaluator logic by extending the bundle’s Evaluator\HandEvaluator class:
class CustomHandEvaluator extends \Bourdeau\HandEvaluatorBundle\Evaluator\HandEvaluator
{
protected function getRank($hand)
{
// Custom logic here
return parent::getRank($hand);
}
}
Register the service in config/services.yaml:
services:
App\Evaluator\CustomHandEvaluator:
arguments: ['@hand_evaluator']
tags: ['hand_evaluator']
Event Listeners:
Hook into hand evaluation events (if the bundle supports them; check source for EventDispatcher usage).
Performance Optimization:
$this->evaluator->setCache(new ArrayCache()); // Hypothetical method
How can I help you explore Laravel packages today?