1 | <?php |
||||
2 | |||||
3 | namespace VGirol\JsonApi\Resources; |
||||
4 | |||||
5 | use Illuminate\Http\Request; |
||||
6 | use VGirol\JsonApiConstant\Members; |
||||
7 | |||||
8 | class ResourceObject extends BaseResource |
||||
9 | { |
||||
10 | use IsData; |
||||
11 | use IsIncluded; |
||||
12 | use HasIdentification; |
||||
13 | use HasRelationships; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
14 | |||||
15 | public const DELETED_MESSAGE = 'Object (#%s) successfully deleted.'; |
||||
16 | |||||
17 | /** |
||||
18 | * Undocumented variable |
||||
19 | * |
||||
20 | * @var \Illuminate\Support\Collection |
||||
21 | */ |
||||
22 | protected $includes; |
||||
23 | |||||
24 | public function __construct($resource, $includes = null) |
||||
25 | { |
||||
26 | parent::__construct($resource); |
||||
27 | |||||
28 | $this->wrap(Members::DATA); |
||||
29 | $this->initIncludes($includes); |
||||
30 | } |
||||
31 | |||||
32 | /** |
||||
33 | * Undocumented function |
||||
34 | * |
||||
35 | * @param \Illuminate\Support\Collection $includes |
||||
36 | * |
||||
37 | * @return void |
||||
38 | */ |
||||
39 | public function initIncludes($includes) |
||||
40 | { |
||||
41 | if (!is_null($includes)) { |
||||
42 | $this->includes = $includes; |
||||
43 | return; |
||||
44 | } |
||||
45 | |||||
46 | $service = jsonapiInclude(); |
||||
47 | $this->includes = $service->parameters(); |
||||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * Transform the resource into an array. |
||||
52 | * |
||||
53 | * @param Request $request |
||||
54 | * |
||||
55 | * @return array |
||||
56 | */ |
||||
57 | public function toArray($request): ?array |
||||
58 | { |
||||
59 | if (is_null($this->resource)) { |
||||
60 | if ($this->isResolving) { |
||||
61 | return [static::$wrap => null]; |
||||
62 | } else { |
||||
63 | return null; |
||||
64 | } |
||||
65 | } |
||||
66 | |||||
67 | // Set identification (i.e., resource type and id) |
||||
68 | $this->setIdentification($request); |
||||
69 | |||||
70 | // Add attributes |
||||
71 | $this->setAttributes($request); |
||||
72 | |||||
73 | // Add relationships |
||||
74 | $this->setRelationships($request); |
||||
75 | |||||
76 | // Add links |
||||
77 | $this->setLinks($request); |
||||
78 | |||||
79 | // Add meta |
||||
80 | $this->setMeta($request); |
||||
81 | |||||
82 | return $this->resultingArray; |
||||
83 | } |
||||
84 | |||||
85 | /** |
||||
86 | * Get any additional data that should be returned with the resource array. |
||||
87 | * |
||||
88 | * @param Request $request |
||||
89 | * |
||||
90 | * @return array |
||||
91 | */ |
||||
92 | public function with($request) |
||||
93 | { |
||||
94 | if (!empty($this->with)) { |
||||
95 | return $this->with; |
||||
96 | } |
||||
97 | |||||
98 | // Add document meta |
||||
99 | $this->setDocumentMeta($request); |
||||
100 | |||||
101 | if ($this->isUpdate($request) && !$this->haveChanged()) { |
||||
102 | return $this->with; |
||||
103 | } |
||||
104 | |||||
105 | // Top-level links member |
||||
106 | if (!$this->isCreation($request)) { |
||||
107 | $this->addDocumentLink(Members::LINK_SELF, $this->getDocumentSelfLink($request)); |
||||
0 ignored issues
–
show
$request of type Illuminate\Http\Request is incompatible with the type Illuminate\Support\Facades\Request expected by parameter $request of VGirol\JsonApi\Resources...::getDocumentSelfLink() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
108 | } |
||||
109 | |||||
110 | // Add document included member |
||||
111 | $this->setIncluded($request); |
||||
112 | |||||
113 | return $this->with; |
||||
114 | } |
||||
115 | |||||
116 | /** |
||||
117 | * Undocumented function |
||||
118 | * |
||||
119 | * @param Request $request |
||||
120 | * |
||||
121 | * @return static|array|null |
||||
122 | */ |
||||
123 | public function getContent($request) |
||||
124 | { |
||||
125 | if ($this->isUpdate($request) && !$this->haveChanged()) { |
||||
126 | return $this->hasDocumentMeta($request) ? $this->getDocumentMeta($request) : null; |
||||
127 | } |
||||
128 | |||||
129 | return $this; |
||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * Check if the resource have had automatic changes. |
||||
134 | * |
||||
135 | * @return bool |
||||
136 | */ |
||||
137 | public function haveChanged(): bool |
||||
138 | { |
||||
139 | return ($this->resource != null |
||||
140 | && $this->resource->automaticChanges); |
||||
141 | } |
||||
142 | |||||
143 | /** |
||||
144 | * Could be overloaded |
||||
145 | * |
||||
146 | * @param Request $request |
||||
147 | * |
||||
148 | * @return void |
||||
149 | */ |
||||
150 | protected function setAttributes($request) |
||||
0 ignored issues
–
show
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
151 | { |
||||
152 | $this->setResourceAttributes($this->resource->attributesToArray()); |
||||
153 | } |
||||
154 | |||||
155 | /** |
||||
156 | * Could be overloaded |
||||
157 | * |
||||
158 | * @param Request $request |
||||
159 | * |
||||
160 | * @return void |
||||
161 | */ |
||||
162 | protected function setMeta($request) |
||||
0 ignored issues
–
show
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
163 | { |
||||
164 | // $this->addResourceMeta('key', 'value'); |
||||
165 | } |
||||
166 | |||||
167 | /** |
||||
168 | * Could be overloaded |
||||
169 | * |
||||
170 | * @param Request $request |
||||
171 | * |
||||
172 | * @return void |
||||
173 | */ |
||||
174 | protected function setLinks($request) |
||||
0 ignored issues
–
show
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
175 | { |
||||
176 | $this->addResourceLink(Members::LINK_SELF, $this->getResourceSelfLink(get_class($this->resource))); |
||||
177 | } |
||||
178 | |||||
179 | /** |
||||
180 | * Could be overloaded |
||||
181 | * |
||||
182 | * @param Request $request |
||||
183 | * |
||||
184 | * @return void |
||||
185 | */ |
||||
186 | protected function setDocumentMeta($request) |
||||
187 | { |
||||
188 | // $this->addDocumentMeta('key', 'value'); |
||||
189 | } |
||||
190 | |||||
191 | /** |
||||
192 | * Undocumented function |
||||
193 | * |
||||
194 | * @param Request $request |
||||
195 | * |
||||
196 | * @return boolean |
||||
197 | */ |
||||
198 | protected function isCreation($request): bool |
||||
199 | { |
||||
200 | return ($request->method() == 'POST'); |
||||
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. ![]() |
|||||
201 | } |
||||
202 | |||||
203 | /** |
||||
204 | * Undocumented function |
||||
205 | * |
||||
206 | * @param Request $request |
||||
207 | * |
||||
208 | * @return boolean |
||||
209 | */ |
||||
210 | protected function isUpdate($request): bool |
||||
211 | { |
||||
212 | return in_array($request->method(), ['PATCH', 'PUT']); |
||||
213 | } |
||||
214 | } |
||||
215 |