class Lottery (View source)

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 callable|null $resultFactory

The factory that should be used to generate results.

Methods

void
__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.

$this
winner(callable $callback)

Set the winner callback.

$this
loser(callable $callback)

Set the loser callback.

mixed
__invoke(mixed ...$args)

Run the lottery.

mixed
choose(null|int $times = null)

Run the lottery.

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

Details

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

Create a new Lottery instance.

Parameters

int|float $chances
int|null $outOf

Return Value

void

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

$this winner(callable $callback)

Set the winner callback.

Parameters

callable $callback

Return Value

$this

$this loser(callable $callback)

Set the loser callback.

Parameters

callable $callback

Return Value

$this

mixed __invoke(mixed ...$args)

Run the lottery.

Parameters

mixed ...$args

Return Value

mixed

mixed choose(null|int $times = null)

Run the lottery.

Parameters

null|int $times

Return Value

mixed

protected callable runCallback(mixed ...$args)

Run the winner or loser callback, randomly.

Parameters

mixed ...$args

Return Value

callable

protected bool wins()

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

Return Value

bool

static protected callable resultFactory()

The factory that determines the lottery result.

Return Value

callable

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

Force the lottery to always result in a win.

Parameters

callable|null $callback

Return Value

void

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

Force the lottery to always result in a lose.

Parameters

callable|null $callback

Return Value

void

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

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

Parameters

array $sequence
callable|null $whenMissing

Return Value

void

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

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

Parameters

array $sequence
callable|null $whenMissing

Return Value

void

static void determineResultsNormally()

Indicate that the lottery results should be determined normally.

Return Value

void

static void determineResultNormally()

Indicate that the lottery results should be determined normally.

Return Value

void

static void setResultFactory(callable $factory)

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

Parameters

callable $factory

Return Value

void