@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | * |
37 | 37 | * @param \Flugg\Responder\Contracts\ResponseFactory $responseFactory |
38 | 38 | */ |
39 | - public function __construct(ResponseFactory $responseFactory) |
|
39 | + public function __construct( ResponseFactory $responseFactory ) |
|
40 | 40 | { |
41 | 41 | $this->responseFactory = $responseFactory; |
42 | 42 | } |
@@ -47,12 +47,12 @@ discard block |
||
47 | 47 | * @param string[]|string $decorator |
48 | 48 | * @return $this |
49 | 49 | */ |
50 | - public function decorator($decorator) |
|
50 | + public function decorator( $decorator ) |
|
51 | 51 | { |
52 | - $decorators = is_array($decorator) ? $decorator : func_get_args(); |
|
52 | + $decorators = is_array( $decorator ) ? $decorator : func_get_args(); |
|
53 | 53 | |
54 | - foreach ($decorators as $decorator) { |
|
55 | - $this->responseFactory = new $decorator($this->responseFactory); |
|
54 | + foreach ( $decorators as $decorator ) { |
|
55 | + $this->responseFactory = new $decorator( $this->responseFactory ); |
|
56 | 56 | }; |
57 | 57 | |
58 | 58 | return $this; |
@@ -65,13 +65,13 @@ discard block |
||
65 | 65 | * @param array $headers |
66 | 66 | * @return \Illuminate\Http\JsonResponse |
67 | 67 | */ |
68 | - public function respond(int $status = null, array $headers = []): JsonResponse |
|
68 | + public function respond( int $status = null, array $headers = [ ] ): JsonResponse |
|
69 | 69 | { |
70 | - if (! is_null($status)) { |
|
71 | - $this->setStatusCode($status); |
|
70 | + if ( ! is_null( $status ) ) { |
|
71 | + $this->setStatusCode( $status ); |
|
72 | 72 | } |
73 | 73 | |
74 | - return $this->responseFactory->make($this->getOutput(), $this->status, $headers); |
|
74 | + return $this->responseFactory->make( $this->getOutput(), $this->status, $headers ); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | */ |
82 | 82 | public function toArray(): array |
83 | 83 | { |
84 | - return $this->respond()->getData(true); |
|
84 | + return $this->respond()->getData( true ); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | */ |
92 | 92 | public function toCollection(): Collection |
93 | 93 | { |
94 | - return new Collection($this->toArray()); |
|
94 | + return new Collection( $this->toArray() ); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -100,9 +100,9 @@ discard block |
||
100 | 100 | * @param int $options |
101 | 101 | * @return string |
102 | 102 | */ |
103 | - public function toJson($options = 0): string |
|
103 | + public function toJson( $options = 0 ): string |
|
104 | 104 | { |
105 | - return json_encode($this->toArray(), $options); |
|
105 | + return json_encode( $this->toArray(), $options ); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | * @param int $status |
112 | 112 | * @return void |
113 | 113 | */ |
114 | - protected function setStatusCode(int $status) |
|
114 | + protected function setStatusCode( int $status ) |
|
115 | 115 | { |
116 | - $this->validateStatusCode($this->status = $status); |
|
116 | + $this->validateStatusCode( $this->status = $status ); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -129,5 +129,5 @@ discard block |
||
129 | 129 | * @param int $status |
130 | 130 | * @return void |
131 | 131 | */ |
132 | - abstract protected function validateStatusCode(int $status); |
|
132 | + abstract protected function validateStatusCode( int $status ); |
|
133 | 133 | } |
@@ -56,11 +56,11 @@ discard block |
||
56 | 56 | * @param \Flugg\Responder\Contracts\ResponseFactory $responseFactory |
57 | 57 | * @param \Flugg\Responder\Contracts\ErrorFactory $errorFactory |
58 | 58 | */ |
59 | - public function __construct(ResponseFactory $responseFactory, ErrorFactory $errorFactory) |
|
59 | + public function __construct( ResponseFactory $responseFactory, ErrorFactory $errorFactory ) |
|
60 | 60 | { |
61 | 61 | $this->errorFactory = $errorFactory; |
62 | 62 | |
63 | - parent::__construct($responseFactory); |
|
63 | + parent::__construct( $responseFactory ); |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | * @param string|null $message |
71 | 71 | * @return $this |
72 | 72 | */ |
73 | - public function error(string $errorCode = null, string $message = null) |
|
73 | + public function error( string $errorCode = null, string $message = null ) |
|
74 | 74 | { |
75 | 75 | $this->errorCode = $errorCode; |
76 | 76 | $this->message = $message; |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | * @param array $data |
85 | 85 | * @return $this |
86 | 86 | */ |
87 | - public function data(array $data) |
|
87 | + public function data( array $data ) |
|
88 | 88 | { |
89 | - $this->data = array_merge((array) $this->data, $data); |
|
89 | + $this->data = array_merge( (array) $this->data, $data ); |
|
90 | 90 | |
91 | 91 | return $this; |
92 | 92 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | */ |
99 | 99 | protected function getOutput(): array |
100 | 100 | { |
101 | - return $this->errorFactory->make($this->errorCode, $this->message, $this->data); |
|
101 | + return $this->errorFactory->make( $this->errorCode, $this->message, $this->data ); |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | /** |
@@ -108,10 +108,10 @@ discard block |
||
108 | 108 | * @return void |
109 | 109 | * @throws \InvalidArgumentException |
110 | 110 | */ |
111 | - protected function validateStatusCode(int $status) |
|
111 | + protected function validateStatusCode( int $status ) |
|
112 | 112 | { |
113 | - if ($status < 400 || $status >= 600) { |
|
114 | - throw new InvalidArgumentException("{$status} is not a valid error HTTP status code."); |
|
113 | + if ( $status < 400 || $status >= 600 ) { |
|
114 | + throw new InvalidArgumentException( "{$status} is not a valid error HTTP status code." ); |
|
115 | 115 | } |
116 | 116 | } |
117 | 117 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | */ |
44 | 44 | protected function getStub() |
45 | 45 | { |
46 | - if ($this->option('plain')) { |
|
46 | + if ( $this->option( 'plain' ) ) { |
|
47 | 47 | return __DIR__ . '/../../resources/stubs/transformer.plain.stub'; |
48 | 48 | } |
49 | 49 | |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | * @param string $rootNamespace |
57 | 57 | * @return string |
58 | 58 | */ |
59 | - protected function getDefaultNamespace($rootNamespace) |
|
59 | + protected function getDefaultNamespace( $rootNamespace ) |
|
60 | 60 | { |
61 | 61 | return $rootNamespace . '\Transformers'; |
62 | 62 | } |
@@ -67,19 +67,19 @@ discard block |
||
67 | 67 | * @param string $name |
68 | 68 | * @return string |
69 | 69 | */ |
70 | - protected function buildClass($name) |
|
70 | + protected function buildClass( $name ) |
|
71 | 71 | { |
72 | - $replace = []; |
|
72 | + $replace = [ ]; |
|
73 | 73 | |
74 | - if (! $this->option('model') && ! $this->option('plain')) { |
|
75 | - $this->input->setOption('model', $this->resolveModelFromClassName()); |
|
74 | + if ( ! $this->option( 'model' ) && ! $this->option( 'plain' ) ) { |
|
75 | + $this->input->setOption( 'model', $this->resolveModelFromClassName() ); |
|
76 | 76 | } |
77 | 77 | |
78 | - if ($this->option('model')) { |
|
79 | - $replace = $this->buildModelReplacements($replace); |
|
78 | + if ( $this->option( 'model' ) ) { |
|
79 | + $replace = $this->buildModelReplacements( $replace ); |
|
80 | 80 | } |
81 | 81 | |
82 | - return str_replace(array_keys($replace), array_values($replace), parent::buildClass($name)); |
|
82 | + return str_replace( array_keys( $replace ), array_values( $replace ), parent::buildClass( $name ) ); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | */ |
90 | 90 | protected function resolveModelFromClassName() |
91 | 91 | { |
92 | - return 'App\\' . str_replace('Transformer', '', array_last(explode('/', $this->getNameInput()))); |
|
92 | + return 'App\\' . str_replace( 'Transformer', '', array_last( explode( '/', $this->getNameInput() ) ) ); |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | /** |
@@ -98,19 +98,19 @@ discard block |
||
98 | 98 | * @param array $replace |
99 | 99 | * @return array |
100 | 100 | */ |
101 | - protected function buildModelReplacements(array $replace) |
|
101 | + protected function buildModelReplacements( array $replace ) |
|
102 | 102 | { |
103 | - if (! class_exists($modelClass = $this->parseModel($this->option('model')))) { |
|
104 | - if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) { |
|
105 | - $this->call('make:model', ['name' => $modelClass]); |
|
103 | + if ( ! class_exists( $modelClass = $this->parseModel( $this->option( 'model' ) ) ) ) { |
|
104 | + if ( $this->confirm( "A {$modelClass} model does not exist. Do you want to generate it?", true ) ) { |
|
105 | + $this->call( 'make:model', [ 'name' => $modelClass ] ); |
|
106 | 106 | } |
107 | 107 | } |
108 | 108 | |
109 | - return array_merge($replace, [ |
|
109 | + return array_merge( $replace, [ |
|
110 | 110 | 'DummyFullModelClass' => $modelClass, |
111 | - 'DummyModelClass' => class_basename($modelClass), |
|
112 | - 'DummyModelVariable' => lcfirst(class_basename($modelClass)), |
|
113 | - ]); |
|
111 | + 'DummyModelClass' => class_basename( $modelClass ), |
|
112 | + 'DummyModelVariable' => lcfirst( class_basename( $modelClass ) ), |
|
113 | + ] ); |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | /** |
@@ -119,15 +119,15 @@ discard block |
||
119 | 119 | * @param string $model |
120 | 120 | * @return string |
121 | 121 | */ |
122 | - protected function parseModel($model) |
|
122 | + protected function parseModel( $model ) |
|
123 | 123 | { |
124 | - if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
|
125 | - throw new InvalidArgumentException('Model name contains invalid characters.'); |
|
124 | + if ( preg_match( '([^A-Za-z0-9_/\\\\])', $model ) ) { |
|
125 | + throw new InvalidArgumentException( 'Model name contains invalid characters.' ); |
|
126 | 126 | } |
127 | 127 | |
128 | - $model = trim(str_replace('/', '\\', $model), '\\'); |
|
128 | + $model = trim( str_replace( '/', '\\', $model ), '\\' ); |
|
129 | 129 | |
130 | - if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) { |
|
130 | + if ( ! Str::startsWith( $model, $rootNamespace = $this->laravel->getNamespace() ) ) { |
|
131 | 131 | $model = $rootNamespace . $model; |
132 | 132 | } |
133 | 133 | |
@@ -142,8 +142,8 @@ discard block |
||
142 | 142 | protected function getOptions() |
143 | 143 | { |
144 | 144 | return [ |
145 | - ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a model transformer.'], |
|
146 | - ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain transformer.'], |
|
145 | + [ 'model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a model transformer.' ], |
|
146 | + [ 'plain', 'p', InputOption::VALUE_NONE, 'Generate a plain transformer.' ], |
|
147 | 147 | ]; |
148 | 148 | } |
149 | 149 | } |
150 | 150 | \ No newline at end of file |
@@ -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; |
@@ -30,14 +30,14 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @var array |
32 | 32 | */ |
33 | - protected $bindings = []; |
|
33 | + protected $bindings = [ ]; |
|
34 | 34 | |
35 | 35 | /** |
36 | 36 | * Construct the resolver class. |
37 | 37 | * |
38 | 38 | * @param \Illuminate\Contracts\Container\Container $container |
39 | 39 | */ |
40 | - public function __construct(Container $container) |
|
40 | + public function __construct( Container $container ) |
|
41 | 41 | { |
42 | 42 | $this->container = $container; |
43 | 43 | } |
@@ -49,11 +49,11 @@ discard block |
||
49 | 49 | * @param string|callback|null $transformer |
50 | 50 | * @return void |
51 | 51 | */ |
52 | - public function bind($transformable, $transformer = null) |
|
52 | + public function bind( $transformable, $transformer = null ) |
|
53 | 53 | { |
54 | - $this->bindings = array_merge($this->bindings, is_array($transformable) ? $transformable : [ |
|
54 | + $this->bindings = array_merge( $this->bindings, is_array( $transformable ) ? $transformable : [ |
|
55 | 55 | $transformable => $transformer, |
56 | - ]); |
|
56 | + ] ); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
@@ -63,13 +63,13 @@ discard block |
||
63 | 63 | * @return \Flugg\Responder\Transformers\Transformer|callable |
64 | 64 | * @throws \Flugg\Responder\Exceptions\InvalidTransformerException |
65 | 65 | */ |
66 | - public function resolve($transformer) |
|
66 | + public function resolve( $transformer ) |
|
67 | 67 | { |
68 | - if (is_string($transformer)) { |
|
69 | - return $this->container->make($transformer); |
|
68 | + if ( is_string( $transformer ) ) { |
|
69 | + return $this->container->make( $transformer ); |
|
70 | 70 | } |
71 | 71 | |
72 | - if (! is_callable($transformer) && ! $transformer instanceof Transformer) { |
|
72 | + if ( ! is_callable( $transformer ) && ! $transformer instanceof Transformer ) { |
|
73 | 73 | throw new InvalidTransformerException; |
74 | 74 | } |
75 | 75 | |
@@ -82,12 +82,12 @@ discard block |
||
82 | 82 | * @param mixed $data |
83 | 83 | * @return \Flugg\Responder\Transformers\Transformer|callable|null |
84 | 84 | */ |
85 | - public function resolveFromData($data) |
|
85 | + public function resolveFromData( $data ) |
|
86 | 86 | { |
87 | - $transformable = $this->resolveTransformable($data); |
|
88 | - $transformer = $this->resolveTransformer($transformable); |
|
87 | + $transformable = $this->resolveTransformable( $data ); |
|
88 | + $transformer = $this->resolveTransformer( $transformable ); |
|
89 | 89 | |
90 | - return $this->resolve($transformer); |
|
90 | + return $this->resolve( $transformer ); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
@@ -96,10 +96,10 @@ discard block |
||
96 | 96 | * @param mixed $data |
97 | 97 | * @return \Flugg\Responder\Contracts\Transformable|null |
98 | 98 | */ |
99 | - protected function resolveTransformable($data) |
|
99 | + protected function resolveTransformable( $data ) |
|
100 | 100 | { |
101 | - if (is_array($data) || $data instanceof Traversable) { |
|
102 | - foreach ($data as $item) { |
|
101 | + if ( is_array( $data ) || $data instanceof Traversable ) { |
|
102 | + foreach ( $data as $item ) { |
|
103 | 103 | return $item; |
104 | 104 | } |
105 | 105 | } |
@@ -113,13 +113,13 @@ discard block |
||
113 | 113 | * @param mixed $transformable |
114 | 114 | * @return \Flugg\Responder\Contracts\Transformable|callable|null |
115 | 115 | */ |
116 | - protected function resolveTransformer($transformable) |
|
116 | + protected function resolveTransformer( $transformable ) |
|
117 | 117 | { |
118 | - if (is_object($transformable) && key_exists(get_class($transformable), $this->bindings)) { |
|
119 | - return $this->bindings[get_class($transformable)]; |
|
118 | + if ( is_object( $transformable ) && key_exists( get_class( $transformable ), $this->bindings ) ) { |
|
119 | + return $this->bindings[ get_class( $transformable ) ]; |
|
120 | 120 | } |
121 | 121 | |
122 | - if ($transformable instanceof Transformable) { |
|
122 | + if ( $transformable instanceof Transformable ) { |
|
123 | 123 | return $transformable->transformer(); |
124 | 124 | } |
125 | 125 | |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | protected function resolveFallbackTransformer() |
135 | 135 | { |
136 | - return function ($data) { |
|
136 | + return function ( $data ) { |
|
137 | 137 | return $data instanceof Arrayable ? $data->toArray() : $data; |
138 | 138 | }; |
139 | 139 | } |
@@ -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 | - $this->with($transformer->extractDefaultRelations()); |
|
244 | + if ( $transformer instanceof Transformer ) { |
|
245 | + $this->with( $transformer->extractDefaultRelations() ); |
|
246 | 246 | } |
247 | 247 | |
248 | - if ($data instanceof Model || $data instanceof Collection) { |
|
249 | - $data->load($this->with); |
|
248 | + if ( $data instanceof Model || $data instanceof Collection ) { |
|
249 | + $data->load( $this->with ); |
|
250 | 250 | } |
251 | 251 | |
252 | - $this->with = collect($this->with)->map(function ($value, $key) { |
|
253 | - return is_numeric($key) ? $value : $key; |
|
252 | + $this->with = collect( $this->with )->map( function ( $value, $key ) { |
|
253 | + return is_numeric( $key ) ? $value : $key; |
|
254 | 254 | })->values()->all(); |
255 | 255 | } |
256 | 256 | } |
257 | 257 | \ No newline at end of file |