1 | <?php |
||||
2 | |||||
3 | namespace VGirol\JsonApi\Controllers; |
||||
4 | |||||
5 | use Illuminate\Http\JsonResponse; |
||||
6 | use Illuminate\Http\Request; |
||||
7 | use Illuminate\Support\Facades\DB; |
||||
8 | use VGirol\JsonApi\Exceptions\JsonApi403Exception; |
||||
9 | use VGirol\JsonApi\Messages\Messages; |
||||
10 | use VGirol\JsonApiConstant\Members; |
||||
11 | |||||
12 | trait ManageRelationships |
||||
13 | { |
||||
14 | /** |
||||
15 | * Display a listing of the relationship. |
||||
16 | * |
||||
17 | * @param Request $request |
||||
18 | * @param mixed $parentId |
||||
19 | * @param string $relationship |
||||
20 | * |
||||
21 | * @return JsonResponse |
||||
22 | */ |
||||
23 | public function relationshipIndex(Request $request, $parentId, string $relationship): JsonResponse |
||||
24 | { |
||||
25 | // Fetch related model or collection |
||||
26 | $related = $this->fetchService->getRelated($request, $parentId, $relationship); |
||||
27 | |||||
28 | // Creates resource |
||||
29 | $resource = $this->exportService->exportAsResourceIdentifier($related, $request); |
||||
30 | |||||
31 | return $this->responseService->ok($resource); |
||||
32 | } |
||||
33 | |||||
34 | /** |
||||
35 | * Add one or more items to a relationships. |
||||
36 | * |
||||
37 | * @param Request $request |
||||
38 | * @param int $parentId |
||||
39 | * @param string $relationship |
||||
40 | * |
||||
41 | * @return JsonResponse |
||||
42 | */ |
||||
43 | // public function relationshipStore(Request $request, $parentId, string $relationship): JsonResponse |
||||
44 | // { |
||||
45 | // // Save relationship |
||||
46 | // $resource = DB::transaction(function () use ($request, $parentId, $relationship) { |
||||
47 | // // Retrieve relationship |
||||
48 | // $relation = $this->getRelationFromRequest($request, $parentId, $relationship); |
||||
49 | |||||
50 | // // Checks that the relation is of type to-many |
||||
51 | // if (!$relation->isToMany()) { |
||||
52 | // throw new JsonApi403Exception( |
||||
53 | // sprintf(Messages::METHOD_NOT_ALLOWED_FOR_RELATIONSHIP, $request->method()) |
||||
54 | // ); |
||||
55 | // } |
||||
56 | |||||
57 | // // Validate data |
||||
58 | // $this->validateResourceLinkages($request); |
||||
59 | |||||
60 | // // Gets the data |
||||
61 | // $data = $request->input(Members::DATA); |
||||
62 | |||||
63 | // // Get related instances |
||||
64 | // $related = $this->getRelatedFromRequestData($data, $relationship); |
||||
65 | |||||
66 | // // Attach related resources to parent |
||||
67 | // $this->addToRelationship($parent, $relationship, $related, false); |
||||
68 | |||||
69 | // // Creates resource |
||||
70 | // $resource = $this->fetchService->getRelated($request, $parentId, $relationship, true); |
||||
71 | |||||
72 | // return $resource; |
||||
73 | // }, config('jsonapi.transactionAttempts')); |
||||
74 | |||||
75 | // // Fills response's content |
||||
76 | // $response = $this->responseService->created($resource); |
||||
77 | |||||
78 | // return $response; |
||||
79 | // } |
||||
80 | |||||
81 | /** |
||||
82 | * Updates all items of a relationship. |
||||
83 | * |
||||
84 | * @param Request $request |
||||
85 | * @param int $parentId |
||||
86 | * @param string $relationship |
||||
87 | * |
||||
88 | * @return JsonResponse |
||||
89 | */ |
||||
90 | public function relationshipUpdate(Request $request, $parentId, string $relationship): JsonResponse |
||||
91 | { |
||||
92 | // Save relationship |
||||
93 | $resource = DB::transaction(function () use ($request, $parentId, $relationship) { |
||||
94 | // Retrieve relationship |
||||
95 | $relation = $this->fetchService->getRelationFromRequest($request, $parentId, $relationship); |
||||
96 | |||||
97 | if ( |
||||
98 | $relation->isToMany() |
||||
99 | && (config('jsonapi.relationshipFullReplacementIsAllowed') === false) |
||||
100 | ) { |
||||
101 | throw new JsonApi403Exception(Messages::RELATIONSHIP_FULL_REPLACEMENT); |
||||
102 | } |
||||
103 | |||||
104 | // Validate data |
||||
105 | $this->validateResourceLinkages($request); |
||||
106 | |||||
107 | // Gets the data |
||||
108 | $data = $request->input(Members::DATA); |
||||
0 ignored issues
–
show
|
|||||
109 | if (($data === null) || ($data === [])) { |
||||
110 | $this->relationshipService->clear($relation); |
||||
111 | } else { |
||||
112 | $this->relationshipService->update($relation, $data); |
||||
113 | } |
||||
114 | |||||
115 | // Fetch required relation |
||||
116 | $requiredRelated = $this->fetchService->getRelated($request, $parentId, $relationship, true); |
||||
117 | |||||
118 | // Creates resource |
||||
119 | $resource = $this->exportService->exportAsResourceIdentifier($requiredRelated, $request); |
||||
120 | |||||
121 | return $resource; |
||||
122 | }, config('jsonapi.transactionAttempts')); |
||||
123 | |||||
124 | // Fills response's content |
||||
125 | $response = $this->responseService->ok($resource); |
||||
126 | |||||
127 | return $response; |
||||
128 | } |
||||
129 | |||||
130 | /** |
||||
131 | * Deletes some items of a relationship. |
||||
132 | * |
||||
133 | * @param Request $request |
||||
134 | * @param int $parentId |
||||
135 | * @param string $relationship |
||||
136 | * |
||||
137 | * @return JsonResponse |
||||
138 | */ |
||||
139 | public function relationshipDestroy(Request $request, $parentId, string $relationship): JsonResponse |
||||
140 | { |
||||
141 | // Save relationship |
||||
142 | $resource = DB::transaction(function () use ($request, $parentId, $relationship) { |
||||
143 | // Retrieve relationship |
||||
144 | $relation = $this->fetchService->getRelationFromRequest($request, $parentId, $relationship); |
||||
145 | |||||
146 | // Checks that the relation is of type to-many |
||||
147 | if (!$relation->isToMany()) { |
||||
148 | throw new JsonApi403Exception( |
||||
149 | sprintf(Messages::METHOD_NOT_ALLOWED_FOR_RELATIONSHIP, $request->method()) |
||||
0 ignored issues
–
show
The method
method() does not exist on Illuminate\Http\Request .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
150 | ); |
||||
151 | } |
||||
152 | |||||
153 | // Validate data |
||||
154 | $this->validateResourceLinkages($request); |
||||
155 | |||||
156 | // Gets the data |
||||
157 | $data = $request->input(Members::DATA); |
||||
158 | |||||
159 | // Get related instances |
||||
160 | $related = $this->fetchService->getRelatedFromRequestData($data); |
||||
161 | |||||
162 | // Attach related resources to parent |
||||
163 | $this->relationshipService->remove($relation, $related); |
||||
164 | |||||
165 | // Creates resource |
||||
166 | $resource = $this->fetchService->getRelated($request, $parentId, $relationship, true); |
||||
167 | |||||
168 | return $resource; |
||||
169 | }, config('jsonapi.transactionAttempts')); |
||||
170 | |||||
171 | // Fills response's content |
||||
172 | $response = $this->responseService->ok($resource); |
||||
173 | |||||
174 | return $response; |
||||
175 | } |
||||
176 | |||||
177 | /** |
||||
178 | * Undocumented function |
||||
179 | * |
||||
180 | * @param Request $request |
||||
181 | * |
||||
182 | * @return void |
||||
183 | */ |
||||
184 | private function validateResourceLinkages($request) |
||||
185 | { |
||||
186 | // Gets the data |
||||
187 | $data = $request->input(Members::DATA, []); |
||||
0 ignored issues
–
show
|
|||||
188 | } |
||||
189 | } |
||||
190 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.