You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -171,6 +179,12 @@ GET http://host/endpoint/?type[in][]=one&type[in][]=two
171
179
```
172
180
-`Tuples` are not allowed in `IN` filter
173
181
182
+
### Function
183
+
```http request
184
+
GET http://host/endpoint?fullName=(Jon,Snow)
185
+
```
186
+
- there is much more options and possibilities with `functions` which you can see [here](#functions-in-filters)
187
+
174
188
## `Tuples` in filters
175
189
`Tuples`
176
190
- are important in filters if you have some values, which **must** be sent together
@@ -385,6 +399,221 @@ Result:
385
399
value: [ action, fantasy ]
386
400
```
387
401
402
+
## Functions in filters
403
+
With function you can handle all kinds of situations, which might be problematic with just a simple filters like `eq`, etc.
404
+
405
+
Let's see how to work with functions and what is required to do. We will show it right on the example.
406
+
407
+
### Example for `fullName` function
408
+
409
+
#### Expected api
410
+
```http request
411
+
GET http://host/endpoint?fullName=(Jon,Snow)
412
+
```
413
+
☝️ _this shows what we want to offer to our consumers. It's easy and explicit enough._
414
+
415
+
It may even hide some inner differences, for example with simple filters, database column must have same name as the field in the filter, but with function, we can change it.
416
+
417
+
Let's say that in database we have something like:
418
+
```fs
419
+
type Person = {
420
+
first_name: string
421
+
lastname: string
422
+
}
423
+
```
424
+
425
+
#### Initialization
426
+
First of all, you have to define functions you want to use.
427
+
```php
428
+
// in DI container/factory
429
+
$apiFilter = new ApiFilter();
430
+
431
+
$apiFilter->declareFunction(
432
+
'fullName',
433
+
[
434
+
new ParameterDefinition('firstName', 'eq', 'first_name'), // parameter name and field name are different, so we need to define it
435
+
'lastname`, // parameter name and field name are the same and we use the implicit `eq` filter, so it is defined simply
436
+
]
437
+
);
438
+
```
439
+
Method `declareFunction` will create a function with filters based on parameters.
440
+
_There is also [registerFunction](#register-and-execute-function) method, which allows you to pass any function you want. This may be useful when you don't need filter functionality at all or have some custom logic/storage, etc._
441
+
442
+
#### Parsing and applying filters
443
+
Now when request with `?fullName=(Jon,Snow)` comes, `ApiFilter` can parse it to:
-`firstName` parameter uses `eq` filter, has `first_name` column in a storage and is mandatory
545
+
-`surname` parameter uses `eq` filter, has `lastname` column in a storage and its value is `Snow` (_which will always be used and no value can override it_)
546
+
547
+
#### Defined as object
548
+
This allows you to pass same options as with the array, but explicitly defined in the object. (_It even has some special constructor methods to simplify special needs._)
549
+
```php
550
+
$apiFilter->declareFunction('fullName', [
551
+
new ParameterDefinition('firstName', 'eq', 'first_name'),
552
+
new ParameterDefinition('surname', 'eq', 'lastname, new Value('Snow'))
553
+
]);
554
+
```
555
+
556
+
#### Combinations
557
+
All options can be combined to best suite the parameter.
558
+
559
+
##### Declaration
560
+
```php
561
+
$apiFilter->declareFunction('fullNameGrownMan', [
562
+
['firstName', 'eq', 'first_name'],
563
+
'surname',
564
+
['age', 'gte', 'age', 18],
565
+
ParameterDefinition::equalToDefaultValue('gender', new Value('male')),
566
+
]);
567
+
```
568
+
569
+
##### Usage
570
+
```http request
571
+
GET http://endpoint/host?fullNameGrownMan=(Jon,Snow)
572
+
```
573
+
574
+
### Register and Execute function
575
+
Example below is just for explicit demonstration, you should probably never allow execute SQL queries like this.
576
+
577
+
#### Usage in PHP
578
+
```php
579
+
// in DI container/factory
580
+
$apiFilter = new ApiFilter();
581
+
582
+
$apiFilter->registerFunction(
583
+
'sql',
584
+
['query'],
585
+
function (\PDO $client, FunctionParameter $query): \PDOStatement {
0 commit comments