Core\Reflect\AttributeInfo::fields
Returns the names of the fields of an attribute, in the order they are written.
An attribute has fields, and each field has a name and a value. For an attribute written as #[Cache(seconds: 600, shared: true)], fields returns seconds and shared. The result is an array of strings. It has the names only. Use field to read a value.
field throws a LogicError for a name the attribute does not have. When a field is optional, check the list from fields first. A framework can also compare the list with the names it knows, to find a setting that somebody typed wrong.
Good to know: the list has every name, also the name of a field whose value field cannot read.
Return value
Returns array<string>. One string for each field. This includes a field whose value Novis does not compute here. field throws an error for such a field.
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. |
0.0.6 | The server now builds the description of each class once, when it loads your program. Core\Reflect::forClass, Core\Reflect::forObject and Core\Reflect\EnumInfo::of return that description, so two calls for the same class return the same object. A list such as properties() or methods() is built once per request. |
Examples
The field names
<?nvs// Shows the field names of an attribute, in the order they are written.
attribute Cache on method { seconds: int, shared: bool, key: string }
class ReportController{ #[Cache(seconds: 600, shared: true, key: 'daily-report')] public function daily(): void {}}
var $info = Core\Reflect::forObject(new ReportController());
// `fields` returns the names only. This prints `seconds, shared, key`.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { echo Core\Str::join($attribute->fields(), ', '), "\n";}seconds, shared, keyCheck a field before reading it
<?nvs// Shows how to check that an attribute has a field before you read it.
// Both fields are optional. Each use below writes only one of them.attribute JobSettings on method { retries?: int, queue?: string }
class Job{ #[JobSettings(retries: 3)] public function sendReport(): void {}
#[JobSettings(queue: 'low')] public function cleanUp(): void {}}
var $info = Core\Reflect::forObject(new Job());
// `field` throws an error for a name that is not in `fields`, so the loop checks the list first.// Without a `retries` field the value is 1. This prints `sendReport: 3` and `cleanUp: 1`.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { int $retries = 1; if (Core\Arr::contains($attribute->fields(), 'retries')) { $retries = $attribute->field('retries') as int; } echo $attribute->target(), ': ', $retries, "\n";}sendReport: 3cleanUp: 1Find unknown settings
<?nvs// Shows how a framework finds a mistyped setting in the attributes of a class.
attribute Endpoint on method { method: string, path?: string }
class CheckoutController{ #[Endpoint(method: 'POST', path: '/checkout')] public function pay(): void {}
// `pth` is a typing mistake for `path`. The program still compiles, // because an attribute may have more fields than its declaration lists. #[Endpoint(method: 'GET', pth: '/checkout/done')] public function done(): void {}}
class Settings{ public static function unknown(string $className, array<string> $allowed): array<string> { array<string> $problems = []; var $info = Core\Reflect::forClass($className); if ($info == null) { return $problems; } foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { foreach ($attribute->fields() as string $field) { if (!Core\Arr::contains($allowed, $field)) { $problems[] = $attribute->target() . ': unknown setting `' . $field . '`'; } } } return $problems; }}
// The method `done` has the unknown setting `pth`, so this prints one line about it.foreach (Settings::unknown('CheckoutController', ['method', 'path']) as string $problem) { echo $problem, "\n";}done: unknown setting `pth`