Skip to content

Commit 68c6809

Browse files
committed
[Refactor] Move helper methods to generic form request class
1 parent 4aa5947 commit 68c6809

File tree

5 files changed

+160
-124
lines changed

5 files changed

+160
-124
lines changed

Diff for: src/Http/Requests/FormRequest.php

+124-22
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,126 @@ public function isJsonApi(): bool
6565
return $this->matchesType(self::JSON_API_MEDIA_TYPE, $this->header('CONTENT_TYPE'));
6666
}
6767

68+
/**
69+
* Is this a request to view any resource? (Index action.)
70+
*
71+
* @return bool
72+
*/
73+
public function isViewingAny(): bool
74+
{
75+
return $this->isMethod('GET') && $this->doesntHaveResourceId() && $this->isNotRelationship();
76+
}
77+
78+
/**
79+
* Is this a request to view a specific resource? (Read action.)
80+
*
81+
* @return bool
82+
*/
83+
public function isViewingOne(): bool
84+
{
85+
return $this->isMethod('GET') && $this->hasResourceId() && $this->isNotRelationship();
86+
}
87+
88+
/**
89+
* Is this a request to view resources in a relationship (Read related/relationship actions.)
90+
*
91+
* @return bool
92+
*/
93+
public function isViewingRelationship(): bool
94+
{
95+
return $this->isMethod('GET') && $this->isRelationship();
96+
}
97+
98+
/**
99+
* Is this a request to create a resource?
100+
*
101+
* @return bool
102+
*/
103+
public function isCreating(): bool
104+
{
105+
return $this->isMethod('POST') && $this->isNotRelationship();
106+
}
107+
108+
/**
109+
* Is this a request to update a resource?
110+
*
111+
* @return bool
112+
*/
113+
public function isUpdating(): bool
114+
{
115+
return $this->isMethod('PATCH') && $this->isNotRelationship();
116+
}
117+
118+
/**
119+
* Is this a request to replace a resource relationship?
120+
*
121+
* @return bool
122+
*/
123+
public function isUpdatingRelationship(): bool
124+
{
125+
return $this->isMethod('PATCH') && $this->isRelationship();
126+
}
127+
128+
/**
129+
* Is this a request to attach records to a resource relationship?
130+
*
131+
* @return bool
132+
*/
133+
public function isAttachingRelationship(): bool
134+
{
135+
return $this->isMethod('POST') && $this->isRelationship();
136+
}
137+
138+
/**
139+
* Is this a request to detach records from a resource relationship?
140+
*
141+
* @return bool
142+
*/
143+
public function isDetachingRelationship(): bool
144+
{
145+
return $this->isMethod('DELETE') && $this->isRelationship();
146+
}
147+
148+
/**
149+
* Is this a request to modify a resource relationship?
150+
*
151+
* @return bool
152+
*/
153+
public function isModifyingRelationship(): bool
154+
{
155+
return $this->isUpdatingRelationship() ||
156+
$this->isAttachingRelationship() ||
157+
$this->isDetachingRelationship();
158+
}
159+
160+
/**
161+
* @return bool
162+
*/
163+
public function isDeleting(): bool
164+
{
165+
return $this->isMethod('DELETE') && $this->isNotRelationship();
166+
}
167+
168+
/**
169+
* Is this a request to view or modify a relationship?
170+
*
171+
* @return bool
172+
*/
173+
public function isRelationship(): bool
174+
{
175+
return $this->jsonApi()->route()->hasRelation();
176+
}
177+
178+
/**
179+
* Is this a request to not view a relationship?
180+
*
181+
* @return bool
182+
*/
183+
public function isNotRelationship(): bool
184+
{
185+
return !$this->isRelationship();
186+
}
187+
68188
/**
69189
* Get the field name for a relationship request.
70190
*
@@ -183,11 +303,11 @@ final protected function jsonApi(): JsonApiService
183303
}
184304

185305
/**
186-
* Is the request for a specific resource?
306+
* Is there a resource id?
187307
*
188308
* @return bool
189309
*/
190-
final protected function isResource(): bool
310+
private function hasResourceId(): bool
191311
{
192312
return $this->jsonApi()->route()->hasResourceId();
193313
}
@@ -197,26 +317,8 @@ final protected function isResource(): bool
197317
*
198318
* @return bool
199319
*/
200-
final protected function isNotResource(): bool
320+
private function doesntHaveResourceId(): bool
201321
{
202-
return !$this->isResource();
203-
}
204-
205-
/**
206-
* Is this a request to modify a relationship?
207-
*
208-
* @return bool
209-
*/
210-
final protected function isRelationship(): bool
211-
{
212-
return $this->jsonApi()->route()->hasRelation();
213-
}
214-
215-
/**
216-
* @return bool
217-
*/
218-
final protected function isNotRelationship(): bool
219-
{
220-
return !$this->isRelationship();
322+
return !$this->hasResourceId();
221323
}
222324
}

Diff for: src/Http/Requests/ResourceQuery.php

-30
Original file line numberDiff line numberDiff line change
@@ -99,36 +99,6 @@ public static function queryOne(string $resourceType): QueryParameters
9999
return $resolver($resourceType);
100100
}
101101

102-
/**
103-
* Is this a request to view any resource? (Index action.)
104-
*
105-
* @return bool
106-
*/
107-
public function isViewingAny(): bool
108-
{
109-
return $this->isMethod('GET') && $this->isNotResource() && $this->isNotRelationship();
110-
}
111-
112-
/**
113-
* Is this a request to view a specific resource? (Read action.)
114-
*
115-
* @return bool
116-
*/
117-
public function isViewingOne(): bool
118-
{
119-
return $this->isMethod('GET') && $this->isResource() && $this->isNotRelationship();
120-
}
121-
122-
/**
123-
* Is this a request to view resources in a relationship (Read related/relationship actions.)
124-
*
125-
* @return bool
126-
*/
127-
public function isViewingRelationship(): bool
128-
{
129-
return $this->isMethod('GET') && $this->isRelationship();
130-
}
131-
132102
/**
133103
* Perform resource authorization.
134104
*

Diff for: src/Http/Requests/ResourceRequest.php

-70
Original file line numberDiff line numberDiff line change
@@ -80,76 +80,6 @@ public static function forResource(string $resourceType): ResourceRequest
8080
return $resolver($resourceType);
8181
}
8282

83-
/**
84-
* Is this a request to create a resource?
85-
*
86-
* @return bool
87-
*/
88-
public function isCreating(): bool
89-
{
90-
return $this->isMethod('POST') && $this->isNotRelationship();
91-
}
92-
93-
/**
94-
* Is this a request to update a resource?
95-
*
96-
* @return bool
97-
*/
98-
public function isUpdating(): bool
99-
{
100-
return $this->isMethod('PATCH') && $this->isNotRelationship();
101-
}
102-
103-
/**
104-
* Is this a request to replace a resource relationship?
105-
*
106-
* @return bool
107-
*/
108-
public function isUpdatingRelationship(): bool
109-
{
110-
return $this->isMethod('PATCH') && $this->isRelationship();
111-
}
112-
113-
/**
114-
* Is this a request to attach records to a resource relationship?
115-
*
116-
* @return bool
117-
*/
118-
public function isAttachingRelationship(): bool
119-
{
120-
return $this->isMethod('POST') && $this->isRelationship();
121-
}
122-
123-
/**
124-
* Is this a request to detach records from a resource relationship?
125-
*
126-
* @return bool
127-
*/
128-
public function isDetachingRelationship(): bool
129-
{
130-
return $this->isMethod('DELETE') && $this->isRelationship();
131-
}
132-
133-
/**
134-
* Is this a request to modify a resource relationship?
135-
*
136-
* @return bool
137-
*/
138-
public function isModifyingRelationship(): bool
139-
{
140-
return $this->isUpdatingRelationship() ||
141-
$this->isAttachingRelationship() ||
142-
$this->isDetachingRelationship();
143-
}
144-
145-
/**
146-
* @return bool
147-
*/
148-
public function isDeleting(): bool
149-
{
150-
return $this->isMethod('DELETE') && $this->isNotRelationship();
151-
}
152-
15383
/**
15484
* Perform resource authorization.
15585
*

Diff for: tests/dummy/app/JsonApi/V1/Posts/PostCollectionQuery.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
<?php
2+
/*
3+
* Copyright 2020 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
219

320
namespace App\JsonApi\V1\Posts;
421

@@ -13,7 +30,7 @@ class PostCollectionQuery extends ResourceQuery
1330
*
1431
* @return array
1532
*/
16-
public function rules()
33+
public function rules(): array
1734
{
1835
return [
1936
'fields' => [

Diff for: tests/dummy/app/JsonApi/V1/Posts/PostQuery.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
<?php
2+
/*
3+
* Copyright 2020 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
219

320
namespace App\JsonApi\V1\Posts;
421

@@ -13,7 +30,7 @@ class PostQuery extends ResourceQuery
1330
*
1431
* @return array
1532
*/
16-
public function rules()
33+
public function rules(): array
1734
{
1835
return [
1936
'fields' => [

0 commit comments

Comments
 (0)