Core\Reflect::steps
Core\Reflect::steps(
propertyPath<T> $path,
): array<Core\Reflect\PropertyInfo>Returns the property that each part of a property path names.
Core\Reflect::steps takes a property path, such as customer.address.city of type propertyPath<Order>, and returns an array of Core\Reflect\PropertyInfo. There is one entry for each part of the path, in order. Each entry has the property's name, whether it is public, and the type it is declared with.
The type tells you how the path reaches the next part. A type like Customer is one object. A type like ?Customer is an object or null. A type like array<Line> is an array of objects. A library can use this to build a database join for each part of the path.
After an array, a path can take the part @key or @value. @key gives an entry named @key with the type int|string. @value gives an entry named @value with the type of the array's elements, such as Author. Both entries are public. A property name never starts with @, so a library can test the first character to tell these parts apart.
The examples below show each part of a path, a join built for each part, and a check of the property to sort by that a request sends.
Parameters
$pathpropertyPath<T>A property path, such ascustomer.address.cityof typepropertyPath<Order>.
Return value
Returns array<Core\Reflect\PropertyInfo>. One Core\Reflect\PropertyInfo for each part of the path, in order. Each one has the property's name, whether it is public, and its declared type. The type shows whether the next part is reached through an object, a nullable object or an array. The part @key gives a row named @key with the type int|string. The part @value gives a row named @value with the type of the array's elements. Both rows are public.
Errors
LogicErrorstepswas called through a callable value, so the class the path starts at is not known.
Changelog
| Version | Change |
|---|---|
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
Each part of a path
<?nvs// Shows the property and the type of each part of a property path.
class Address{ public string $city = '';}
class Customer{ public string $name = ''; public ?Address $address = null;}
class Order{ public int $total = 0; public ?Customer $customer = null;}
class Show{ public static function steps(propertyPath<Order> $path): void { // `Core\Reflect::steps` returns one entry for each part of the path. foreach (Core\Reflect::steps($path) as var $step) { echo $step->name(), ': ', $step->type() ?? 'no type', "\n"; } }}
// This prints three lines: `customer: ?Customer`, `address: ?Address` and `city: string`.Show::steps('customer.address.city');customer: ?Customeraddress: ?Addresscity: stringA join for each part
<?nvs// Shows how a small query builder adds a database join for each part of a path.
class Customer{ public string $email = '';}
class Line{ public int $quantity = 0;}
class Order{ public Customer $customer; public array<Line> $lines = [];
public function constructor() { $this->customer = new Customer(); }}
class Query{ public static function joins(propertyPath<Order> $path): void { array<Core\Reflect\PropertyInfo> $steps = Core\Reflect::steps($path); uint $i = 0; // Every part but the last one leads to another table. while ($i + 1 < Core\Arr::count($steps)) { string $type = $steps[$i as int]->type() ?? ''; // An object that can be `null`, or an array of objects, needs a LEFT JOIN. // A LEFT JOIN keeps the order when no row matches. if (Core\Str::startsWith($type, '?') || Core\Str::startsWith($type, 'array<')) { echo 'LEFT JOIN ', $steps[$i as int]->name(), "\n"; } else { echo 'JOIN ', $steps[$i as int]->name(), "\n"; } $i = $i + 1; } }}
// This prints `JOIN customer`, because every order has a customer.Query::joins('customer.email');// This prints `LEFT JOIN lines`, because an order can have no lines.Query::joins('lines.quantity');JOIN customerLEFT JOIN linesCheck the property to sort by
<?nvs// Shows how to check the property to sort by that a request sends, before it is used in a query.
class Customer{ public string $name = ''; private string $passwordHash = '';}
class Line{ public int $quantity = 0;}
class Order{ public int $total = 0; public ?Customer $customer = null; public array<Line> $lines = [];}
class Sort{ public static function check(string $field): string { // `as ?propertyPath<Order>` gives `null` when the text is not a path of public properties. ?propertyPath<Order> $path = $field as ?propertyPath<Order>; if ($path == null) { return 'not a property path'; } // A path through an array has many values for one order, so it cannot be the property to sort by. foreach (Core\Reflect::steps($path) as var $step) { if (Core\Str::startsWith($step->type() ?? '', 'array<')) { return 'goes through an array'; } } return 'ok'; }}
// These texts could come from a request, for example from `?sort=customer.name`.array<string> $fields = ['total', 'customer.name', 'lines.quantity', 'customer.passwordHash'];foreach ($fields as string $field) { echo $field, ': ', Sort::check($field), "\n";}total: okcustomer.name: oklines.quantity: goes through an arraycustomer.passwordHash: not a property path