phil / secret-santa
Run a fair gift exchange: draws that keep households and last year's pairs apart, in a single loop, verifiable by seed, mended when someone drops out.
Requires
- php: >=8.5
- ext-sodium: *
Requires (Dev)
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 02:52:50 UTC
README
Run a fair gift exchange: draws that keep households and last year's pairs apart, in a single loop, verifiable by seed, mended when someone drops out.
phil/secret-santa is a plain PHP library without dependencies. It decides who gives a gift to whom in an exchange — a family, an office, a club, the guests of an event — under the rules the organizer sets, and keeps the draw fair when the list changes after it.
- Rules that people actually ask for: a giver kept from one receiver, two people kept from each other, a whole household, couple or team kept apart in one rule, last year's gifts not repeated. Nobody ever draws themselves.
- A single loop: by default every gift links into one chain through everybody, so no small group ends up giving only among itself and nobody gives to the person who gives to them. Several loops stay possible when the rules leave no single one, or when the organizer prefers.
- A clear refusal: rules that leave someone without anybody to give to, or without anybody to receive from, name that person; rules that leave no complete draw say so, proven, instead of looping forever.
- Uniform when it can be: a draw is a shuffle checked against the rules, so every allowed single loop is equally likely; only rules so tight that hundreds of shuffles fail fall back to a matching, which still finds a draw whenever one exists.
- Verifiable by anyone: the organizer publishes the SHA-256 commitment of a secret seed before drawing and reveals the seed afterwards; the same seed, participants and rules give the same draw on every server, so nobody can have drawn again until the result suited them.
- Repairs that touch little: a withdrawal or a late arrival changes one to three gifts, and the result names the givers to tell; everyone else keeps their name.
- Private by design: a draw holds identifiers only — never names, addresses or wish lists — and
receiverOf()gives each giver their own receiver without showing anyone the rest. - Bounded: up to 1,000 participants and 100,000 rules; a thousand participants draw in under a tenth of a second. Every service keeps no state, safe in a long-running worker.
- Versioned JSON: draws and rules encode to small JSON objects with their version, and decoding checks every field, within a size limit.
Status
| Part | State |
|---|---|
| Rules: forbidden pairs, groups kept apart, last year's gifts | Implemented and tested |
| Drawing a single loop, preferring one, or any complete draw | Implemented and tested |
| Seeded, verifiable draws | Implemented and tested |
| Withdrawals and late arrivals | Implemented and tested |
| The JSON codec of draws and rules | Implemented and tested, fuzzed |
Documentation
| Document | What it covers |
|---|---|
| Installation | Requirements, installing, a first draw. |
| Drawing | Rules, loops, how a draw is found, and when there is none. |
| Verifiable draws | Committing to a seed, revealing it, drawing again to check. |
| Repairs | Withdrawals and late arrivals, and who learns a new name. |
| Storage | The JSON codec, columns, and keeping a draw private. |
| Extension points | What an application may replace or tune, and what is stable. |
| Testing | The tests, the fuzz target and the benchmark. |
| Reference | Every public type, with its role. |
Quick start
composer require phil/secret-santa
use Phil\SecretSanta\Constraints; use Phil\SecretSanta\Drawer; $rules = Constraints::none() ->keepApart('ana', 'ben') // a couple ->keepApart('chloe', 'david', 'emma') // a household ->avoidRepeating($lastYear); // last year's draw, if any $draw = new Drawer()->draw(['ana', 'ben', 'chloe', 'david', 'emma', 'felix'], $rules); $draw->receiverOf('ana'); // the one name Ana should see $draw->isSingleCycle(); // true: one loop through everybody
Requirements
PHP 8.5 or later.
Contributing
Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.
License
MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.