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

__construct(int|float $chances, int|null $outOf = null)

Create a new Lottery instance.

static Lottery
odds(int|float $chances, int|null $outOf = null)

Create a new Lottery instance.

winner(callable $callback)

Set the winner callback.

loser(callable $callback)

Set the loser callback.

mixed
__invoke(mixed ...$args)

Run the lottery.

mixed
choose(int|null $times = null)

Run the lottery.

mixed
runCallback(mixed ...$args)

Run the winner or loser callback, randomly.

bool
wins()

Determine if the lottery "wins" or "loses".

static callable
resultFactory()

The factory that determines the lottery result.

static void
alwaysWin(callable|null $callback = null)

Force the lottery to always result in a win.

static void
alwaysLose(callable|null $callback = null)

Force the lottery to always result in a loss.

static void
fix(array $sequence, callable|null $whenMissing = null)

Set the sequence that will be used to determine lottery results.

static void
forceResultWithSequence(array $sequence, callable|null $whenMissing = null)

Set the sequence that will be used to determine lottery results.

static void
determineResultsNormally()

Indicate that the lottery results should be determined normally.

static void
determineResultNormally()

Indicate that the lottery results should be determined normally.

static void
setResultFactory(callable $factory)

Set the factory that should be used to determine the lottery results.

static void
flushState()

Flush all static state.

Details

at line 50
__construct(int|float $chances, int|null $outOf = null)

Create a new Lottery instance.

Parameters

int|float $chances
int|null $outOf

Exceptions

RuntimeException

at line 68
static Lottery odds(int|float $chances, int|null $outOf = null)

Create a new Lottery instance.

Parameters

int|float $chances
int|null $outOf

Return Value

Lottery

at line 78
Lottery winner(callable $callback)

Set the winner callback.

Parameters

callable $callback

Return Value

Lottery

at line 90
Lottery loser(callable $callback)

Set the loser callback.

Parameters

callable $callback

Return Value

Lottery

at line 100
mixed __invoke(mixed ...$args)

Run the lottery.

Parameters

mixed ...$args

Return Value

mixed

at line 110
mixed choose(int|null $times = null)

Run the lottery.

Parameters

int|null $times

Return Value

mixed

at line 128
protected mixed runCallback(mixed ...$args)

Run the winner or loser callback, randomly.

Parameters

mixed ...$args

Return Value

mixed

at line 138
protected bool wins()

Determine if the lottery "wins" or "loses".

Return Value

bool

at line 146
static protected callable resultFactory()

The factory that determines the lottery result.

Return Value

callable

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.

Parameters

callable|null $callback

Return Value

void

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.

Parameters

callable|null $callback

Return Value

void

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.

Parameters

array $sequence
callable|null $whenMissing

Return Value

void

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.

Parameters

array $sequence
callable|null $whenMissing

Return Value

void

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.

Return Value

void

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.

Return Value

void

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.

Parameters

callable $factory

Return Value

void

at line 284
static void flushState()

Flush all static state.

Return Value

void