class CalendarWindow extends AdmissionPolicy

Constants

MAX_INTEGER

The largest integer represented exactly by every first-party store.

MIDNIGHT

The local time at which a daily window resets by default.

Properties

string $at

The local time at which a daily window resets.

string $timezone

The timezone whose clock and calendar the window follows.

Methods

__construct(int $maxAttempts, string $period, string $at = self::MIDNIGHT, string|null $timezone = null, string $key = '', int $cost = 1, bool $global = false, Closure|null $afterCallback = null, Closure|null $responseCallback = null)

Create a new calendar-window limit.

by(Stringable|UnitEnum|string|int|null $key)

Set the key of the rate limit.

cost(int $cost)

Set the capacity consumed by each operation.

globally(bool $global = true)

Apply the policy without a named limiter scope.

after(callable $callback)

Set the callback that determines whether the operation should be consumed.

response(callable $callback)

Set the callback that generates a response when the limit is exceeded.

newInstance(string $key, int $cost, bool $global, Closure|null $afterCallback, Closure|null $responseCallback)

Create a copy with the given shared policy values.

string
normalizeKey(Stringable|UnitEnum|string|int|null $key)

Convert a caller key to its canonical string value.

static void
ensurePositive(string $name, int $value)

Validate a positive shared integer.

static int
multiply(int $value, int $multiplier, string $name)

Multiply positive rate limit values without losing integer precision.

static CalendarWindow
perMinute(int $maxAttempts)

Create a new limit that resets at the start of every minute.

static CalendarWindow
perHour(int $maxAttempts)

Create a new limit that resets at the start of every hour.

static CalendarWindow
perDay(int $maxAttempts, string $at = self::MIDNIGHT)

Create a new limit that resets every day at the given local time.

static CalendarWindow
perMonth(int $maxAttempts)

Create a new limit that resets at the start of every month.

timezone(string $timezone)

Set the timezone whose clock and calendar the window follows.

array
window(int $nowMicroseconds)

Get the start and end of the window containing the given time, in epoch microseconds.

Details

at line 39
__construct(int $maxAttempts, string $period, string $at = self::MIDNIGHT, string|null $timezone = null, string $key = '', int $cost = 1, bool $global = false, Closure|null $afterCallback = null, Closure|null $responseCallback = null)

Create a new calendar-window limit.

Parameters

int $maxAttempts
string $period
string $at
string|null $timezone
string $key
int $cost
bool $global
Closure|null $afterCallback
Closure|null $responseCallback

in AdmissionPolicy at line 37
AdmissionPolicy by(Stringable|UnitEnum|string|int|null $key)

Set the key of the rate limit.

Parameters

Stringable|UnitEnum|string|int|null $key

Return Value

AdmissionPolicy

in AdmissionPolicy at line 51
AdmissionPolicy cost(int $cost)

Set the capacity consumed by each operation.

Parameters

int $cost

Return Value

AdmissionPolicy

in AdmissionPolicy at line 70
AdmissionPolicy globally(bool $global = true)

Apply the policy without a named limiter scope.

Parameters

bool $global

Return Value

AdmissionPolicy

in AdmissionPolicy at line 84
AdmissionPolicy after(callable $callback)

Set the callback that determines whether the operation should be consumed.

Parameters

callable $callback

Return Value

AdmissionPolicy

in AdmissionPolicy at line 98
AdmissionPolicy response(callable $callback)

Set the callback that generates a response when the limit is exceeded.

Parameters

callable $callback

Return Value

AdmissionPolicy

at line 150
protected AdmissionPolicy newInstance(string $key, int $cost, bool $global, Closure|null $afterCallback, Closure|null $responseCallback)

Create a copy with the given shared policy values.

Parameters

string $key
int $cost
bool $global
Closure|null $afterCallback
Closure|null $responseCallback

Return Value

AdmissionPolicy

in AdmissionPolicy at line 123
protected string normalizeKey(Stringable|UnitEnum|string|int|null $key)

Convert a caller key to its canonical string value.

Parameters

Stringable|UnitEnum|string|int|null $key

Return Value

string

in AdmissionPolicy at line 135
static protected void ensurePositive(string $name, int $value)

Validate a positive shared integer.

Parameters

string $name
int $value

Return Value

void

in AdmissionPolicy at line 149
static protected int multiply(int $value, int $multiplier, string $name)

Multiply positive rate limit values without losing integer precision.

Parameters

int $value
int $multiplier
string $name

Return Value

int

at line 75
static CalendarWindow perMinute(int $maxAttempts)

Create a new limit that resets at the start of every minute.

Parameters

int $maxAttempts

Return Value

CalendarWindow

at line 83
static CalendarWindow perHour(int $maxAttempts)

Create a new limit that resets at the start of every hour.

Parameters

int $maxAttempts

Return Value

CalendarWindow

at line 91
static CalendarWindow perDay(int $maxAttempts, string $at = self::MIDNIGHT)

Create a new limit that resets every day at the given local time.

Parameters

int $maxAttempts
string $at

Return Value

CalendarWindow

at line 99
static CalendarWindow perMonth(int $maxAttempts)

Create a new limit that resets at the start of every month.

Parameters

int $maxAttempts

Return Value

CalendarWindow

at line 107
CalendarWindow timezone(string $timezone)

Set the timezone whose clock and calendar the window follows.

Parameters

string $timezone

Return Value

CalendarWindow

at line 129
array window(int $nowMicroseconds)

Get the start and end of the window containing the given time, in epoch microseconds.

The end is always later than the given time.

Parameters

int $nowMicroseconds

Return Value

array