Neuron-PHP

RateLimitFilter extends Filter
in package

Rate limiting filter for HTTP requests.

Implements configurable rate limiting with support for multiple storage backends and flexible key generation strategies.

Table of Contents

Properties

$_blacklist  : array<string|int, mixed>
$_config  : RateLimitConfig
$_ipResolver  : IIpResolver
$_keyStrategy  : string
$_postFn  : Closure|null
$_preFn  : Closure|null
$_storage  : IRateLimitStorage
$_whitelist  : array<string|int, mixed>

Methods

__construct()  : mixed
clear()  : void
Clear all rate limits.
getStorage()  : IRateLimitStorage
Get the storage instance (for testing).
post()  : mixed|null
pre()  : mixed|null
reset()  : void
Reset rate limit for a specific key.
addRateLimitHeaders()  : void
Add the rate limit headers to response.
checkRateLimit()  : void
Check the rate limit for the current request.
generateKey()  : string
Generate a unique key for rate limiting.
getClientIp()  : string
Get the client IP address using the configured IP resolver.
getCustomKey()  : string
Get a custom key for rate limiting.
getLimit()  : int
Get rate limit for a key.
getUserId()  : string
Get user ID for authenticated requests.
getWindow()  : int
Get the time window for a key.
isBlacklisted()  : bool
Check if a key is blacklisted.
isWhitelisted()  : bool
Check if a key is whitelisted.

Properties

$_postFn

private Closure|null $_postFn

$_preFn

private Closure|null $_preFn

Methods

__construct()

public __construct(RateLimitConfig $config[, string $keyStrategy = 'ip' ][, array<string|int, mixed> $whitelist = [] ][, array<string|int, mixed> $blacklist = [] ][, IIpResolver|null $ipResolver = null ]) : mixed
Parameters
$config : RateLimitConfig

Rate limit configuration

$keyStrategy : string = 'ip'

Key generation strategy ('ip', 'user', 'route', 'custom')

$whitelist : array<string|int, mixed> = []

IP addresses or user IDs to exempt from rate limiting

$blacklist : array<string|int, mixed> = []

IP addresses or user IDs to apply stricter limits

$ipResolver : IIpResolver|null = null

Optional IP resolver (defaults to DefaultIpResolver)

Tags
throws
Exception

reset()

Reset rate limit for a specific key.

public reset(string $key) : void
Parameters
$key : string

addRateLimitHeaders()

Add the rate limit headers to response.

protected addRateLimitHeaders(string $key, int $limit, int $window) : void
Parameters
$key : string
$limit : int
$window : int

generateKey()

Generate a unique key for rate limiting.

protected generateKey(RouteMap $route) : string
Parameters
$route : RouteMap
Return values
string

getClientIp()

Get the client IP address using the configured IP resolver.

protected getClientIp() : string
Return values
string

getCustomKey()

Get a custom key for rate limiting.

protected getCustomKey(RouteMap $route) : string
Parameters
$route : RouteMap
Return values
string

getLimit()

Get rate limit for a key.

protected getLimit(string $key) : int
Parameters
$key : string
Return values
int

getUserId()

Get user ID for authenticated requests.

protected getUserId() : string
Return values
string

getWindow()

Get the time window for a key.

protected getWindow(string $key) : int
Parameters
$key : string
Return values
int

isBlacklisted()

Check if a key is blacklisted.

protected isBlacklisted(string $key) : bool
Parameters
$key : string
Return values
bool

isWhitelisted()

Check if a key is whitelisted.

protected isWhitelisted(string $key) : bool
Parameters
$key : string
Return values
bool

        
On this page

Search results