Skip to content

Commit 2f22140

Browse files
committed
Allow defining custom parameters for validation on endpoints
1 parent e493f7e commit 2f22140

15 files changed

+222
-20
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Tobyz\JsonApiServer\Endpoint\Concerns;
4+
5+
use Tobyz\JsonApiServer\Schema\Parameter;
6+
7+
trait HasParameters
8+
{
9+
/**
10+
* @var Parameter[]
11+
*/
12+
protected array $parameters = [];
13+
14+
/**
15+
* Set custom parameters for the request.
16+
*
17+
* @param Parameter[] $parameters
18+
*/
19+
public function parameters(array $parameters): static
20+
{
21+
$this->parameters = array_merge($this->parameters, $parameters);
22+
23+
return $this;
24+
}
25+
}

src/Endpoint/Concerns/HasResponse.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
trait HasResponse
1313
{
14-
/** @var Header[] */
14+
/**
15+
* @var Header[]
16+
*/
1517
protected array $headers = [];
1618

17-
/** @var Closure[] */
19+
/**
20+
* @var Closure[]
21+
*/
1822
protected array $responseCallbacks = [];
1923

2024
/**

src/Endpoint/Create.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Psr\Http\Message\ResponseInterface;
77
use RuntimeException;
88
use Tobyz\JsonApiServer\Context;
9+
use Tobyz\JsonApiServer\Endpoint\Concerns\HasParameters;
910
use Tobyz\JsonApiServer\Endpoint\Concerns\HasResponse;
1011
use Tobyz\JsonApiServer\Endpoint\Concerns\MutatesResource;
1112
use Tobyz\JsonApiServer\Endpoint\Concerns\SerializesResourceDocument;
@@ -16,6 +17,7 @@
1617
use Tobyz\JsonApiServer\Resource\Creatable;
1718
use Tobyz\JsonApiServer\Schema\Concerns\HasSchema;
1819
use Tobyz\JsonApiServer\Schema\Concerns\HasVisibility;
20+
use Tobyz\JsonApiServer\Schema\Parameter;
1921
use Tobyz\JsonApiServer\SchemaContext;
2022

2123
use function Tobyz\JsonApiServer\has_value;
@@ -24,6 +26,7 @@
2426
class Create implements Endpoint, ProvidesRootSchema
2527
{
2628
use HasVisibility;
29+
use HasParameters;
2730
use HasResponse;
2831
use HasSchema;
2932
use MutatesResource;
@@ -59,7 +62,7 @@ public function handle(Context $context): ?ResponseInterface
5962
throw new ForbiddenException();
6063
}
6164

62-
$context = $context->withParameters($this->resourceDocumentParameters());
65+
$context = $context->withParameters($this->getParameters());
6366

6467
$data = $this->parseData($context);
6568

@@ -181,6 +184,10 @@ public function rootSchema(SchemaContext $context): array
181184
"/$type" => [
182185
'post' => $this->mergeSchema([
183186
'tags' => [$type],
187+
'parameters' => array_map(
188+
fn(Parameter $parameter) => $parameter->getSchema($context),
189+
$this->getParameters(),
190+
),
184191
'requestBody' => [
185192
'required' => true,
186193
'content' => [
@@ -203,4 +210,9 @@ public function rootSchema(SchemaContext $context): array
203210
],
204211
];
205212
}
213+
214+
protected function getParameters(): array
215+
{
216+
return [...$this->resourceDocumentParameters(), ...$this->parameters];
217+
}
206218
}

src/Endpoint/Delete.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Psr\Http\Message\ResponseInterface;
66
use RuntimeException;
77
use Tobyz\JsonApiServer\Context;
8+
use Tobyz\JsonApiServer\Endpoint\Concerns\HasParameters;
89
use Tobyz\JsonApiServer\Endpoint\Concerns\HasResponse;
910
use Tobyz\JsonApiServer\Endpoint\Concerns\ResolvesModel;
1011
use Tobyz\JsonApiServer\Endpoint\Concerns\SerializesDocument;
@@ -13,10 +14,12 @@
1314
use Tobyz\JsonApiServer\Resource\Deletable;
1415
use Tobyz\JsonApiServer\Schema\Concerns\HasSchema;
1516
use Tobyz\JsonApiServer\Schema\Link;
17+
use Tobyz\JsonApiServer\Schema\Parameter;
1618
use Tobyz\JsonApiServer\SchemaContext;
1719

1820
class Delete implements Endpoint, ProvidesRootSchema, ProvidesResourceLinks
1921
{
22+
use HasParameters;
2023
use HasResponse;
2124
use HasSchema;
2225
use ResolvesModel;
@@ -47,6 +50,8 @@ public function handle(Context $context): ?ResponseInterface
4750
);
4851
}
4952

53+
$context = $context->withParameters($this->parameters);
54+
5055
$context->resource->delete($context->model, $context);
5156

5257
return $this->createResponse($this->serializeDocument($context), $context)->withStatus(204);
@@ -68,6 +73,10 @@ public function rootSchema(SchemaContext $context): array
6873
'required' => true,
6974
'schema' => ['type' => 'string'],
7075
],
76+
...array_map(
77+
fn(Parameter $parameter) => $parameter->getSchema($context),
78+
$this->parameters,
79+
),
7180
],
7281
'responses' => [
7382
'204' => [

src/Endpoint/Index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Psr\Http\Message\ResponseInterface as Response;
66
use RuntimeException;
77
use Tobyz\JsonApiServer\Context;
8+
use Tobyz\JsonApiServer\Endpoint\Concerns\HasParameters;
89
use Tobyz\JsonApiServer\Endpoint\Concerns\HasResponse;
910
use Tobyz\JsonApiServer\Endpoint\Concerns\ResolvesList;
1011
use Tobyz\JsonApiServer\Endpoint\Concerns\SerializesResourceDocument;
@@ -24,6 +25,7 @@
2425
class Index implements Endpoint, ProvidesRootSchema
2526
{
2627
use HasVisibility;
28+
use HasParameters;
2729
use HasResponse;
2830
use HasSchema;
2931
use ResolvesList;
@@ -142,6 +144,7 @@ protected function getParameters(Collection $collection): array
142144
return [
143145
...$this->resourceDocumentParameters(),
144146
...$this->listParameters($collection, $this->defaultSort, $this->pagination),
147+
...$this->parameters,
145148
];
146149
}
147150
}

src/Endpoint/ResourceAction.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Psr\Http\Message\ResponseInterface;
77
use Tobyz\JsonApiServer\Context;
8+
use Tobyz\JsonApiServer\Endpoint\Concerns\HasParameters;
89
use Tobyz\JsonApiServer\Endpoint\Concerns\HasResponse;
910
use Tobyz\JsonApiServer\Endpoint\Concerns\ResolvesModel;
1011
use Tobyz\JsonApiServer\Endpoint\Concerns\SerializesResourceDocument;
@@ -19,6 +20,7 @@
1920
class ResourceAction implements Endpoint, ProvidesRootSchema
2021
{
2122
use HasVisibility;
23+
use HasParameters;
2224
use HasResponse;
2325
use HasSchema;
2426
use ResolvesModel;
@@ -60,7 +62,7 @@ public function handle(Context $context): ?ResponseInterface
6062
throw new ForbiddenException();
6163
}
6264

63-
$context = $context->withParameters($this->resourceDocumentParameters());
65+
$context = $context->withParameters($this->getParameters());
6466

6567
if ($response = ($this->handler)($context->model, $context)) {
6668
return $this->applyResponseHooks($response, $context);
@@ -90,7 +92,7 @@ public function rootSchema(SchemaContext $context): array
9092
],
9193
...array_map(
9294
fn(Parameter $parameter) => $parameter->getSchema($context),
93-
$this->resourceDocumentParameters(),
95+
$this->getParameters(),
9496
),
9597
],
9698
'responses' => [
@@ -115,4 +117,9 @@ public function rootSchema(SchemaContext $context): array
115117
],
116118
];
117119
}
120+
121+
protected function getParameters(): array
122+
{
123+
return [...$this->resourceDocumentParameters(), ...$this->parameters];
124+
}
118125
}

src/Endpoint/Show.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ public function endpoints(): array
2929
return [$this->showResource, $this->showRelated, $this->showRelationship];
3030
}
3131

32+
public function showResource(Closure $callback): static
33+
{
34+
$callback($this->showResource);
35+
36+
return $this;
37+
}
38+
39+
public function showRelated(Closure $callback): static
40+
{
41+
$callback($this->showRelated);
42+
43+
return $this;
44+
}
45+
46+
public function showRelationship(Closure $callback): static
47+
{
48+
$callback($this->showRelationship);
49+
50+
return $this;
51+
}
52+
3253
public function visible(bool|Closure $condition = true): static
3354
{
3455
$this->showResource->visible($condition);
@@ -47,6 +68,13 @@ public function hidden(bool|Closure $condition = true): static
4768
return $this;
4869
}
4970

71+
public function parameters(array $parameters): static
72+
{
73+
$this->showResource->parameters($parameters);
74+
75+
return $this;
76+
}
77+
5078
public function response(Closure $callback): static
5179
{
5280
$this->showResource->response($callback);

src/Endpoint/ShowRelated.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Psr\Http\Message\ResponseInterface;
66
use Tobyz\JsonApiServer\Context;
7+
use Tobyz\JsonApiServer\Endpoint\Concerns\HasParameters;
78
use Tobyz\JsonApiServer\Endpoint\Concerns\HasResponse;
89
use Tobyz\JsonApiServer\Endpoint\Concerns\ResolvesList;
910
use Tobyz\JsonApiServer\Endpoint\Concerns\ResolvesModel;
@@ -21,6 +22,7 @@
2122

2223
class ShowRelated implements Endpoint, ProvidesRootSchema, ProvidesRelationshipLinks
2324
{
25+
use HasParameters;
2426
use HasResponse;
2527
use ResolvesModel;
2628
use ResolvesRelationship;
@@ -54,7 +56,7 @@ public function handle(Context $context): ?ResponseInterface
5456
return null;
5557
}
5658

57-
$context = $context->withParameters($this->parameters($field, $context));
59+
$context = $context->withParameters($this->getParameters($field, $context));
5860

5961
$relatedData = $this->resolveRelationshipData($context, $field);
6062

@@ -117,7 +119,7 @@ public function rootSchema(SchemaContext $context): array
117119
],
118120
...array_map(
119121
fn(Parameter $parameter) => $parameter->getSchema($context),
120-
$this->parameters($field, $context),
122+
$this->getParameters($field, $context),
121123
),
122124
],
123125
'responses' => [
@@ -141,7 +143,7 @@ public function rootSchema(SchemaContext $context): array
141143
return ['paths' => $paths];
142144
}
143145

144-
protected function parameters(Relationship $field, SchemaContext $context): array
146+
protected function getParameters(Relationship $field, SchemaContext $context): array
145147
{
146148
$parameters = $this->resourceDocumentParameters();
147149

@@ -152,7 +154,7 @@ protected function parameters(Relationship $field, SchemaContext $context): arra
152154
);
153155
}
154156

155-
return $parameters;
157+
return [...$parameters, ...$this->parameters];
156158
}
157159

158160
public function relationshipLinks(Relationship $field, SchemaContext $context): array

src/Endpoint/ShowRelationship.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Psr\Http\Message\ResponseInterface;
66
use Tobyz\JsonApiServer\Context;
7+
use Tobyz\JsonApiServer\Endpoint\Concerns\HasParameters;
78
use Tobyz\JsonApiServer\Endpoint\Concerns\HasResponse;
89
use Tobyz\JsonApiServer\Endpoint\Concerns\ResolvesList;
910
use Tobyz\JsonApiServer\Endpoint\Concerns\ResolvesModel;
@@ -20,6 +21,7 @@
2021

2122
class ShowRelationship implements Endpoint, ProvidesRootSchema, ProvidesRelationshipLinks
2223
{
24+
use HasParameters;
2325
use HasResponse;
2426
use ResolvesModel;
2527
use ResolvesRelationship;
@@ -53,7 +55,7 @@ public function handle(Context $context): ?ResponseInterface
5355
return null;
5456
}
5557

56-
$context = $context->withParameters($this->parameters($field, $context));
58+
$context = $context->withParameters($this->getParameters($field, $context));
5759

5860
$relatedData = $this->resolveRelationshipData($context, $field);
5961

@@ -99,7 +101,7 @@ public function rootSchema(SchemaContext $context): array
99101
],
100102
...array_map(
101103
fn(Parameter $parameter) => $parameter->getSchema($context),
102-
$this->parameters($field, $context),
104+
$this->getParameters($field, $context),
103105
),
104106
],
105107
'responses' => [
@@ -120,7 +122,7 @@ public function rootSchema(SchemaContext $context): array
120122
return ['paths' => $paths];
121123
}
122124

123-
protected function parameters(Relationship $field, SchemaContext $context): array
125+
protected function getParameters(Relationship $field, SchemaContext $context): array
124126
{
125127
$parameters = [];
126128

@@ -131,7 +133,7 @@ protected function parameters(Relationship $field, SchemaContext $context): arra
131133
);
132134
}
133135

134-
return $parameters;
136+
return [...$parameters, ...$this->parameters];
135137
}
136138

137139
public function relationshipLinks(Relationship $field, SchemaContext $context): array

src/Endpoint/ShowResource.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Psr\Http\Message\ResponseInterface;
66
use RuntimeException;
77
use Tobyz\JsonApiServer\Context;
8+
use Tobyz\JsonApiServer\Endpoint\Concerns\HasParameters;
89
use Tobyz\JsonApiServer\Endpoint\Concerns\HasResponse;
910
use Tobyz\JsonApiServer\Endpoint\Concerns\ResolvesModel;
1011
use Tobyz\JsonApiServer\Endpoint\Concerns\SerializesResourceDocument;
@@ -17,6 +18,7 @@
1718

1819
class ShowResource implements Endpoint, ProvidesRootSchema, ProvidesResourceLinks
1920
{
21+
use HasParameters;
2022
use HasResponse;
2123
use HasSchema;
2224
use ResolvesModel;
@@ -41,7 +43,7 @@ public function handle(Context $context): ?ResponseInterface
4143

4244
$context = $this->resolveModel($context, $segments[0]);
4345

44-
$context = $context->withParameters($this->resourceDocumentParameters());
46+
$context = $context->withParameters($this->getParameters());
4547

4648
return $this->createResponse(
4749
$this->serializeResourceDocument($context->model, $context),
@@ -67,7 +69,7 @@ public function rootSchema(SchemaContext $context): array
6769
],
6870
...array_map(
6971
fn(Parameter $parameter) => $parameter->getSchema($context),
70-
$this->resourceDocumentParameters(),
72+
$this->getParameters(),
7173
),
7274
],
7375
'responses' => [
@@ -135,4 +137,9 @@ public function resourceLinks(SchemaContext $context): array
135137
),
136138
];
137139
}
140+
141+
protected function getParameters(): array
142+
{
143+
return [...$this->resourceDocumentParameters(), ...$this->parameters];
144+
}
138145
}

0 commit comments

Comments
 (0)