Download the PHP package dseguy/genie without Composer
On this page you can find all versions of the php package dseguy/genie. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package genie
genie — Systematic Value Generation for PHP
A PHP 8.2+ library for generating all values in a domain using lazy, composable iterators.
Use case: when code needs to walk an entire value domain — all letters, all digit pairs, all 3-character strings — this library provides the building blocks to express that concisely and memory-efficiently.
Installation
Requirements
- PHP 8.2+
- No runtime dependencies
Quick start
Primitive generators
Each primitive is a class with static factory methods. All generators are lazy — values are computed only when iterated.
Letters
Yields individual alphabetic characters.
| Factory method | Yields |
|---|---|
Letters::lower() |
a … z (26 values) |
Letters::upper() |
A … Z (26 values) |
Letters::all() |
a … z then A … Z (52 values) |
Digits
Yields integers over a bounded range.
| Factory method | Yields |
|---|---|
Digits::all() |
0 … 9 |
Digits::range(int $start, int $end, int $step = 1) |
integers from $start to $end inclusive, stepping by $step |
Throws InvalidArgumentException if $start > $end or $step <= 0.
Booleans
Yields boolean (and optionally null) values.
| Factory method | Yields |
|---|---|
Booleans::values() |
true, false |
Booleans::withNull() |
true, false, null |
Enums
Yields values derived from a PHP 8.1+ enum. Three factory methods cover the three things callers typically need from an enum.
| Factory method | Accepts | Yields |
|---|---|---|
Enums::cases(string $class) |
any enum | the enum case objects (\UnitEnum instances) |
Enums::names(string $class) |
any enum | the case names as strings |
Enums::values(string $class) |
backed enum only | the backing values (int or string) |
Throws InvalidArgumentException if the class is not an enum, or if values() is called on a pure (non-backed) enum.
Collection
Wraps an arbitrary PHP array as a generator source. Keys are discarded; only values are yielded.
| Factory method | Yields |
|---|---|
Collection::of(array $items) |
every value in $items, re-indexed from 0 |
FromCallable
Wraps a factory callable that returns a \Traversable (generator, iterator, or iterator aggregate). The factory is invoked fresh on every iteration, making the source fully reusable and safe to use with all combinators.
| Factory method | Accepts | Yields |
|---|---|---|
FromCallable::of(callable $factory) |
callable(): \Traversable |
every value produced by the traversable, keys discarded |
Warning — infinite sources: a factory that yields forever is valid with
filter(),map(), andmerge(), but will loop forever or exhaust memory insideproduct()orrepeat(). There is currently no way to detect an infinite source at construction time; support for this is planned in a future version.
Permutations
Yields all ordered arrangements of $length distinct characters drawn from a charset. No character repeats within a single value.
| Factory method | Yields |
|---|---|
Permutations::of(int $length, string $charset) |
All permutations of exactly $length distinct chars from $charset |
Throws InvalidArgumentException if $length <= 0 or the charset has fewer than $length distinct characters.
Combinators
Combinators are methods available on every generator. They return a new generator and preserve laziness. Chain them in any order.
->merge(GeneratorInterface ...$others) — Union
Chains generators end-to-end into a single sequence.
->product(GeneratorInterface ...$others) — Cartesian product
Yields all combinations as flat arrays (one element per source generator).
Arrays from nested product() or repeat() calls are flattened — values spread into the result rather than nest.
->filter(callable $predicate) — Conditional filter
Excludes values for which $predicate returns falsy.
->map(callable $transform) — Transform
Applies a function to every yielded value.
->repeat(int $n) — Power (G^n)
Yields all $n-length sequences, with repetition allowed.
Each yielded value is a flat array. Throws InvalidArgumentException if $n <= 0.
repeat()vsPermutations:repeat()allows the same value to appear multiple times in one tuple;Permutationsdoes not.
Pragmatic usage examples
Real-world scenarios where systematic value generation is useful.
Password / token brute-force testing
Generate all possible PIN codes or short alphanumeric tokens to verify a rate-limiter or lockout policy in tests.
Exhaustive unit test data providers
Feed a PHPUnit data provider with every combination of inputs rather than hand-picking a few.
Generating test fixtures
Populate a database or in-memory store with a systematic set of records.
Exhaustive enum coverage in tests
Iterate every case of an enum in a data provider so no case is accidentally omitted from test coverage.
Slug / identifier collision detection
Verify that a slug generator produces unique output across all inputs.
Scanning configuration ranges
Walk every valid port in a range, every HTTP status code, or every timeout value to verify system behaviour.
Character set validation
Probe a sanitiser or encoder against every character in a defined alphabet.
Generating SQL / query permutations
Build every variant of a parameterised query for fuzz-style integration tests.
Terminal method
After composing a chain, call ->toIterator() to obtain a plain \Iterator that can be assigned to a variable and passed around.
All generators also implement \IteratorAggregate, so they can be used directly in foreach without calling ->toIterator().
Composing chains
Combinators compose freely. Each step returns a new generator.
API reference
Namespace
Primitive generators
| Class | Factory methods |
|---|---|
Letters |
lower(), upper(), all() |
Digits |
all(), range(int $start, int $end, int $step = 1) |
Booleans |
values(), withNull() |
Enums |
cases(string $class), names(string $class), values(string $class) |
Collection |
of(array $items) |
FromCallable |
of(callable $factory) |
Permutations |
of(int $length, string $charset) |
GeneratorInterface
All generators implement GeneratorInterface, which extends \IteratorAggregate.
| Method | Returns | Description |
|---|---|---|
getIterator() |
\Generator |
Yields domain values |
merge(...$others) |
GeneratorInterface |
Chain generators end-to-end |
product(...$others) |
GeneratorInterface |
Cartesian product |
filter($predicate) |
GeneratorInterface |
Filter by predicate |
map($transform) |
GeneratorInterface |
Transform each value |
repeat(int $n) |
GeneratorInterface |
All n-length sequences |
toIterator() |
\Iterator |
Materialise the chain |
Design principles
- Lazy by default — every generator uses PHP
yield; no value is computed until iterated. - Composable — any generator can be used as input to any combinator.
- Fluent API — combinators chain directly on generator objects.
- Deterministic — fixed iteration order; generators can be re-iterated to produce the same sequence.
- Fail fast — invalid constructor arguments throw
InvalidArgumentExceptionimmediately, never produce silent empty sequences. - No runtime dependencies — pure PHP; only dev dependencies (PHPUnit, PHPStan).
Out of scope (v1)
- Random / shuffled generation
- Weighted generators
- CSV / file-based value sources
- Output adapters (
->toArray(),->toCsv(),->echo()) - Framework integrations (Laravel, Symfony)