Core\Attributes::all
Core\Attributes::all<T>(callable $target, string $member = ""): array<T>Core\Attributes::all<T>( callable $target, string $member = "", ): array<T>
Returns every attribute of one kind attached to a class, a method, a property or a parameter.
You write the attribute's name between < and > at the call. This is the name of an attribute declaration, such as Tag for attribute Tag on class { name: string }. Only attributes written as #[Tag(…)] match. The result is an array, in the order the attributes were written. It is empty when nothing matches.
Name the declaration with a method reference, which is the method's name without a call. User::constructor names the class User itself, and User::save names its method save. For a property or a parameter, add its name as a second argument.
Good to know: Novis reads the attributes while it compiles your program, so this costs nothing while the program runs. Use Core\Attributes::get when a declaration has at most one attribute of this kind.
The examples below read several labels from one class, then the rules on one property, then the permissions a method lists.
Parameters
$targetcallableThe class or function whose attributes are read.$memberstringdefault ""The name of a method, property or parameter of$target. Its attributes are read instead of the attributes of$target. The default is the empty string, which reads$targetitself.
Return value
Returns array<T>. An array<T> of the matching payload objects, in the order they are declared. It is empty when no attribute matches T. A payload matches when it has at least the fields of T, so the empty shape {} matches every attribute.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Attributes::get<Table> returns only the attributes written as #[Table(...)], so two attributes with the same fields are not mixed up. For a method that a class inherits and does not override, get and all return the attributes its parent class wrote on it. Core\Reflect\AttributeInfo::name returns the full name of the declaration. |
Examples
Every label on one class
<?nvs// Shows how to read every attribute of one kind that is attached to a class.
// `repeatable` allows more than one `Tag` on the same class.repeatable attribute Tag on class { name: string }
#[Tag(name: 'users')]#[Tag(name: 'audited')]class User{ public string $email = '';}
// `Core\Attributes::all` returns the matching attributes in the order they were// written. `User::constructor` names the class itself. This prints 2.var $tags = Core\Attributes::all<Tag>(User::constructor);echo Core\Arr::count($tags), "\n";
// This prints `users` and then `audited`.foreach ($tags as var $tag) { echo $tag->name, "\n";}
// A class with no attribute on it. The result is an empty array, so this loop// runs no times and the count is 0.class Session{}
echo Core\Arr::count(Core\Attributes::all<Tag>(Session::constructor)), "\n";2usersaudited0The rules on one property
<?nvs// Shows how to read the attributes of a single property. The second argument is// the name of the property.
repeatable attribute Rule on property { check: string }
class Signup{ #[Rule(check: 'not-empty')] #[Rule(check: 'an-email-address')] public string $email = '';
#[Rule(check: 'at-least-8-characters')] public string $password = '';}
// `Signup::constructor` names the class, and `"email"` picks the property// out of it. This prints `not-empty, an-email-address`.var $rules = Core\Attributes::all<Rule>(Signup::constructor, 'email');string $line = '';foreach ($rules as var $rule) { $line = $line == '' ? $rule->check : $line . ', ' . $rule->check;}echo $line, "\n";
// The other property carries one rule, so this prints 1.echo Core\Arr::count(Core\Attributes::all<Rule>(Signup::constructor, 'password')), "\n";not-empty, an-email-address1The permissions a handler needs
<?nvs// Shows a check you can write before a handler runs. The handler lists the// permissions it needs, and the program reads that list back.
repeatable attribute Needs on method { permission: string }
class Orders{ #[Needs(permission: 'orders.read')] #[Needs(permission: 'orders.write')] public static function cancelAll(): string { return 'every open order is cancelled'; }}
class Access{ // Returns true when `$held` contains every permission in `$needed`. public static function allows(array<{ permission: string }> $needed, array<string> $held): bool { foreach ($needed as var $need) { if (!Core\Arr::contains($held, $need->permission)) { return false; } } return true; }}
// `Orders::cancelAll` names the method. Both of its permissions come back.var $needed = Core\Attributes::all<Needs>(Orders::cancelAll);array<string> $held = ['orders.read'];
// This user may read orders but not write them, so this prints `not allowed`.if (Access::allows($needed, $held)) { echo Orders::cancelAll(), "\n";} else { echo "not allowed\n";}not allowed