@@ -26,14 +26,14 @@ discard block |
||
26 | 26 | * |
27 | 27 | * @var array |
28 | 28 | */ |
29 | - protected $messages = []; |
|
29 | + protected $messages = [ ]; |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * Construct the resolver class. |
33 | 33 | * |
34 | 34 | * @param \Illuminate\Contracts\Translation\Translator $translator |
35 | 35 | */ |
36 | - public function __construct(Translator $translator) |
|
36 | + public function __construct( Translator $translator ) |
|
37 | 37 | { |
38 | 38 | $this->translator = $translator; |
39 | 39 | } |
@@ -45,11 +45,11 @@ discard block |
||
45 | 45 | * @param string $message |
46 | 46 | * @return void |
47 | 47 | */ |
48 | - public function register(string $errorCode, string $message) |
|
48 | + public function register( string $errorCode, string $message ) |
|
49 | 49 | { |
50 | - $this->messages = array_merge($this->messages, is_array($errorCode) ? $errorCode : [ |
|
50 | + $this->messages = array_merge( $this->messages, is_array( $errorCode ) ? $errorCode : [ |
|
51 | 51 | $errorCode => $message, |
52 | - ]); |
|
52 | + ] ); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -58,14 +58,14 @@ discard block |
||
58 | 58 | * @param string $errorCode |
59 | 59 | * @return string|null |
60 | 60 | */ |
61 | - public function resolve(string $errorCode) |
|
61 | + public function resolve( string $errorCode ) |
|
62 | 62 | { |
63 | - if (key_exists($errorCode, $this->messages)) { |
|
64 | - return $this->messages[$errorCode]; |
|
63 | + if ( key_exists( $errorCode, $this->messages ) ) { |
|
64 | + return $this->messages[ $errorCode ]; |
|
65 | 65 | } |
66 | 66 | |
67 | - if ($this->translator->has($errorCode = "errors.$errorCode")) { |
|
68 | - return $this->translator->trans($errorCode); |
|
67 | + if ( $this->translator->has( $errorCode = "errors.$errorCode" ) ) { |
|
68 | + return $this->translator->trans( $errorCode ); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | return null; |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @param \League\Fractal\Manager $manager |
31 | 31 | */ |
32 | - public function __construct(Manager $manager) |
|
32 | + public function __construct( Manager $manager ) |
|
33 | 33 | { |
34 | 34 | $this->manager = $manager; |
35 | 35 | } |
@@ -42,15 +42,15 @@ discard block |
||
42 | 42 | * @param array $options |
43 | 43 | * @return array |
44 | 44 | */ |
45 | - public function make(ResourceInterface $resource, SerializerAbstract $serializer, array $options = []): array |
|
45 | + public function make( ResourceInterface $resource, SerializerAbstract $serializer, array $options = [ ] ): array |
|
46 | 46 | { |
47 | - $options = $this->parseOptions($options, $resource); |
|
47 | + $options = $this->parseOptions( $options, $resource ); |
|
48 | 48 | |
49 | - return $this->manager->setSerializer($serializer) |
|
50 | - ->parseIncludes($options['includes']) |
|
51 | - ->parseExcludes($options['excludes']) |
|
52 | - ->parseFieldsets($options['fieldsets']) |
|
53 | - ->createData($resource) |
|
49 | + return $this->manager->setSerializer( $serializer ) |
|
50 | + ->parseIncludes( $options[ 'includes' ] ) |
|
51 | + ->parseExcludes( $options[ 'excludes' ] ) |
|
52 | + ->parseFieldsets( $options[ 'fieldsets' ] ) |
|
53 | + ->createData( $resource ) |
|
54 | 54 | ->toArray(); |
55 | 55 | } |
56 | 56 | |
@@ -61,20 +61,20 @@ discard block |
||
61 | 61 | * @param \League\Fractal\Resource\ResourceInterface $resource |
62 | 62 | * @return array |
63 | 63 | */ |
64 | - protected function parseOptions(array $options, ResourceInterface $resource): array |
|
64 | + protected function parseOptions( array $options, ResourceInterface $resource ): array |
|
65 | 65 | { |
66 | - $options = array_merge([ |
|
67 | - 'includes' => [], |
|
68 | - 'excludes' => [], |
|
69 | - 'fieldsets' => [], |
|
70 | - ], $options); |
|
71 | - |
|
72 | - if (! empty($options['fieldsets'])) { |
|
73 | - if (is_null($resourceKey = $resource->getResourceKey())) { |
|
74 | - throw new LogicException('Filtering fields using sparse fieldsets require resource key to be set.'); |
|
66 | + $options = array_merge( [ |
|
67 | + 'includes' => [ ], |
|
68 | + 'excludes' => [ ], |
|
69 | + 'fieldsets' => [ ], |
|
70 | + ], $options ); |
|
71 | + |
|
72 | + if ( ! empty($options[ 'fieldsets' ]) ) { |
|
73 | + if ( is_null( $resourceKey = $resource->getResourceKey() ) ) { |
|
74 | + throw new LogicException( 'Filtering fields using sparse fieldsets require resource key to be set.' ); |
|
75 | 75 | } |
76 | 76 | |
77 | - $options['fieldsets'] = $this->parseFieldsets($options['fieldsets'], $resourceKey, $options['includes']); |
|
77 | + $options[ 'fieldsets' ] = $this->parseFieldsets( $options[ 'fieldsets' ], $resourceKey, $options[ 'includes' ] ); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | return $options; |
@@ -88,19 +88,19 @@ discard block |
||
88 | 88 | * @param array $includes |
89 | 89 | * @return array |
90 | 90 | */ |
91 | - protected function parseFieldsets(array $fieldsets, string $resourceKey, array $includes): array |
|
91 | + protected function parseFieldsets( array $fieldsets, string $resourceKey, array $includes ): array |
|
92 | 92 | { |
93 | - $includes = array_map(function ($include) use ($resourceKey) { |
|
93 | + $includes = array_map( function ( $include ) use ($resourceKey) { |
|
94 | 94 | return "$resourceKey.$include"; |
95 | - }, $includes); |
|
95 | + }, $includes ); |
|
96 | 96 | |
97 | - foreach ($fieldsets as $key => $fields) { |
|
98 | - if (is_numeric($key)) { |
|
99 | - unset($fieldsets[$key]); |
|
97 | + foreach ( $fieldsets as $key => $fields ) { |
|
98 | + if ( is_numeric( $key ) ) { |
|
99 | + unset($fieldsets[ $key ]); |
|
100 | 100 | $key = $resourceKey; |
101 | 101 | } |
102 | 102 | |
103 | - $fieldsets[$key] = $this->parseFieldset($key, (array) $fields, $includes); |
|
103 | + $fieldsets[ $key ] = $this->parseFieldset( $key, (array) $fields, $includes ); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | return $fieldsets; |
@@ -114,13 +114,13 @@ discard block |
||
114 | 114 | * @param array $includes |
115 | 115 | * @return string |
116 | 116 | */ |
117 | - protected function parseFieldset(string $key, array $fields, array $includes): string |
|
117 | + protected function parseFieldset( string $key, array $fields, array $includes ): string |
|
118 | 118 | { |
119 | - $childIncludes = array_reduce($includes, function ($segments, $include) use ($key) { |
|
120 | - return array_merge($segments, $this->resolveChildIncludes($key, $include)); |
|
121 | - }, []); |
|
119 | + $childIncludes = array_reduce( $includes, function ( $segments, $include ) use ($key) { |
|
120 | + return array_merge( $segments, $this->resolveChildIncludes( $key, $include ) ); |
|
121 | + }, [ ] ); |
|
122 | 122 | |
123 | - return implode(',', array_merge($fields, array_unique($childIncludes))); |
|
123 | + return implode( ',', array_merge( $fields, array_unique( $childIncludes ) ) ); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
@@ -130,14 +130,14 @@ discard block |
||
130 | 130 | * @param string $include |
131 | 131 | * @return array |
132 | 132 | */ |
133 | - protected function resolveChildIncludes($key, string $include): array |
|
133 | + protected function resolveChildIncludes( $key, string $include ): array |
|
134 | 134 | { |
135 | - if (count($segments = explode('.', $include)) <= 1) { |
|
136 | - return []; |
|
135 | + if ( count( $segments = explode( '.', $include ) ) <= 1 ) { |
|
136 | + return [ ]; |
|
137 | 137 | } |
138 | 138 | |
139 | - $relation = $key === array_shift($segments) ? [$segments[0]] : []; |
|
139 | + $relation = $key === array_shift( $segments ) ? [ $segments[ 0 ] ] : [ ]; |
|
140 | 140 | |
141 | - return array_merge($relation, $this->resolveChildIncludes($key, implode('.', $segments))); |
|
141 | + return array_merge( $relation, $this->resolveChildIncludes( $key, implode( '.', $segments ) ) ); |
|
142 | 142 | } |
143 | 143 | } |
144 | 144 | \ No newline at end of file |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | * @param \Flugg\Responder\Resources\DataNormalizer $normalizer |
41 | 41 | * @param \Flugg\Responder\Contracts\Transformers\TransformerResolver $transformerResolver |
42 | 42 | */ |
43 | - public function __construct(DataNormalizer $normalizer, TransformerResolver $transformerResolver) |
|
43 | + public function __construct( DataNormalizer $normalizer, TransformerResolver $transformerResolver ) |
|
44 | 44 | { |
45 | 45 | $this->normalizer = $normalizer; |
46 | 46 | $this->transformerResolver = $transformerResolver; |
@@ -54,17 +54,17 @@ discard block |
||
54 | 54 | * @param string|null $resourceKey |
55 | 55 | * @return \League\Fractal\Resource\ResourceInterface |
56 | 56 | */ |
57 | - public function make($data = null, $transformer = null, string $resourceKey = null): ResourceInterface |
|
57 | + public function make( $data = null, $transformer = null, string $resourceKey = null ): ResourceInterface |
|
58 | 58 | { |
59 | - if ($data instanceof ResourceInterface) { |
|
60 | - return $data->setTransformer($this->resolveTransformer($data->getData(), $transformer ?: $data->getTransformer())); |
|
61 | - } elseif (is_null($data = $this->normalizer->normalize($data))) { |
|
62 | - return $this->instatiateResource($data); |
|
59 | + if ( $data instanceof ResourceInterface ) { |
|
60 | + return $data->setTransformer( $this->resolveTransformer( $data->getData(), $transformer ?: $data->getTransformer() ) ); |
|
61 | + } elseif ( is_null( $data = $this->normalizer->normalize( $data ) ) ) { |
|
62 | + return $this->instatiateResource( $data ); |
|
63 | 63 | } |
64 | 64 | |
65 | - $transformer = $this->resolveTransformer($data, $transformer); |
|
65 | + $transformer = $this->resolveTransformer( $data, $transformer ); |
|
66 | 66 | |
67 | - return $this->instatiateResource($data, $transformer, $resourceKey); |
|
67 | + return $this->instatiateResource( $data, $transformer, $resourceKey ); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -74,13 +74,13 @@ discard block |
||
74 | 74 | * @param \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer |
75 | 75 | * @return \Flugg\Responder\Transformers\Transformer|callable |
76 | 76 | */ |
77 | - protected function resolveTransformer($data, $transformer) |
|
77 | + protected function resolveTransformer( $data, $transformer ) |
|
78 | 78 | { |
79 | - if (isset($transformer)) { |
|
80 | - return $this->transformerResolver->resolve($transformer); |
|
79 | + if ( isset($transformer) ) { |
|
80 | + return $this->transformerResolver->resolve( $transformer ); |
|
81 | 81 | } |
82 | 82 | |
83 | - return $this->transformerResolver->resolveFromData($data); |
|
83 | + return $this->transformerResolver->resolveFromData( $data ); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
@@ -91,15 +91,15 @@ discard block |
||
91 | 91 | * @param string|null $resourceKey |
92 | 92 | * @return \League\Fractal\Resource\ResourceInterface |
93 | 93 | */ |
94 | - protected function instatiateResource($data, $transformer = null, string $resourceKey = null): ResourceInterface |
|
94 | + protected function instatiateResource( $data, $transformer = null, string $resourceKey = null ): ResourceInterface |
|
95 | 95 | { |
96 | - if (is_null($data)) { |
|
96 | + if ( is_null( $data ) ) { |
|
97 | 97 | return new NullResource; |
98 | - } elseif ($this->shouldCreateCollection($data)) { |
|
99 | - return new CollectionResource($data, $transformer, $resourceKey); |
|
98 | + } elseif ( $this->shouldCreateCollection( $data ) ) { |
|
99 | + return new CollectionResource( $data, $transformer, $resourceKey ); |
|
100 | 100 | } |
101 | 101 | |
102 | - return new ItemResource($data, $transformer, $resourceKey); |
|
102 | + return new ItemResource( $data, $transformer, $resourceKey ); |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | /** |
@@ -108,10 +108,10 @@ discard block |
||
108 | 108 | * @param mixed $data |
109 | 109 | * @return bool |
110 | 110 | */ |
111 | - protected function shouldCreateCollection($data): bool |
|
111 | + protected function shouldCreateCollection( $data ): bool |
|
112 | 112 | { |
113 | - if (is_array($data)) { |
|
114 | - return ! is_scalar(Arr::first($data)); |
|
113 | + if ( is_array( $data ) ) { |
|
114 | + return ! is_scalar( Arr::first( $data ) ); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | return $data instanceof Traversable; |
@@ -65,21 +65,21 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @var array |
67 | 67 | */ |
68 | - protected $with = []; |
|
68 | + protected $with = [ ]; |
|
69 | 69 | |
70 | 70 | /** |
71 | 71 | * A list of excluded relations. |
72 | 72 | * |
73 | 73 | * @var array |
74 | 74 | */ |
75 | - protected $without = []; |
|
75 | + protected $without = [ ]; |
|
76 | 76 | |
77 | 77 | /** |
78 | 78 | * A list of sparse fieldsets. |
79 | 79 | * |
80 | 80 | * @var array |
81 | 81 | */ |
82 | - protected $only = []; |
|
82 | + protected $only = [ ]; |
|
83 | 83 | |
84 | 84 | /** |
85 | 85 | * Construct the builder class. |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | * @param \Flugg\Responder\Contracts\TransformFactory $transformFactory |
89 | 89 | * @param \Flugg\Responder\Contracts\Pagination\PaginatorFactory $paginatorFactory |
90 | 90 | */ |
91 | - public function __construct(ResourceFactory $resourceFactory, TransformFactory $transformFactory, PaginatorFactory $paginatorFactory) |
|
91 | + public function __construct( ResourceFactory $resourceFactory, TransformFactory $transformFactory, PaginatorFactory $paginatorFactory ) |
|
92 | 92 | { |
93 | 93 | $this->resourceFactory = $resourceFactory; |
94 | 94 | $this->transformFactory = $transformFactory; |
@@ -103,14 +103,14 @@ discard block |
||
103 | 103 | * @param string|null $resourceKey |
104 | 104 | * @return $this |
105 | 105 | */ |
106 | - public function resource($data = null, $transformer = null, string $resourceKey = null) |
|
106 | + public function resource( $data = null, $transformer = null, string $resourceKey = null ) |
|
107 | 107 | { |
108 | - $this->resource = $this->resourceFactory->make($data, $transformer, $resourceKey); |
|
108 | + $this->resource = $this->resourceFactory->make( $data, $transformer, $resourceKey ); |
|
109 | 109 | |
110 | - if ($data instanceof CursorPaginator) { |
|
111 | - $this->cursor($this->paginatorFactory->makeCursor($data)); |
|
112 | - } elseif ($data instanceof LengthAwarePaginator) { |
|
113 | - $this->paginator($this->paginatorFactory->make($data)); |
|
110 | + if ( $data instanceof CursorPaginator ) { |
|
111 | + $this->cursor( $this->paginatorFactory->makeCursor( $data ) ); |
|
112 | + } elseif ( $data instanceof LengthAwarePaginator ) { |
|
113 | + $this->paginator( $this->paginatorFactory->make( $data ) ); |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | return $this; |
@@ -122,9 +122,9 @@ discard block |
||
122 | 122 | * @param \League\Fractal\Pagination\Cursor $cursor |
123 | 123 | * @return $this |
124 | 124 | */ |
125 | - public function cursor(Cursor $cursor) |
|
125 | + public function cursor( Cursor $cursor ) |
|
126 | 126 | { |
127 | - $this->resource->setCursor($cursor); |
|
127 | + $this->resource->setCursor( $cursor ); |
|
128 | 128 | |
129 | 129 | return $this; |
130 | 130 | } |
@@ -135,9 +135,9 @@ discard block |
||
135 | 135 | * @param \League\Fractal\Pagination\IlluminatePaginatorAdapter $paginator |
136 | 136 | * @return $this |
137 | 137 | */ |
138 | - public function paginator(IlluminatePaginatorAdapter $paginator) |
|
138 | + public function paginator( IlluminatePaginatorAdapter $paginator ) |
|
139 | 139 | { |
140 | - $this->resource->setPaginator($paginator); |
|
140 | + $this->resource->setPaginator( $paginator ); |
|
141 | 141 | |
142 | 142 | return $this; |
143 | 143 | } |
@@ -148,9 +148,9 @@ discard block |
||
148 | 148 | * @param array $data |
149 | 149 | * @return $this |
150 | 150 | */ |
151 | - public function meta(array $data) |
|
151 | + public function meta( array $data ) |
|
152 | 152 | { |
153 | - $this->resource->setMeta($data); |
|
153 | + $this->resource->setMeta( $data ); |
|
154 | 154 | |
155 | 155 | return $this; |
156 | 156 | } |
@@ -161,9 +161,9 @@ discard block |
||
161 | 161 | * @param string[]|string $relations |
162 | 162 | * @return $this |
163 | 163 | */ |
164 | - public function with($relations) |
|
164 | + public function with( $relations ) |
|
165 | 165 | { |
166 | - $this->with = array_merge($this->with, is_array($relations) ? $relations : func_get_args()); |
|
166 | + $this->with = array_merge( $this->with, is_array( $relations ) ? $relations : func_get_args() ); |
|
167 | 167 | |
168 | 168 | return $this; |
169 | 169 | } |
@@ -174,9 +174,9 @@ discard block |
||
174 | 174 | * @param string[]|string $relations |
175 | 175 | * @return $this |
176 | 176 | */ |
177 | - public function without($relations) |
|
177 | + public function without( $relations ) |
|
178 | 178 | { |
179 | - $this->without = array_merge($this->without, is_array($relations) ? $relations : func_get_args()); |
|
179 | + $this->without = array_merge( $this->without, is_array( $relations ) ? $relations : func_get_args() ); |
|
180 | 180 | |
181 | 181 | return $this; |
182 | 182 | } |
@@ -187,9 +187,9 @@ discard block |
||
187 | 187 | * @param string[]|string $fields |
188 | 188 | * @return $this |
189 | 189 | */ |
190 | - public function only($fields) |
|
190 | + public function only( $fields ) |
|
191 | 191 | { |
192 | - $this->only = array_merge($this->only, is_array($fields) ? $fields : func_get_args()); |
|
192 | + $this->only = array_merge( $this->only, is_array( $fields ) ? $fields : func_get_args() ); |
|
193 | 193 | |
194 | 194 | return $this; |
195 | 195 | } |
@@ -201,13 +201,13 @@ discard block |
||
201 | 201 | * @return $this |
202 | 202 | * @throws \Flugg\Responder\Exceptions\InvalidSerializerException |
203 | 203 | */ |
204 | - public function serializer($serializer) |
|
204 | + public function serializer( $serializer ) |
|
205 | 205 | { |
206 | - if (is_string($serializer)) { |
|
206 | + if ( is_string( $serializer ) ) { |
|
207 | 207 | $serializer = new $serializer; |
208 | 208 | } |
209 | 209 | |
210 | - if (! $serializer instanceof SerializerAbstract) { |
|
210 | + if ( ! $serializer instanceof SerializerAbstract ) { |
|
211 | 211 | throw new InvalidSerializerException; |
212 | 212 | } |
213 | 213 | |
@@ -223,13 +223,13 @@ discard block |
||
223 | 223 | */ |
224 | 224 | public function transform(): array |
225 | 225 | { |
226 | - $this->prepareRelations($this->resource->getData(), $this->resource->getTransformer()); |
|
226 | + $this->prepareRelations( $this->resource->getData(), $this->resource->getTransformer() ); |
|
227 | 227 | |
228 | - return $this->transformFactory->make($this->resource ?: new NullResource, $this->serializer, [ |
|
228 | + return $this->transformFactory->make( $this->resource ?: new NullResource, $this->serializer, [ |
|
229 | 229 | 'includes' => $this->with, |
230 | 230 | 'excludes' => $this->without, |
231 | 231 | 'fieldsets' => $this->only, |
232 | - ]); |
|
232 | + ] ); |
|
233 | 233 | } |
234 | 234 | |
235 | 235 | /** |
@@ -239,18 +239,18 @@ discard block |
||
239 | 239 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
240 | 240 | * @return void |
241 | 241 | */ |
242 | - protected function prepareRelations($data, $transformer) |
|
242 | + protected function prepareRelations( $data, $transformer ) |
|
243 | 243 | { |
244 | - if ($transformer instanceof Transformer) { |
|
245 | - $transformer->setRelations($this->with); |
|
246 | - $this->with($transformer->extractDefaultRelations()); |
|
244 | + if ( $transformer instanceof Transformer ) { |
|
245 | + $transformer->setRelations( $this->with ); |
|
246 | + $this->with( $transformer->extractDefaultRelations() ); |
|
247 | 247 | } |
248 | 248 | |
249 | - if ($data instanceof Model || $data instanceof Collection) { |
|
250 | - $data->load($this->with); |
|
249 | + if ( $data instanceof Model || $data instanceof Collection ) { |
|
250 | + $data->load( $this->with ); |
|
251 | 251 | } |
252 | 252 | |
253 | - $this->with = $this->stripRelations($this->with); |
|
253 | + $this->with = $this->stripRelations( $this->with ); |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | /** |
@@ -259,10 +259,10 @@ discard block |
||
259 | 259 | * @param array $relations |
260 | 260 | * @return void |
261 | 261 | */ |
262 | - protected function stripRelations(array $relations) |
|
262 | + protected function stripRelations( array $relations ) |
|
263 | 263 | { |
264 | - return collect($relations)->map(function ($value, $key) { |
|
265 | - return is_numeric($key) ? $value : $key; |
|
264 | + return collect( $relations )->map( function ( $value, $key ) { |
|
265 | + return is_numeric( $key ) ? $value : $key; |
|
266 | 266 | })->values()->all(); |
267 | 267 | } |
268 | 268 | } |
269 | 269 | \ No newline at end of file |