@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @var array |
41 | 41 | */ |
42 | - protected $meta = []; |
|
42 | + protected $meta = [ ]; |
|
43 | 43 | |
44 | 44 | /** |
45 | 45 | * The Fractal resource instance containing the data and transformer. |
@@ -69,13 +69,13 @@ discard block |
||
69 | 69 | * @param \Flugg\Responder\ResourceFactory $resourceFactory |
70 | 70 | * @param \League\Fractal\Manager $manager |
71 | 71 | */ |
72 | - public function __construct(ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager) |
|
72 | + public function __construct( ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager ) |
|
73 | 73 | { |
74 | 74 | $this->resourceFactory = $resourceFactory; |
75 | 75 | $this->manager = $manager; |
76 | 76 | $this->resource = $this->resourceFactory->make(); |
77 | 77 | |
78 | - parent::__construct($responseFactory); |
|
78 | + parent::__construct( $responseFactory ); |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | /** |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | * @param array $data |
85 | 85 | * @return self |
86 | 86 | */ |
87 | - public function addMeta(array $data):SuccessResponseBuilder |
|
87 | + public function addMeta( array $data ):SuccessResponseBuilder |
|
88 | 88 | { |
89 | - $this->meta = array_merge($this->meta, $data); |
|
89 | + $this->meta = array_merge( $this->meta, $data ); |
|
90 | 90 | |
91 | 91 | return $this; |
92 | 92 | } |
@@ -97,9 +97,9 @@ discard block |
||
97 | 97 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
98 | 98 | * @return self |
99 | 99 | */ |
100 | - public function serializer($serializer):SuccessResponseBuilder |
|
100 | + public function serializer( $serializer ):SuccessResponseBuilder |
|
101 | 101 | { |
102 | - $this->manager->setSerializer($this->resolveSerializer($serializer)); |
|
102 | + $this->manager->setSerializer( $this->resolveSerializer( $serializer ) ); |
|
103 | 103 | |
104 | 104 | return $this; |
105 | 105 | } |
@@ -111,13 +111,13 @@ discard block |
||
111 | 111 | * @return self |
112 | 112 | * @throws \InvalidArgumentException |
113 | 113 | */ |
114 | - public function setStatus(int $statusCode):ResponseBuilder |
|
114 | + public function setStatus( int $statusCode ):ResponseBuilder |
|
115 | 115 | { |
116 | - if ($statusCode < 100 || $statusCode >= 400) { |
|
117 | - throw new InvalidArgumentException("{$statusCode} is not a valid success HTTP status code."); |
|
116 | + if ( $statusCode < 100 || $statusCode >= 400 ) { |
|
117 | + throw new InvalidArgumentException( "{$statusCode} is not a valid success HTTP status code." ); |
|
118 | 118 | } |
119 | 119 | |
120 | - return parent::setStatus($statusCode); |
|
120 | + return parent::setStatus( $statusCode ); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
@@ -129,17 +129,17 @@ discard block |
||
129 | 129 | * @param string|null $resourceKey |
130 | 130 | * @return self |
131 | 131 | */ |
132 | - public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder |
|
132 | + public function transform( $data = null, $transformer = null, string $resourceKey = null ):SuccessResponseBuilder |
|
133 | 133 | { |
134 | - $resource = $this->resourceFactory->make($data); |
|
134 | + $resource = $this->resourceFactory->make( $data ); |
|
135 | 135 | |
136 | - if (! is_null($resource->getData())) { |
|
137 | - $model = $this->resolveModel($resource->getData()); |
|
138 | - $transformer = $this->resolveTransformer($model, $transformer); |
|
139 | - $resourceKey = $this->resolveResourceKey($model, $resourceKey); |
|
136 | + if ( ! is_null( $resource->getData() ) ) { |
|
137 | + $model = $this->resolveModel( $resource->getData() ); |
|
138 | + $transformer = $this->resolveTransformer( $model, $transformer ); |
|
139 | + $resourceKey = $this->resolveResourceKey( $model, $resourceKey ); |
|
140 | 140 | } |
141 | 141 | |
142 | - $this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey); |
|
142 | + $this->resource = $resource->setTransformer( $transformer )->setResourceKey( $resourceKey ); |
|
143 | 143 | |
144 | 144 | return $this; |
145 | 145 | } |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | */ |
152 | 152 | public function toArray():array |
153 | 153 | { |
154 | - return $this->serialize($this->getResource()); |
|
154 | + return $this->serialize( $this->getResource() ); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | */ |
162 | 162 | public function getResource():ResourceInterface |
163 | 163 | { |
164 | - return $this->resource->setMeta($this->meta); |
|
164 | + return $this->resource->setMeta( $this->meta ); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
@@ -181,13 +181,13 @@ discard block |
||
181 | 181 | * @return \League\Fractal\Serializer\SerializerAbstract |
182 | 182 | * @throws \Flugg\Responder\Exceptions\InvalidSerializerException |
183 | 183 | */ |
184 | - protected function resolveSerializer($serializer):SerializerAbstract |
|
184 | + protected function resolveSerializer( $serializer ):SerializerAbstract |
|
185 | 185 | { |
186 | - if (is_string($serializer)) { |
|
186 | + if ( is_string( $serializer ) ) { |
|
187 | 187 | $serializer = new $serializer; |
188 | 188 | } |
189 | 189 | |
190 | - if (! $serializer instanceof SerializerAbstract) { |
|
190 | + if ( ! $serializer instanceof SerializerAbstract ) { |
|
191 | 191 | throw new InvalidSerializerException(); |
192 | 192 | } |
193 | 193 | |
@@ -201,15 +201,15 @@ discard block |
||
201 | 201 | * @return \Illuminate\Database\Eloquent\Model |
202 | 202 | * @throws \InvalidArgumentException |
203 | 203 | */ |
204 | - protected function resolveModel($data):Model |
|
204 | + protected function resolveModel( $data ):Model |
|
205 | 205 | { |
206 | - if ($data instanceof Model) { |
|
206 | + if ( $data instanceof Model ) { |
|
207 | 207 | return $data; |
208 | 208 | } |
209 | 209 | |
210 | - $model = $data[0]; |
|
211 | - if (! $model instanceof Model) { |
|
212 | - throw new InvalidArgumentException('You can only transform data containing Eloquent models.'); |
|
210 | + $model = $data[ 0 ]; |
|
211 | + if ( ! $model instanceof Model ) { |
|
212 | + throw new InvalidArgumentException( 'You can only transform data containing Eloquent models.' ); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | return $model; |
@@ -222,15 +222,15 @@ discard block |
||
222 | 222 | * @param \Flugg\Responder\Transformer|callable|null $transformer |
223 | 223 | * @return \Flugg\Responder\Transformer|callable |
224 | 224 | */ |
225 | - protected function resolveTransformer(Model $model, $transformer = null) |
|
225 | + protected function resolveTransformer( Model $model, $transformer = null ) |
|
226 | 226 | { |
227 | - $transformer = $transformer ?: $this->resolveTransformerFromModel($model); |
|
227 | + $transformer = $transformer ?: $this->resolveTransformerFromModel( $model ); |
|
228 | 228 | |
229 | - if (is_string($transformer)) { |
|
229 | + if ( is_string( $transformer ) ) { |
|
230 | 230 | $transformer = new $transformer; |
231 | 231 | } |
232 | 232 | |
233 | - return $this->parseTransformer($transformer); |
|
233 | + return $this->parseTransformer( $transformer ); |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
@@ -240,9 +240,9 @@ discard block |
||
240 | 240 | * @param \Illuminate\Database\ELoquent\Model $model |
241 | 241 | * @return \Flugg\Responder\Transformer|callable |
242 | 242 | */ |
243 | - protected function resolveTransformerFromModel(Model $model) |
|
243 | + protected function resolveTransformerFromModel( Model $model ) |
|
244 | 244 | { |
245 | - if (! $model instanceof Transformable) { |
|
245 | + if ( ! $model instanceof Transformable ) { |
|
246 | 246 | return function () use ($model) { |
247 | 247 | return $model->toArray(); |
248 | 248 | }; |
@@ -258,14 +258,14 @@ discard block |
||
258 | 258 | * @return \Flugg\Responder\Transformer|callable |
259 | 259 | * @throws \InvalidTransformerException |
260 | 260 | */ |
261 | - protected function parseTransformer($transformer) |
|
261 | + protected function parseTransformer( $transformer ) |
|
262 | 262 | { |
263 | - if ($transformer instanceof Transformer) { |
|
264 | - $transformer = $transformer->setRelations($this->resolveRelations($model)); |
|
265 | - $this->manager->parseIncludes($transformer->getRelations()); |
|
263 | + if ( $transformer instanceof Transformer ) { |
|
264 | + $transformer = $transformer->setRelations( $this->resolveRelations( $model ) ); |
|
265 | + $this->manager->parseIncludes( $transformer->getRelations() ); |
|
266 | 266 | |
267 | - if (! is_callable($transformer)) { |
|
268 | - throw new InvalidTransformerException($model); |
|
267 | + if ( ! is_callable( $transformer ) ) { |
|
268 | + throw new InvalidTransformerException( $model ); |
|
269 | 269 | } |
270 | 270 | } |
271 | 271 | |
@@ -278,9 +278,9 @@ discard block |
||
278 | 278 | * @param \Illuminate\Database\Eloquent\Model $model |
279 | 279 | * @return array |
280 | 280 | */ |
281 | - protected function resolveRelations(Model $model):array |
|
281 | + protected function resolveRelations( Model $model ):array |
|
282 | 282 | { |
283 | - return array_keys($model->getRelations()); |
|
283 | + return array_keys( $model->getRelations() ); |
|
284 | 284 | } |
285 | 285 | |
286 | 286 | /** |
@@ -290,13 +290,13 @@ discard block |
||
290 | 290 | * @param string|null $resourceKey |
291 | 291 | * @return string |
292 | 292 | */ |
293 | - protected function resolveResourceKey(Model $model, string $resourceKey = null):string |
|
293 | + protected function resolveResourceKey( Model $model, string $resourceKey = null ):string |
|
294 | 294 | { |
295 | - if (! is_null($resourceKey)) { |
|
295 | + if ( ! is_null( $resourceKey ) ) { |
|
296 | 296 | return $resourceKey; |
297 | 297 | } |
298 | 298 | |
299 | - if (method_exists($model, 'getResourceKey')) { |
|
299 | + if ( method_exists( $model, 'getResourceKey' ) ) { |
|
300 | 300 | return $model->getResourceKey(); |
301 | 301 | } |
302 | 302 | |
@@ -309,8 +309,8 @@ discard block |
||
309 | 309 | * @param ResourceInterface $resource |
310 | 310 | * @return array |
311 | 311 | */ |
312 | - protected function serialize(ResourceInterface $resource):array |
|
312 | + protected function serialize( ResourceInterface $resource ):array |
|
313 | 313 | { |
314 | - return $this->manager->createData($resource)->toArray(); |
|
314 | + return $this->manager->createData( $resource )->toArray(); |
|
315 | 315 | } |
316 | 316 | } |
317 | 317 | \ No newline at end of file |