Autocomplete and parsing for class property names in strings
orderBy("customer.email") names class properties in a string. A property is a variable that
belongs to a class. In Novis, the editor completes these names, and a typo gets a red underline.
This works because the string has its own type. propertyPath<Order> is a path of property names of
Order. A string with holes, string<propertyPath<Order>>, can have several paths in one
condition, such as "{total} > ? AND {customer.city} = ?".
Your code can split these strings. Core\Str::parts gives the text and the holes of a condition.
Core\Reflect::steps gives one property for each name of a path. So you can build a query builder,
an ORM or any other library that takes class property names.
This page builds a small data mapper in six steps. Each step adds one thing, and each program runs. The mapper writes SQL text. It does not connect to a database.
One property
Section titled “One property”<?nvs// This program builds a query that sorts orders by one property.
class Order{ public int $total = 0; public string $status = 'paid';}
class Mapper{ // `property<Order>` is the name of one public property of `Order`. // The compiler checks a name that the call writes as a string. public static function orderBy(property<Order> $field): string { return 'SELECT * FROM orders ORDER BY ' . ($field as string); }}
echo Mapper::orderBy('total'), "\n";SELECT * FROM orders ORDER BY totalThe parameter type property<Order> accepts the name of one public property of Order. The
compiler checks the string "total" in the call. If you write "totl", the program does not
compile.
A path
Section titled “A path”<?nvs// This program sorts orders by a property of the customer.
class Customer{ public string $name = '';}
class Order{ public int $total = 0; public ?Customer $customer = null;}
class Mapper{ // `propertyPath<Order>` is a list of property names joined with dots. // Each name is a property of the class that the name before it has. public static function orderBy(propertyPath<Order> $path): string { return 'SELECT * FROM orders ORDER BY ' . ($path as string); }}
echo Mapper::orderBy('total'), "\n";echo Mapper::orderBy('customer.name'), "\n";SELECT * FROM orders ORDER BY totalSELECT * FROM orders ORDER BY customer.namepropertyPath<Order> accepts names joined with dots. customer is a ?Customer, so the next name
is a property of Customer. A path can also go through an array of objects, such as
array<Line>.
The editor shows the names you can use. After the ", it lists the five properties of Order.
Each letter you type keeps only the names that match. After each ., it lists the properties of
the class the path has reached, here the four properties of Customer:
What this recording shows
- Type
'. A list of suggestions opens. It showscreatedAt,customer,idand 2 more. - Type
c. It showscreatedAtandcustomer. - Type
u. It showscustomer. - Press Enter to choose
customer. - Type
.. A list of suggestions opens. It showscity,email,nameand 1 more. - Type
e. It showsemail,nameandphone. - Type
m. It showsemail. - Press Enter to choose
email.
A misspelled name is underlined alone. The rest of the path is not underlined:
What this recording shows
- Type
.. A list of suggestions opens. It showscity,email,nameand 1 more. - Type
em. It showsemail. - Type
ial. It shows no suggestions. - Point at the red underline. The error says:
Customerhas no public property$emialThe error code isE0405.
A condition
Section titled “A condition”<?nvs// This program builds a query condition. The compiler checks its property names.
class Customer{ public string $name = '';}
class Order{ public int $total = 0; public ?Customer $customer = null;}
class Mapper{ // `string<propertyPath<Order>>` is a string with holes. // Each `{...}` in it is a property path of `Order`. public static function where(string<propertyPath<Order>> $condition): string { return 'SELECT * FROM orders WHERE ' . ($condition as string); }}
echo Mapper::where('{total} = ? AND {customer.name} = ?'), "\n";SELECT * FROM orders WHERE {total} = ? AND {customer.name} = ?string<propertyPath<Order>> is a string with holes. Each {...} is a property path of Order,
and the compiler checks it. Write {{ for a { that is not a hole, and }} for a }.
One method for every class
Section titled “One method for every class”<?nvs// This program writes one method in a parent class that checks paths for each child class.
class Model{ // `static` is the class that the call names. // `Order::where` checks paths of `Order`, and `Customer::where` checks paths of `Customer`. public static function where(string<propertyPath<static>> $condition): string { return 'WHERE ' . $condition; }}
class Customer extends Model{ public string $name = ''; public string $email = '';}
class Order extends Model{ public int $total = 0; public ?Customer $customer = null;}
echo Order::where('{customer.email} LIKE ?'), "\n";echo Customer::where('{name} = ?'), "\n";WHERE {customer.email} LIKE ?WHERE {name} = ?In a parameter type, static is the class that the call names. So Order::where checks paths of
Order, and Customer::where checks paths of Customer. You write the method once, in the parent
class.
From holes to SQL
Section titled “From holes to SQL”<?nvs// This program turns a condition with holes into SQL, with a join for each object on a path.
class Customer{ public string $email = '';}
class Order{ public int $total = 0; public ?Customer $customer = null;}
class Mapper{ public static function where(string<propertyPath<Order>> $condition): string { string $joins = ''; string $sql = ''; bool $hole = false; // `Core\Str::parts` gives the text and the holes in turn. The first part is text. foreach (Core\Str::parts($condition) as var $part) { if (!$hole) { $sql = $sql . ($part as string); } else { string $table = 'orders'; string $column = ''; // `Core\Reflect::steps` gives one property for each name in the path. foreach (Core\Reflect::steps($part as propertyPath<Order>) as var $step) { if ($column != '') { $joins = $joins . ' JOIN ' . $column; $table = $column; } $column = $step->name(); } $sql = $sql . $table . '.' . $column; } $hole = !$hole; } return 'SELECT orders.* FROM orders' . $joins . ' WHERE ' . $sql; }}
echo Mapper::where('{total} = ? AND {customer.email} = ?'), "\n";SELECT orders.* FROM orders JOIN customer WHERE orders.total = ? AND customer.email = ?Core\Str::parts gives the text and the holes of the condition in turn. Core\Reflect::steps
gives one property for each name of a path. The mapper adds a JOIN for each object on the path.
A step also has the property’s type, so a library can see if it is an array or can be null.
Novis does not read the SQL text between the holes. A typo in a column name there is found when the query runs. A name in a hole is checked when the program compiles.
The property to sort by, from a request
Section titled “The property to sort by, from a request”<?nvs// This program sorts orders by a property that the address names.// The request is `GET /orders?sort=customer.name&other=customer.passwordHash`.
class Customer{ public string $name = ''; private string $passwordHash = '';}
class Order{ public int $total = 0; public ?Customer $customer = null;}
class Mapper{ public static function orderBy(?tainted string $field): string { // `as ?propertyPath<Order>` checks each name of the path while the program runs. // If a name is wrong or private, the result is `null`. ?propertyPath<Order> $path = $field as ?propertyPath<Order>; if ($path == null) { return 'not a property path'; } return 'SELECT * FROM orders ORDER BY ' . ($path as string); }}
echo Mapper::orderBy(Core\Request::queryParameter('sort')), "\n";echo Mapper::orderBy(Core\Request::queryParameter('other')), "\n";SELECT * FROM orders ORDER BY customer.namenot a property pathA property name from a request is a tainted string. as ?propertyPath<Order> checks each name while
the program runs. customer.passwordHash names a private property, so the result is null. A path
with a wrong name or a brace also gives null.
A condition from a request stays tainted after as string<K>. Only its holes are checked. So it
cannot reach the text of a database query.
Do and don’t
Section titled “Do and don’t”- Do type a parameter
propertyPath<T>when it takes a property name, so each call is checked. - Do convert a property name from a request with
as ?propertyPath<T>, and handle thenull. - Do pass values in a query as parameters, with
?. Put only property names in the holes. - Don’t build a condition from request text. The result is
tainted, and a query does not accept it.
Read more in Concepts: how property name types work.
