PHPackages                             nuffy/cards - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nuffy/cards

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nuffy/cards
===========

Abstraction layer for playing cards

013PHP

Since Apr 14Pushed 1y ago1 watchersCompare

[ Source](https://github.com/NuffZetPand0ra/cards)[ Packagist](https://packagist.org/packages/nuffy/cards)[ RSS](/packages/nuffy-cards/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Cards
=====

[](#cards)

A dependency-free PHP library for handling playing cards, providing an object-oriented abstraction layer for cards, decks, and card collections.

Features
--------

[](#features)

- Create and manage standard 52-card decks
- Draw cards from specific positions (top, specific index)
- Draw specific cards by value
- Draw multiple cards at once
- Manage drawn vs. remaining cards
- Shuffle cards using customizable shuffling strategies
- Iterate through cards (implements Iterator interface)
- Card comparison and validation
- Sort cards in various ways
- No external dependencies

Installation
------------

[](#installation)

```
composer require nuffy/cards
```

Basic Usage
-----------

[](#basic-usage)

```
use nuffy\cards\DeckFactory;
use nuffy\cards\Card\CardFactory;

// Create a standard 52-card deck
$deck = DeckFactory::createNormalDeck();

// Shuffle the deck
$deck->shuffle();

// Draw the top card
$top_card = $deck->draw();

// Draw specific card if it exists in the deck
$king_of_spades = $deck->drawSpecific(CardFactory::createFromString('KS'));
```

Detailed Usage Guide
--------------------

[](#detailed-usage-guide)

### Card Notation

[](#card-notation)

Cards are represented using a two-character string notation:

```
[Rank][Suit]

```

- **Rank**: 2-9, T (Ten), J (Jack), Q (Queen), K (King), A (Ace)
- **Suit**: S (Spades), H (Hearts), D (Diamonds), C (Clubs)

Examples:

- `AS` = Ace of Spades
- `TH` = Ten of Hearts (note: Ten uses 'T', not '10')
- `QD` = Queen of Diamonds
- `2C` = Two of Clubs

### Working with Cards

[](#working-with-cards)

```
use nuffy\cards\Card\CardFactory;
use nuffy\cards\Card\Rank;
use nuffy\cards\Card\Suit;
use nuffy\cards\Card\Card;

// Create a card using the factory
$ace_of_spades = CardFactory::createFromString('AS');

// Create a card directly
$king_of_hearts = new Card(Rank::create('K'), Suit::create('H'));

// Get card components
$rank = $ace_of_spades->getRank();
$suit = $ace_of_spades->getSuit();

// Cards have string representation
echo $ace_of_spades; // Outputs: A♤
```

### Creating and Managing Decks

[](#creating-and-managing-decks)

```
use nuffy\cards\DeckFactory;
use nuffy\cards\Card\CardFactory;

// Create a standard 52-card deck
$deck = DeckFactory::createNormalDeck();

// Shuffle the deck
$deck->shuffle();

// Count remaining cards
$count = count($deck); // or $deck->count();

// Draw the top card
$top_card = $deck->draw();

// Draw from a specific position (0-based index)
$card_at_position_5 = $deck->draw(5);

// Draw multiple cards at once
$five_cards = $deck->drawMultiple(5);

// Draw multiple cards starting from a specific position
$three_cards_from_pos_2 = $deck->drawMultiple(3, 2);

// Draw all remaining cards
$all_remaining = $deck->drawRemaining();

// Check drawn cards
$drawn = $deck->getDrawnCards();

// Get remaining cards
$remaining = $deck->getRemainingCards();

// Return all drawn cards back to the deck
$deck->rewind();

// Only clear the list of drawn cards (don't return them to deck)
$deck->flushDraws();

// Search for a specific card (returns position or null if not found)
$position = $deck->search(CardFactory::createFromString('JD'));

// Sort the deck (default sort or custom sorting function)
$deck->sort();
// Or with custom sort function
$deck->sort(function($a, $b) {
    // Custom sort logic
    return $a->getRank()->getValue()  $b->getRank()->getValue();
});
```

### Iterating Through a Deck

[](#iterating-through-a-deck)

The `Deck` class implements PHP's `Iterator` interface, allowing iteration:

```
$deck = DeckFactory::createNormalDeck();

foreach ($deck as $key => $card) {
    // Each iteration draws the top card
    echo "Position $key: $card\n";

    // After this loop, all cards will be in the "drawn" pile
}
```

### Custom Shuffling Strategies

[](#custom-shuffling-strategies)

The library uses the Strategy pattern for shuffling:

```
use nuffy\cards\DeckFactory;
use nuffy\cards\Deck;
use nuffy\cards\ShufflingStrategy\ShufflingStrategyInterface;

// Create a custom shuffling strategy
class MyShufflingStrategy implements ShufflingStrategyInterface {
    public function shuffle(array &$cards): void {
        // Custom shuffling logic
    }
}

// Create a deck with custom shuffling
$deck = new Deck(new MyShufflingStrategy());
$deck->shuffle();
```

Requirements
------------

[](#requirements)

- PHP 8.0 or higher

Documentation
-------------

[](#documentation)

If you have PHPDocumentor, you can generate the API documentation with:

```
composer run-script update-docs
```

Then find the documentation in the `./docs` directory.

License
-------

[](#license)

MIT License

Author
------

[](#author)

NuffZetPand0ra ()

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

###  Health Score

16

—

LowBetter than 4% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0519ddaf9ad994f7c8ecf82e82edbf208b22a4c6b36ab28afddddfa6784f2ea4?d=identicon)[Esben](/maintainers/Esben)

---

Top Contributors

[![NuffZetPand0ra](https://avatars.githubusercontent.com/u/786349?v=4)](https://github.com/NuffZetPand0ra "NuffZetPand0ra (1 commits)")

### Embed Badge

![Health badge](/badges/nuffy-cards/health.svg)

```
[![Health](https://phpackages.com/badges/nuffy-cards/health.svg)](https://phpackages.com/packages/nuffy-cards)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
