Flags
in package
Read flags from a target, combining both attachment styles:
- Imperative — flags attached via
FlagDecorator::attach(). - Attribute —
#[Flag(...)]decorations on the closure / method / class.
Mirrors the read half of aiogram.dispatcher.flags (extract_flags,
extract_flags_from_object, get_flag, check_flags). The two storage
paths exist because Python attaches flags by mutating the callback object
directly (value.aiogram_flag = {...}) — PHP can't do that with closures,
so we use a WeakMap + attribute reflection hybrid.
Precedence: imperative attachments come first in extractFlags(), then
attribute-driven ones. getFlag() therefore prefers imperative attachments
when the same name appears twice — useful when the imperative path is being
used to override a class-level default.
Table of Contents
Methods
- checkFlags() : bool
- Predicate: every name in `$required` must appear on `$target` (by flag name, regardless of value). An empty `$required` list is vacuously true.
- extractFlags() : array<int, Flag>
- All flags attached to `$target`, imperative attachments first then attribute-driven ones. The `object` parameter type accepts `\Closure` since closures are objects — a redundant `\Closure|object` union is rejected by PHP 8.5.
- extractFlagsFromObject() : array<int, Flag>
- Class-level `#[Flag]` attributes on the given object. Used when the handler is dispatched against a class rather than a callable — the class itself carries the flag metadata (mirror of upstream's `extract_flags_from_object`).
- getFlag() : Flag|null
- First flag matching `$name`, or `null` if absent. Imperative attachments win over attribute-driven ones — see class docblock for the precedence rationale.
- extractFromAttributes() : array<int, Flag>
- Attribute-only path. For closures we go through `ReflectionFunction`; for objects through `ReflectionObject` (which reads class-level attributes). Method-level attributes on an object are NOT collected here — callers that want a specific method's flags should pass `Closure::fromCallable([$obj, 'method'])` instead.
Methods
checkFlags()
Predicate: every name in `$required` must appear on `$target` (by flag name, regardless of value). An empty `$required` list is vacuously true.
public
static checkFlags(object $target, array<int, string> $required) : bool
Used by middleware-style gates that ask "does this handler opt into all
of these capabilities" — e.g. Flags::checkFlags($h, ['auth', 'paid']).
Parameters
- $target : object
- $required : array<int, string>
Return values
boolextractFlags()
All flags attached to `$target`, imperative attachments first then attribute-driven ones. The `object` parameter type accepts `\Closure` since closures are objects — a redundant `\Closure|object` union is rejected by PHP 8.5.
public
static extractFlags(object $target) : array<int, Flag>
Parameters
- $target : object
Return values
array<int, Flag>extractFlagsFromObject()
Class-level `#[Flag]` attributes on the given object. Used when the handler is dispatched against a class rather than a callable — the class itself carries the flag metadata (mirror of upstream's `extract_flags_from_object`).
public
static extractFlagsFromObject(object $obj) : array<int, Flag>
Parameters
- $obj : object
Return values
array<int, Flag>getFlag()
First flag matching `$name`, or `null` if absent. Imperative attachments win over attribute-driven ones — see class docblock for the precedence rationale.
public
static getFlag(object $target, string $name) : Flag|null
Parameters
- $target : object
- $name : string
Return values
Flag|nullextractFromAttributes()
Attribute-only path. For closures we go through `ReflectionFunction`; for objects through `ReflectionObject` (which reads class-level attributes). Method-level attributes on an object are NOT collected here — callers that want a specific method's flags should pass `Closure::fromCallable([$obj, 'method'])` instead.
private
static extractFromAttributes(object $target) : array<int, Flag>
Parameters
- $target : object