|
| 1 | +As you might be already familiar WP-FastEndpoints allows you to fetch request data via function arguments, and optionally |
| 2 | +you can validate that data via type-hinting. |
| 3 | + |
| 4 | +### Built-in type-hints |
| 5 | + |
| 6 | +By default, when you rely on built-in PHP types, DateTime, DateTimeInterface or when no type-hint is used, |
| 7 | +WP-FastEndpoints will look for the data in the url or in the query parameters, in this order. However, if desired this |
| 8 | +behaviour can be changed via *#[Attributes]*. |
| 9 | + |
| 10 | +=== "Looks-up for URL or query parameter" |
| 11 | + |
| 12 | + ```php |
| 13 | + <?php |
| 14 | + $router->get('/posts', function (int $ID) { |
| 15 | + // Your logic |
| 16 | + }); |
| 17 | + ``` |
| 18 | + |
| 19 | +=== "Looks-up for a header" |
| 20 | + |
| 21 | + ```php hl_lines="4" |
| 22 | + <?php |
| 23 | + use Attributes\Wp\FastEndpoints\Options\Header; |
| 24 | + |
| 25 | + $router->get('/user', function (#[Header('Authorization')] string $token) { |
| 26 | + // Your logic |
| 27 | + }); |
| 28 | + ``` |
| 29 | + |
| 30 | +The following is a list of all those type-hints: |
| 31 | + |
| 32 | +- bool |
| 33 | +- int |
| 34 | +- float |
| 35 | +- string |
| 36 | +- callable |
| 37 | +- array |
| 38 | +- object |
| 39 | +- [*DateTime*](https://www.php.net/manual/en/class.datetime.php) |
| 40 | +- [*DateTimeInterface*](https://www.php.net/manual/en/class.datetimeinterface.php) |
| 41 | + |
| 42 | +### Custom classes |
| 43 | + |
| 44 | +For more complex types of data structures we can rely on custom classes. If you know how to write a class you know how |
| 45 | +to validate a request in WP-FastEndpoints. |
| 46 | + |
| 47 | +By default, when you rely on type-hints from custom classes WP-FastEndpoints will look for the data either in the |
| 48 | +JSON or body of the current request. However, as with built-in type-hints, this behaviour can be changed via *#[Attributes]*. |
| 49 | + |
| 50 | +```php |
| 51 | +<?php |
| 52 | +class HelloWorld { |
| 53 | + public string $name; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +=== "Looks-up for JSON or body parameters" |
| 58 | + |
| 59 | + ```php |
| 60 | + <?php |
| 61 | + $router->post('/hello', function (HelloWorld $hello) { |
| 62 | + // Your logic |
| 63 | + }); |
| 64 | + ``` |
| 65 | + |
| 66 | +=== "Looks-up for URL or query parameters" |
| 67 | + |
| 68 | + ```php hl_lines="5" |
| 69 | + <?php |
| 70 | + use Attributes\Wp\FastEndpoints\Options\Url; |
| 71 | + use Attributes\Wp\FastEndpoints\Options\Query; |
| 72 | + |
| 73 | + $router->post('/hello', function (#[Url, Query] HelloWorld $hello) { |
| 74 | + // Your logic |
| 75 | + }); |
| 76 | + ``` |
| 77 | + |
| 78 | +#### How to type-hint arrays? |
| 79 | + |
| 80 | +Unfortunately, PHP doesn't provide a way to properly type-hint array's. Dictionaries aren't an issue because custom classes |
| 81 | +can be used, but for sequenced arrays is a bit more tricky. |
| 82 | + |
| 83 | +To solve this issue, WP-FastEndpoints assumes that any [*ArrayObject*](https://www.php.net/manual/en/class.arrayobject.php) |
| 84 | +child class should be considered a *typed-hint* array. However, to actually type-hint that array a property name `$type` |
| 85 | +with a type-hint needs to be specified in the class. Otherwise, an array of `mixed` is assumed. |
| 86 | + |
| 87 | +```php hl_lines="3" |
| 88 | +<?php |
| 89 | +class HelloArr extends ArrayObject { |
| 90 | + public Hello $type; |
| 91 | +} |
| 92 | + |
| 93 | +$router->get('/say-hello', function (HelloArr $allHellos) { |
| 94 | + foreach ($allHellos as $hello) { |
| 95 | + echo "Hello $hello->name!"; |
| 96 | + } |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +!!! tip |
| 101 | + [attributes-php/validation](https://packagist.org/packages/Attributes-PHP/validation) provides some typed-arrays, |
| 102 | + like: 1) [*BoolArr*](https://github.com/Attributes-PHP/validation/blob/main/src/Types/BoolArr.php), |
| 103 | + 2) [*IntArr*](https://github.com/Attributes-PHP/validation/blob/main/src/Types/IntArr.php), |
| 104 | + 3) [*StrArr*](https://github.com/Attributes-PHP/validation/blob/main/src/Types/StrArr.php) and |
| 105 | + [others](https://github.com/Attributes-PHP/validation/tree/main/src/Types). |
| 106 | + |
| 107 | +### Special classes |
| 108 | + |
| 109 | +When an argument has a type-hint it usually means that the request payload should follow that class structure. However, |
| 110 | +there are three special classes which don't follow this pattern: |
| 111 | + |
| 112 | +- [*WP_REST_Request*](https://developer.wordpress.org/reference/classes/wp_rest_request/) - Can be used to retrieve the |
| 113 | + current request |
| 114 | +- [*WP_REST_Response*](https://developer.wordpress.org/reference/classes/wp_rest_response/) - Can be used to retrieve |
| 115 | + the response to be sent to the client, in case of a success. You could change the response HTTP status code or the data |
| 116 | + which is sent to the client (wouldn't recommend the last one though). |
| 117 | +- [*Endpoint*](https://github.com/Attributes-PHP/wp-fastendpoints/blob/main/src/Endpoint.php#L44) - The current endpoint |
| 118 | + instance. You shouldn't ever need this one. |
| 119 | + |
| 120 | +=== "Get current request" |
| 121 | + |
| 122 | + ```php |
| 123 | + <?php |
| 124 | + $router->get('/request', function (WP_REST_Request $request) { |
| 125 | + return $request->get_params(); |
| 126 | + }); |
| 127 | + ``` |
| 128 | + |
| 129 | +=== "Change HTTP status code of a response" |
| 130 | + |
| 131 | + ```php |
| 132 | + <?php |
| 133 | + $router->get('/response/204', function (WP_REST_Response $response) { |
| 134 | + $response->set_status(204); |
| 135 | + }); |
| 136 | + ``` |
| 137 | + |
| 138 | +### Required vs optional properties |
| 139 | + |
| 140 | +Until now, we have seen most how to specify required properties from a request payload. In case your property is optional |
| 141 | +you can rely on default values for that, like the following. |
| 142 | + |
| 143 | +=== "Required property" |
| 144 | + |
| 145 | + ```php hl_lines="2" |
| 146 | + <?php |
| 147 | + $router->get('/required', function (int $id) { |
| 148 | + return $id; |
| 149 | + }); |
| 150 | + ``` |
| 151 | + |
| 152 | +=== "Optional property" |
| 153 | + |
| 154 | + ```php hl_lines="2" |
| 155 | + <?php |
| 156 | + $router->get('/optional', function (int $id = 0) { |
| 157 | + return $id; |
| 158 | + }); |
| 159 | + ``` |
| 160 | + |
| 161 | +=== "Optional payload" |
| 162 | + |
| 163 | + ```php hl_lines="2" |
| 164 | + <?php |
| 165 | + $router->get('/payload/optional', function (?HelloWorld $hello = null) { |
| 166 | + return $hello ? $hello->name : "No payload"; |
| 167 | + }); |
| 168 | + ``` |
0 commit comments