Skip to content

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.

<?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";
Output
SELECT * FROM orders ORDER BY total

The 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.

<?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";
Output
SELECT * FROM orders ORDER BY total
SELECT * FROM orders ORDER BY customer.name

propertyPath<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:

How it looks in your editorA recording from the Novis editor extension. You cannot type here.
What this recording shows
  1. Type '. A list of suggestions opens. It shows createdAt, customer, id and 2 more.
  2. Type c. It shows createdAt and customer.
  3. Type u. It shows customer.
  4. Press Enter to choose customer.
  5. Type .. A list of suggestions opens. It shows city, email, name and 1 more.
  6. Type e. It shows email, name and phone.
  7. Type m. It shows email.
  8. Press Enter to choose email.

A misspelled name is underlined alone. The rest of the path is not underlined:

How it looks in your editorA recording from the Novis editor extension. You cannot type here.
What this recording shows
  1. Type .. A list of suggestions opens. It shows city, email, name and 1 more.
  2. Type em. It shows email.
  3. Type ial. It shows no suggestions.
  4. Point at the red underline. The error says: Customer has no public property $emial The error code is E0405.
<?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";
Output
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 }.

<?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";
Output
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.

<?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";
Output
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.

<?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";
nvs serve (HTTP Server Response)
SELECT * FROM orders ORDER BY customer.name
not a property path

A 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 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 the null.
  • 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.