Skip to content

Parses

The interface for a class that is built from a piece of text and written back as text.

A class that implements Parses declares two methods. The static method parse(tainted string $s): static checks the text and returns an object of the class. The text is tainted. That means it came from outside your program, so you must check it before you trust it. When the text is not valid, parse throws a ParseError.

The method format() returns the text that parse reads. Its return type is tainted string, or string when the text is always safe.

A route capture, a #[Core\Query] value, a command argument or a command option can have the type of your class. Novis then calls parse, and your code gets an object that is already checked. Core\Router::url and Core\Uri::buildQuery call format to write the object into a link.

Good to know: Parses needs no use. Every program can use it, like Comparable and Stringable.

Examples

A coupon code checked in one place

<?nvs
// A class that implements `Parses` can be built from a piece of text.
// Here the class checks a coupon code in one place. After that, every line
// uses a `CouponCode` object that is already checked.
class CouponCode implements Parses
{
public tainted string $text = '';
public function constructor(tainted string $text)
{
$this->text = $text;
}
// `parse` returns an object of the class. If the text is wrong, it throws
// an error with a message that says why.
public static function parse(tainted string $s): static
{
if (Core\Str::length($s) != 8) {
throw new ParseError('a coupon code is eight characters long');
}
if ($s != Core\Str::upper($s)) {
throw new ParseError('a coupon code is written in capitals');
}
return new static($s);
}
// `format` returns the text that `parse` reads.
public function format(): tainted string
{
return $this->text;
}
}
// Text the class accepts gives you the class.
echo 'accepted ', CouponCode::parse('SUMMER24')->text, "\n";
// Text that is not valid throws a `ParseError`. The message is the one the class wrote.
try {
echo CouponCode::parse('summer24')->text, "\n";
} catch (ParseError $refusal) {
echo 'refused: ', $refusal->message, "\n";
}
// A list of codes that somebody pasted into a form. Each code is passed to `parse`.
// A code that is not valid is reported and skipped. The other codes are kept.
array<string> $pasted = ['SUMMER24', 'AUTUMN7', 'spring01', 'WINTER10'];
uint $kept = 0;
foreach ($pasted as string $row) {
try {
var $code = CouponCode::parse($row);
echo 'keeping ', $code->text, "\n";
$kept = $kept + 1;
} catch (ParseError $refusal) {
echo 'dropping a row: ', $refusal->message, "\n";
}
}
echo $kept, " codes are usable\n";
Output
accepted SUMMER24
refused: a coupon code is written in capitals
keeping SUMMER24
dropping a row: a coupon code is eight characters long
dropping a row: a coupon code is written in capitals
keeping WINTER10
2 codes are usable

Changelog

VersionChange
0.0.6A class that implements Parses must now also declare format(). It returns the text that parse reads, and Core\Router::url uses it to write a link.