Lottery
class Lottery
Properties
| protected int|float | $chances | The number of expected wins. |
|
| protected int|null | $outOf | The number of potential opportunities to win. |
|
| protected null|callable | $winner | The winning callback. |
|
| protected null|callable | $loser | The losing callback. |
|
| static protected null|callable | $resultFactory | The factory that should be used to generate results. |
Methods
Create a new Lottery instance.
Run the lottery.
Run the lottery.
Run the winner or loser callback, randomly.
Determine if the lottery "wins" or "loses".
The factory that determines the lottery result.
Force the lottery to always result in a win.
Force the lottery to always result in a lose.
Set the sequence that will be used to determine lottery results.
Set the sequence that will be used to determine lottery results.
Indicate that the lottery results should be determined normally.
Indicate that the lottery results should be determined normally.
Set the factory that should be used to determine the lottery results.
Flush all static state.
Details
at line 49
__construct(int|float $chances, int|null $outOf = null)
Create a new Lottery instance.
at line 67
static Lottery
odds(int|float $chances, int|null $outOf = null)
Create a new Lottery instance.
at line 75
Lottery
winner(callable $callback)
Set the winner callback.
at line 85
Lottery
loser(callable $callback)
Set the loser callback.
at line 95
mixed
__invoke(mixed ...$args)
Run the lottery.
at line 103
mixed
choose(int|null $times = null)
Run the lottery.
at line 121
protected mixed
runCallback(mixed ...$args)
Run the winner or loser callback, randomly.
at line 131
protected bool
wins()
Determine if the lottery "wins" or "loses".
at line 139
static protected callable
resultFactory()
The factory that determines the lottery result.
at line 152
static void
alwaysWin(callable|null $callback = null)
Force the lottery to always result in a win.
Tests only. Without a callback, the result factory persists in static state for the worker lifetime and affects every subsequent lottery.
at line 173
static void
alwaysLose(callable|null $callback = null)
Force the lottery to always result in a lose.
Tests only. Without a callback, the result factory persists in static state for the worker lifetime and affects every subsequent lottery.
at line 194
static void
fix(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to determine lottery results.
Tests only. The sequence factory persists in a static property for the worker lifetime and affects every subsequent lottery result.
at line 205
static void
forceResultWithSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to determine lottery results.
Tests only. The sequence factory persists in a static property for the worker lifetime and affects every subsequent lottery result.
at line 237
static void
determineResultsNormally()
Indicate that the lottery results should be determined normally.
at line 248
static void
determineResultNormally()
Indicate that the lottery results should be determined normally.
Tests only. Clears the worker-wide result factory; concurrent coroutines may observe different lottery behavior depending on timing.
at line 259
static void
setResultFactory(callable $factory)
Set the factory that should be used to determine the lottery results.
Tests only. The factory persists in a static property for the worker lifetime and affects every subsequent lottery result.
at line 267
static void
flushState()
Flush all static state.