Skip to content

Commit 30d4f96

Browse files
committed
changed filters validation
1 parent f1dd28d commit 30d4f96

5 files changed

Lines changed: 88 additions & 3 deletions

File tree

app/V1Module/presenters/UsersPresenter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Helpers\MetaFormats\Attributes\Post;
66
use App\Helpers\MetaFormats\Attributes\Query;
77
use App\Helpers\MetaFormats\Attributes\Path;
8+
use App\Helpers\MetaFormats\FormatDefinitions\UserFilterFormat;
89
use App\Helpers\MetaFormats\Validators\VArray;
910
use App\Helpers\MetaFormats\Validators\VBool;
1011
use App\Helpers\MetaFormats\Validators\VEmail;
@@ -29,6 +30,7 @@
2930
use App\Exceptions\BadRequestException;
3031
use App\Helpers\EmailVerificationHelper;
3132
use App\Helpers\AnonymizationHelper;
33+
use App\Helpers\MetaFormats\Validators\VObject;
3234
use App\Model\View\GroupViewFactory;
3335
use App\Model\View\InstanceViewFactory;
3436
use App\Model\View\UserViewFactory;
@@ -130,7 +132,7 @@ public function checkDefault()
130132
required: false,
131133
nullable: true,
132134
)]
133-
#[Query("filters", new VArray(), "Named filters that prune the result.", required: false, nullable: true)]
135+
#[Query("filters", new VObject(UserFilterFormat::class), "Named filters that prune the result.", required: false, nullable: true)]
134136
#[Query(
135137
"locale",
136138
new VString(),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Helpers\MetaFormats\FormatDefinitions;
4+
5+
use App\Helpers\MetaFormats\Attributes\Format;
6+
use App\Helpers\MetaFormats\MetaFormat;
7+
use App\Helpers\MetaFormats\Attributes\FPost;
8+
use App\Helpers\MetaFormats\Attributes\FQuery;
9+
use App\Helpers\MetaFormats\Validators\VBool;
10+
use App\Helpers\MetaFormats\Validators\VInt;
11+
use App\Helpers\MetaFormats\Validators\VMixed;
12+
use App\Helpers\MetaFormats\Validators\VString;
13+
use ArrayAccess;
14+
15+
#[Format(UserFilterFormat::class)]
16+
class UserFilterFormat extends MetaFormat implements ArrayAccess
17+
{
18+
#[FQuery(new VString(), required: false)]
19+
public ?string $search;
20+
21+
#[FQuery(new VString(), required: false)]
22+
public ?string $instanceId;
23+
24+
#[FQuery(new VString(), required: false)]
25+
public ?string $roles;
26+
27+
public function offsetExists(mixed $offset): bool
28+
{
29+
return isset($this->$offset);
30+
}
31+
32+
/**
33+
* Offset to retrieve
34+
* @param mixed $offset The offset to retrieve.
35+
* @return mixed Can return all value types.
36+
*/
37+
public function offsetGet(mixed $offset): mixed
38+
{
39+
return $this->$offset ?? null;
40+
}
41+
42+
/**
43+
* Offset to set
44+
* @param mixed $offset The offset to assign the value to.
45+
* @param mixed $value The value to set.
46+
*/
47+
public function offsetSet(mixed $offset, mixed $value): void
48+
{
49+
$this->$offset = $value;
50+
}
51+
52+
/**
53+
* Offset to unset
54+
* @param mixed $offset The offset to unset.
55+
*/
56+
public function offsetUnset(mixed $offset): void
57+
{
58+
$this->$offset = null;
59+
}
60+
}

app/helpers/MetaFormats/Validators/VObject.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function __construct(string $format, bool $strict = true)
2828

2929
public function validate(mixed $value): bool
3030
{
31+
// query parameters can contain arrays
32+
if (!$this->strict && is_array($value)) {
33+
return true;
34+
}
35+
3136
// fine-grained checking is done in the properties
3237
return $value instanceof MetaFormat;
3338
}

app/helpers/Swagger/AnnotationParameterData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ private function generateSchemaAnnotation(): string
136136
}
137137

138138
$this->addArrayItemsIfArray($body);
139+
$this->addObjectParamsIfObject($body);
139140

140141
return $head . $body->toString();
141142
}

docs/swagger.yaml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4624,9 +4624,26 @@ paths:
46244624
description: 'Named filters that prune the result.'
46254625
required: false
46264626
schema:
4627-
type: array
4628-
items: { }
4627+
properties:
4628+
search:
4629+
description: ''
4630+
type: string
4631+
example: text
4632+
nullable: false
4633+
instanceId:
4634+
description: ''
4635+
type: string
4636+
example: text
4637+
nullable: false
4638+
roles:
4639+
description: ''
4640+
type: string
4641+
example: text
4642+
nullable: false
4643+
type: object
46294644
nullable: true
4645+
style: deepObject
4646+
explode: true
46304647
-
46314648
name: locale
46324649
in: query

0 commit comments

Comments
 (0)