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 lose.

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 49
__construct(int|float $chances, int|null $outOf = null)

Create a new Lottery instance.

Parameters

int|float $chances
int|null $outOf

Exceptions

RuntimeException

at line 67
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 75
Lottery winner(callable $callback)

Set the winner callback.

Parameters

callable $callback

Return Value

Lottery

at line 85
Lottery loser(callable $callback)

Set the loser callback.

Parameters

callable $callback

Return Value

Lottery

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

Run the lottery.

Parameters

mixed ...$args

Return Value

mixed

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

Run the lottery.

Parameters

int|null $times

Return Value

mixed

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

Run the winner or loser callback, randomly.

Parameters

mixed ...$args

Return Value

mixed

at line 131
protected bool wins()

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

Return Value

bool

at line 139
static protected callable resultFactory()

The factory that determines the lottery result.

Return Value

callable

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.

Parameters

callable|null $callback

Return Value

void

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.

Parameters

callable|null $callback

Return Value

void

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.

Parameters

array $sequence
callable|null $whenMissing

Return Value

void

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.

Parameters

array $sequence
callable|null $whenMissing

Return Value

void

at line 237
static void determineResultsNormally()

Indicate that the lottery results should be determined normally.

Return Value

void

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.

Return Value

void

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.

Parameters

callable $factory

Return Value

void

at line 267
static void flushState()

Flush all static state.

Return Value

void