Core\Reflect\AttributeInfo::field
Returns the value of one field of an attribute, by the name of the field.
The value is a string, an int, a bool or a float. The return type is mixed, so you convert the result with as to the type you expect.
field throws a LogicError when the attribute has no field with that name. fields returns the names that exist.
Good to know: field reads a value that is written directly as a text, a number, true or false. It throws a LogicError when the value is a constant, an enum case or the name of a class. Core\Attributes::get reads those values too.
Parameters
$namestringThe field's name, asfieldsreturns it. The text cannot betainted. Check text from a request withCore\Taint::assertTrustedfirst.
Return value
Returns mixed. The value as a string, int, bool or float. These are the four types a payload field can have here.
Errors
LogicErrorThe attribute's payload has no field of that name. This is a mistake in the program, so the method throws an error and does not return an empty value.fieldsreturns the names that exist. Or Novis does not compute the field's value here. That is a class constant, an enum case orFoo::class, which depend on the namespace the attribute is written in. It is also any value thatCore\Reflect\ConstantInfocannot read.Core\Attributes::getreads all of these values at compile time, by shape.
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
Read one value
<?nvs// Shows how to read one value from an attribute, by the name of its field.
attribute RateLimit on class { perMinute: int, scope: string }
#[RateLimit(perMinute: 120, scope: 'user')]class SearchController{}
var $info = Core\Reflect::forObject(new SearchController());
// `field` returns the value as it is written: here an `int` and a `string`.// This prints `120 per minute, per user`.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { int $limit = $attribute->field('perMinute') as int; tainted string $scope = $attribute->field('scope') as string; echo $limit, ' per minute, per ', $scope, "\n";}120 per minute, per userA name that is not there
<?nvs// Shows what happens when you read a field that the attribute does not have.
attribute ImportSettings on class { timeout: int }
#[ImportSettings(timeout: 30)]class Import{}
var $info = Core\Reflect::forObject(new Import());
foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { // The field is `timeout`, not `timeouts`, so `field` throws a `LogicError`. // This prints `no field named timeouts`. try { echo $attribute->field('timeouts') as string, "\n"; } catch (LogicError $error) { echo "no field named timeouts\n"; }}no field named timeoutsCache headers for each page
<?nvs// Shows how to build the `Cache-Control` header for each page from a cache attribute on its method.
attribute Cache on method { seconds: int, shared: bool }
class PageController{ #[Cache(seconds: 3600, shared: true)] public function home(): void {}
#[Cache(seconds: 60, shared: false)] public function account(): void {}}
class CacheHeader{ public static function forPage(string $className, string $method): string { var $info = Core\Reflect::forClass($className); if ($info == null) { return 'no-store'; } foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { if ($attribute->name() != 'Cache' || $attribute->target() != $method) { continue; } int $seconds = $attribute->field('seconds') as int; bool $shared = $attribute->field('shared') as bool; return ($shared ? 'public' : 'private') . ', max-age=' . $seconds as string; } // A page without a `Cache` attribute is not stored. return 'no-store'; }}
// This prints `public, max-age=3600`, then `private, max-age=60`, then `no-store`.echo CacheHeader::forPage('PageController', 'home'), "\n";echo CacheHeader::forPage('PageController', 'account'), "\n";echo CacheHeader::forPage('PageController', 'logout'), "\n";public, max-age=3600private, max-age=60no-store