Download the PHP package garak/card without Composer
On this page you can find all versions of the php package garak/card. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package card
Short Description A PHP library to manage generic card games
License LGPL-3.0-or-later
Informations about the package card
PHP Card library
Introduction
This library offers a few VO classes to use inside Card-related applications:
Card: represents a Card, for example an ace of spades.Rank: represents the rank value of a Card, for example "A" or "7" ("T" is used for 10, to keep the same length).Suit: represents the card suit, for example spades or diamonds.CardBack: represents the back color of a card, for example red or blue. This allows distinguishing between multiple decks in games played with more than one deck.
Some more classes, more elaborate, are available. They are abstract, and thus require a custom implementation to extend them:
Hand: represents a set of Card objects, usually the ones assigned to a playerHandsTrick: represents a trick of hands (think for example Poker, when players show their hands to declare a winner)CardTrick: represents a trick of cards (think for example the 4 cards of a Bridge turn)
Installation
Just use composer require garak/card.
Usage
Example:
You can also get a full deck:
Multiple Decks
When playing with multiple decks, cards can be distinguished by their back color. Each deck automatically gets a back color, cycling through the available values if you request more decks than backs:
Upgrading from version 0.8
Rank and Suit have been converted from regular classes to backed enums.
The following breaking changes apply.
Instantiation
| Before | After |
|---|---|
new Rank('A') |
Rank::Ace |
new Rank('T') |
Rank::Ten |
new Suit('s') |
Suit::Spades |
new Suit('h') |
Suit::Hearts |
When you only have a string at runtime (e.g. from user input or persistence), use the enum's from() factory:
Invalid values
Previously invalid values threw \InvalidArgumentException.
They now throw \ValueError (the standard PHP exception for invalid enum values):
Use Rank::tryFrom('X') / Suit::tryFrom('X') to get null instead of an exception.
Iterating over all ranks or suits
The public static arrays Rank::$ranks and Suit::$suits have been removed.
Use the standard enum cases() method instead:
Suit::$jokerColors has also been removed; joker suits are now Suit::BlackJoker and Suit::RedJoker.