Core\Request::queryParametersAs
Core\Request::queryParametersAs<T>({name?: string}): TCore\Request::queryParametersAs<T>( {name?: string}, ): T
Reads the query string of the request into a class or a shape that you write. The query string is the part of the address after the ?, such as q=shoes&page=2. You write the type between < and >: Core\Request::queryParametersAs<Search>(). A class needs the #[Core\Json\Derive] attribute, and each public property reads the parameter with the same name. The address comes from the client, so every string field must be tainted string. This also applies to a class inside a shape or an array, however deep it is.
Every value in a query string is text. A field of another type, such as uint, converts the text the same way as does. Parameters that the type does not name are ignored.
The option name reads only the parameters inside one bracket name. For price[min]=10, you write { name: "price" }, and the field min gets 10.
If a field is missing or its value does not convert, queryParametersAs throws a ParseError. Its issues list has one entry for each wrong field.
The examples below read a search into a class and show the error for a wrong page number. The last one filters a product list by a price range.
Options
namestringoptionalThe parameter whose nested value is read, written without brackets. Brackets work the same way as inqueryParameter. If you leave it out, the whole query string is read.
Return value
Returns T. A new T with every field of its declared type, or one T for each entry for an array<T>. Nothing is kept. Every call reads the query string again, so two callers never get the same object.
Errors
LogicErrorThis program is not handling a request. Or the query string has percent escapes that decode to bytes that are not UTF-8. OrTis a class without#[Json\Derive].ParseErrorA field thatTneeds is missing, orascannot convert its value to the declared type. It is also thrown when the value undernameis not a set of fields. The error has one issue for each wrong field, at its own dotted path, so a form can show the whole list.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Request::queryParameter, queryParameters and queryParametersAs read the query string. formField, formFields and formFieldsAs read a submitted form. The old names were query, queryArray, queryAs, post, postArray and postAs. |
0.0.6 | Some decoders take a type, such as Core\Json::decodeAs and Core\Request::jsonAs. They check every class inside a shape or an array of that type, at any depth. Core\Request::jsonAs, queryParametersAs and formFieldsAs give the error E0810 when such a class has a plain string property. |
Examples
Reading a search into a class
<?nvs// Shows how to read the parameters of an address such as `/search?q=red+shoes&page=2` into your own class.
// With `#[Core\Json\Derive]`, each public property reads the parameter with the same name.// The address comes from the client, so a string property must be `tainted string`.#[Core\Json\Derive]class Search{ public tainted string $q; public uint $page;
public function constructor(tainted string $q, uint $page) { $this->q = $q; $this->page = $page; }}
// The class between `<` and `>` is the class to create.// The text "2" is converted to the number 2 for the `uint` property.var $search = Core\Request::queryParametersAs<Search>();
// This prints "red shoes, page 2".echo $search->q, ', page ', $search->page, "\n";red shoes, page 2A page number that is not a number
<?nvs// Shows what `Core\Request::queryParametersAs` does when a parameter is missing or has the wrong type.
// The address is `/products?page=abc`. "abc" is not a number, and there is no "sort".// `queryParametersAs` checks every field and then throws one `ParseError`.try { var $list = Core\Request::queryParametersAs<tainted { page: uint, sort: string }>(); echo 'page ', $list->page, "\n";} catch (ParseError $error) { // The error's `issues` property has one entry for each wrong field. This prints two lines: // "page: expected uint, found a string" and "sort: required field missing". foreach ($error->issues as { path: string, message: string } $issue) { echo $issue->path, ': ', $issue->message, "\n"; }}page: expected uint, found a stringsort: required field missingFiltering products by price
<?nvs// Shows how a shop filters its product list by a price range from the address.
array<uint> $prices = ['notebook' => 4, 'backpack' => 35, 'lamp' => 60];
// The address is `/products?price[min]=10&price[max]=50&sort=name`.// The option `name` reads only the parameters inside `price[...]`.var $range = Core\Request::queryParametersAs<{ min: uint, max: uint }>({ name: 'price' });
// This prints "backpack".foreach ($prices as string $product => uint $price) { if ($price >= $range->min && $price <= $range->max) { echo $product, "\n"; }}backpack