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 loss.
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 50
__construct(int|float $chances, int|null $outOf = null)
Create a new Lottery instance.
at line 68
static Lottery
odds(int|float $chances, int|null $outOf = null)
Create a new Lottery instance.
at line 78
Lottery
winner(callable $callback)
Set the winner callback.
at line 90
Lottery
loser(callable $callback)
Set the loser callback.
at line 100
mixed
__invoke(mixed ...$args)
Run the lottery.
at line 110
mixed
choose(int|null $times = null)
Run the lottery.
at line 128
protected mixed
runCallback(mixed ...$args)
Run the winner or loser callback, randomly.
at line 138
protected bool
wins()
Determine if the lottery "wins" or "loses".
at line 146
static protected callable
resultFactory()
The factory that determines the lottery result.
at line 159
static void
alwaysWin(callable|null $callback = null)
Force the lottery to always result in a win.
Without a callback, the result factory persists in static state for the worker lifetime and affects every subsequent lottery.
at line 181
static void
alwaysLose(callable|null $callback = null)
Force the lottery to always result in a loss.
Without a callback, the result factory persists in static state for the worker lifetime and affects every subsequent lottery.
at line 203
static void
fix(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to determine lottery results.
The sequence factory persists in a static property for the worker lifetime and affects every subsequent lottery result.
at line 215
static void
forceResultWithSequence(array $sequence, callable|null $whenMissing = null)
Set the sequence that will be used to determine lottery results.
The sequence factory persists in a static property for the worker lifetime and affects every subsequent lottery result.
at line 251
static void
determineResultsNormally()
Indicate that the lottery results should be determined normally.
Clears the worker-wide result factory; concurrent coroutines may observe different lottery behavior depending on timing.
at line 263
static void
determineResultNormally()
Indicate that the lottery results should be determined normally.
Clears the worker-wide result factory; concurrent coroutines may observe different lottery behavior depending on timing.
at line 275
static void
setResultFactory(callable $factory)
Set the factory that should be used to determine the lottery results.
The factory persists in a static property for the worker lifetime and affects every subsequent lottery result.
at line 284
static void
flushState()
Flush all static state.