AndFilter
extends Filter
in package
"Every child must accept" combinator with a kwarg cascade.
This class is named after upstream aiogram.filters.logic._AndFilter
(aiogram/filters/logic.py) but intentionally widens that class's
__call__ semantics in one respect: the kwarg cascade.
Upstream _AndFilter.__call__ (no cascade)
Upstream calls each target as await target.call(*args, **kwargs) where
kwargs is the original set received by __call__ — it does NOT spread
any dict results from earlier siblings into the next call. The accumulated
final_result dict is only returned at the end, not forwarded mid-loop.
PHP widening: intentional kwarg cascade
The PHP port instead spreads [...$kwargs, ...$merged] into every child
(via $target($event, ...[...$kwargs, ...$merged])), so each filter sees
the original kwargs plus every kwarg contributed by any preceding sibling
in the same AndFilter group.
This matches the semantics of upstream's outer dispatcher loop
(HandlerObject.check in aiogram/dispatcher/event/handler.py:114-123),
which does cascade: each FilterObject.call result is merged into
kwargs before the next filter is invoked. Nesting Filter::all
therefore gives user code the same kwarg-propagation behaviour it gets
at the top-level dispatcher layer — no surprising gaps when filters are
composed.
The contract is codified by testKwargCascadeForwardsEarlierFilterReturnsIntoLaterFilters
in tests/Filters/Logic/AndFilterTest.php.
Semantics
- With zero targets, the loop is a no-op and the filter returns
true(identity-accept). - Each child is invoked in declaration order with the original
$kwargsUNIONED with the kwargs every preceding child contributed via an array return (intentional widening over upstream — see above). - A
falsevote short-circuits the chain: later children are skipped and the combinator returnsfalse. (Note: unlikeHandlerObject::check, this combinator's rejection ladder is strictly=== false, not arbitrary falsy values — the abstractFilter::__invokereturn type only admitsbool|array, so we never seenull/0/''/[].) - After every child accepts: return the accumulated kwarg map if any
child contributed entries; otherwise return
true.
Table of Contents
Properties
Methods
- __construct() : mixed
- __invoke() : array<string, mixed>|bool
- Evaluate the filter against an update.
- all() : AndFilter
- Compose an AND across filters: every child must accept, kwargs cascade. PHP equivalent of Python's `f1 & f2`.
- any() : OrFilter
- Compose an OR across filters: the first accepting child wins, no cascade. PHP equivalent of Python's `f1 | f2`.
- invertOf() : InvertFilter
- Invert a filter's accept/reject decision. Named `invertOf` rather than `not` because PHP forbids a static and an instance method sharing one name in a single class (the instance-side `$f->not()` convenience may land in a later task).
Properties
$targets read-only
public
array<int, Filter>
$targets
Methods
__construct()
public
__construct(Filter ...$targets) : mixed
Parameters
- $targets : Filter
__invoke()
Evaluate the filter against an update.
public
__invoke(object $event, mixed ...$kwargs) : array<string, mixed>|bool
$kwargs is captured variadically so that CallableObject::prepareKwargs
detects the variadic tail and passes through the ENTIRE dispatcher kwargs
bag (bot, event_context, state, …) rather than intersecting it down
to only the parameter names literally declared here. The variadic capture
produces a regular array<string, mixed> inside the method body, so
existing accesses like $kwargs['bot'] ?? null continue to work unchanged.
Parameters
- $event : object
- $kwargs : mixed
Return values
array<string, mixed>|bool —See class docblock for the interpretation contract.
all()
Compose an AND across filters: every child must accept, kwargs cascade. PHP equivalent of Python's `f1 & f2`.
public
static all(Filter ...$filters) : AndFilter
Parameters
- $filters : Filter
Return values
AndFilterany()
Compose an OR across filters: the first accepting child wins, no cascade. PHP equivalent of Python's `f1 | f2`.
public
static any(Filter ...$filters) : OrFilter
Parameters
- $filters : Filter
Return values
OrFilterinvertOf()
Invert a filter's accept/reject decision. Named `invertOf` rather than `not` because PHP forbids a static and an instance method sharing one name in a single class (the instance-side `$f->not()` convenience may land in a later task).
public
static invertOf(Filter $filter) : InvertFilter
Parameters
- $filter : Filter