@@ -25,9 +25,9 @@ |
||
| 25 | 25 | * @return void |
| 26 | 26 | * @throws ValidationFailedException |
| 27 | 27 | */ |
| 28 | - protected function failedValidation(Validator $validator) |
|
| 28 | + protected function failedValidation( Validator $validator ) |
|
| 29 | 29 | { |
| 30 | - throw new ValidationFailedException($validator); |
|
| 30 | + throw new ValidationFailedException( $validator ); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
@@ -19,9 +19,9 @@ discard block |
||
| 19 | 19 | * @param string $key |
| 20 | 20 | * @return bool |
| 21 | 21 | */ |
| 22 | - public function __isset($key) |
|
| 22 | + public function __isset( $key ) |
|
| 23 | 23 | { |
| 24 | - return parent::__isset(snake_case($key)); |
|
| 24 | + return parent::__isset( snake_case( $key ) ); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -30,9 +30,9 @@ discard block |
||
| 30 | 30 | * @param string $key |
| 31 | 31 | * @return mixed |
| 32 | 32 | */ |
| 33 | - public function __get($key) |
|
| 33 | + public function __get( $key ) |
|
| 34 | 34 | { |
| 35 | - return parent::__get(snake_case($key)); |
|
| 35 | + return parent::__get( snake_case( $key ) ); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | protected function getValidatorInstance() |
| 44 | 44 | { |
| 45 | - $this->getInputSource()->replace($this->getConvertedParameters()); |
|
| 45 | + $this->getInputSource()->replace( $this->getConvertedParameters() ); |
|
| 46 | 46 | |
| 47 | 47 | return parent::getValidatorInstance(); |
| 48 | 48 | } |
@@ -62,11 +62,11 @@ discard block |
||
| 62 | 62 | protected function getConvertedParameters():array |
| 63 | 63 | { |
| 64 | 64 | $parameters = $this->all(); |
| 65 | - $parameters = $this->castBooleans($parameters); |
|
| 66 | - $parameters = $this->convertToSnakeCase($parameters); |
|
| 65 | + $parameters = $this->castBooleans( $parameters ); |
|
| 66 | + $parameters = $this->convertToSnakeCase( $parameters ); |
|
| 67 | 67 | |
| 68 | - if (method_exists($this, 'convertParameters')) { |
|
| 69 | - $parameters = $this->convertParameters($parameters); |
|
| 68 | + if ( method_exists( $this, 'convertParameters' ) ) { |
|
| 69 | + $parameters = $this->convertParameters( $parameters ); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | return $parameters; |
@@ -85,16 +85,16 @@ discard block |
||
| 85 | 85 | * @param mixed $input |
| 86 | 86 | * @return array |
| 87 | 87 | */ |
| 88 | - protected function castBooleans($input):array |
|
| 88 | + protected function castBooleans( $input ):array |
|
| 89 | 89 | { |
| 90 | - if ($this->castToBooleanIsDisabled()) { |
|
| 90 | + if ( $this->castToBooleanIsDisabled() ) { |
|
| 91 | 91 | return; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - $casted = []; |
|
| 94 | + $casted = [ ]; |
|
| 95 | 95 | |
| 96 | - foreach ($input as $key => $value) { |
|
| 97 | - $casted[$key] = $this->castValueToBoolean($value); |
|
| 96 | + foreach ( $input as $key => $value ) { |
|
| 97 | + $casted[ $key ] = $this->castValueToBoolean( $value ); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | return $casted; |
@@ -116,10 +116,10 @@ discard block |
||
| 116 | 116 | * @param mixed $value |
| 117 | 117 | * @return mixed |
| 118 | 118 | */ |
| 119 | - protected function castValueToBoolean($value) |
|
| 119 | + protected function castValueToBoolean( $value ) |
|
| 120 | 120 | { |
| 121 | - if (in_array($value, ['true', 'false'])) { |
|
| 122 | - return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
| 121 | + if ( in_array( $value, [ 'true', 'false' ] ) ) { |
|
| 122 | + return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | return $value; |
@@ -131,19 +131,19 @@ discard block |
||
| 131 | 131 | * @param mixed $input |
| 132 | 132 | * @return mixed |
| 133 | 133 | */ |
| 134 | - protected function convertToSnakeCase($input) |
|
| 134 | + protected function convertToSnakeCase( $input ) |
|
| 135 | 135 | { |
| 136 | - if ($this->convertToSnakeCaseIsDisabled()) { |
|
| 136 | + if ( $this->convertToSnakeCaseIsDisabled() ) { |
|
| 137 | 137 | return; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - if (is_null($input)) { |
|
| 140 | + if ( is_null( $input ) ) { |
|
| 141 | 141 | return null; |
| 142 | - } elseif (is_array($input)) { |
|
| 143 | - return $this->convertArrayToSnakeCase($input); |
|
| 142 | + } elseif ( is_array( $input ) ) { |
|
| 143 | + return $this->convertArrayToSnakeCase( $input ); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - return snake_case($input); |
|
| 146 | + return snake_case( $input ); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | /** |
@@ -162,12 +162,12 @@ discard block |
||
| 162 | 162 | * @param array $input |
| 163 | 163 | * @return array |
| 164 | 164 | */ |
| 165 | - protected function convertArrayToSnakeCase(array $input):array |
|
| 165 | + protected function convertArrayToSnakeCase( array $input ):array |
|
| 166 | 166 | { |
| 167 | - $converted = []; |
|
| 167 | + $converted = [ ]; |
|
| 168 | 168 | |
| 169 | - foreach ($input as $key => $value) { |
|
| 170 | - $converted[snake_case($key)] = $value; |
|
| 169 | + foreach ( $input as $key => $value ) { |
|
| 170 | + $converted[ snake_case( $key ) ] = $value; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | return $converted; |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | * |
| 44 | 44 | * @param Filesystem $files |
| 45 | 45 | */ |
| 46 | - public function __construct(Filesystem $files) |
|
| 46 | + public function __construct( Filesystem $files ) |
|
| 47 | 47 | { |
| 48 | 48 | parent::__construct(); |
| 49 | 49 | |
@@ -67,21 +67,21 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | protected function generateTransformer() |
| 69 | 69 | { |
| 70 | - $name = (string) $this->argument('name'); |
|
| 70 | + $name = (string) $this->argument( 'name' ); |
|
| 71 | 71 | $path = $this->laravel->basePath() . '/app/Transformers/' . $name . '.php'; |
| 72 | 72 | |
| 73 | - if ($this->files->exists($path)) { |
|
| 74 | - return $this->error($name . ' already exists!'); |
|
| 73 | + if ( $this->files->exists( $path ) ) { |
|
| 74 | + return $this->error( $name . ' already exists!' ); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - $this->makeDirectory($path); |
|
| 77 | + $this->makeDirectory( $path ); |
|
| 78 | 78 | |
| 79 | - $stubPath = $this->option('pivot') ? 'resources/stubs/transformer.pivot.stub' : 'resources/stubs/transformer.stub'; |
|
| 80 | - $stub = $this->files->get(__DIR__ . '/../../' . $stubPath); |
|
| 79 | + $stubPath = $this->option( 'pivot' ) ? 'resources/stubs/transformer.pivot.stub' : 'resources/stubs/transformer.stub'; |
|
| 80 | + $stub = $this->files->get( __DIR__ . '/../../' . $stubPath ); |
|
| 81 | 81 | |
| 82 | - $this->files->put($path, $this->makeTransformer($name, $stub)); |
|
| 82 | + $this->files->put( $path, $this->makeTransformer( $name, $stub ) ); |
|
| 83 | 83 | |
| 84 | - $this->info('Transformer created successfully.'); |
|
| 84 | + $this->info( 'Transformer created successfully.' ); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
@@ -90,10 +90,10 @@ discard block |
||
| 90 | 90 | * @param string $path |
| 91 | 91 | * @return void |
| 92 | 92 | */ |
| 93 | - protected function makeDirectory(string $path) |
|
| 93 | + protected function makeDirectory( string $path ) |
|
| 94 | 94 | { |
| 95 | - if (! $this->files->isDirectory(dirname($path))) { |
|
| 96 | - $this->files->makeDirectory(dirname($path), 0777, true, true); |
|
| 95 | + if ( ! $this->files->isDirectory( dirname( $path ) ) ) { |
|
| 96 | + $this->files->makeDirectory( dirname( $path ), 0777, true, true ); |
|
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | 99 | |
@@ -104,11 +104,11 @@ discard block |
||
| 104 | 104 | * @param string $stub |
| 105 | 105 | * @return string |
| 106 | 106 | */ |
| 107 | - protected function makeTransformer(string $name, string $stub):string |
|
| 107 | + protected function makeTransformer( string $name, string $stub ):string |
|
| 108 | 108 | { |
| 109 | - $stub = $this->replaceNamespace($stub); |
|
| 110 | - $stub = $this->replaceClass($stub, $name); |
|
| 111 | - $stub = $this->replaceModel($stub, $name); |
|
| 109 | + $stub = $this->replaceNamespace( $stub ); |
|
| 110 | + $stub = $this->replaceClass( $stub, $name ); |
|
| 111 | + $stub = $this->replaceModel( $stub, $name ); |
|
| 112 | 112 | |
| 113 | 113 | return $stub; |
| 114 | 114 | } |
@@ -119,15 +119,15 @@ discard block |
||
| 119 | 119 | * @param string $stub |
| 120 | 120 | * @return string |
| 121 | 121 | */ |
| 122 | - protected function replaceNamespace(string $stub):string |
|
| 122 | + protected function replaceNamespace( string $stub ):string |
|
| 123 | 123 | { |
| 124 | - if (method_exists($this->laravel, 'getNameSpace')) { |
|
| 124 | + if ( method_exists( $this->laravel, 'getNameSpace' ) ) { |
|
| 125 | 125 | $namespace = $this->laravel->getNamespace() . 'Transformers'; |
| 126 | 126 | } else { |
| 127 | 127 | $namespace = 'App\Transformers'; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - $stub = str_replace('DummyNamespace', $namespace, $stub); |
|
| 130 | + $stub = str_replace( 'DummyNamespace', $namespace, $stub ); |
|
| 131 | 131 | |
| 132 | 132 | return $stub; |
| 133 | 133 | } |
@@ -139,9 +139,9 @@ discard block |
||
| 139 | 139 | * @param string $name |
| 140 | 140 | * @return string |
| 141 | 141 | */ |
| 142 | - protected function replaceClass(string $stub, string $name):string |
|
| 142 | + protected function replaceClass( string $stub, string $name ):string |
|
| 143 | 143 | { |
| 144 | - $stub = str_replace('DummyClass', $name, $stub); |
|
| 144 | + $stub = str_replace( 'DummyClass', $name, $stub ); |
|
| 145 | 145 | |
| 146 | 146 | return $stub; |
| 147 | 147 | } |
@@ -153,14 +153,14 @@ discard block |
||
| 153 | 153 | * @param string $name |
| 154 | 154 | * @return string |
| 155 | 155 | */ |
| 156 | - protected function replaceModel(string $stub, string $name):string |
|
| 156 | + protected function replaceModel( string $stub, string $name ):string |
|
| 157 | 157 | { |
| 158 | - $model = $this->getModelNamespace($name); |
|
| 159 | - $class = $this->getClassFromNamespace($model); |
|
| 158 | + $model = $this->getModelNamespace( $name ); |
|
| 159 | + $class = $this->getClassFromNamespace( $model ); |
|
| 160 | 160 | |
| 161 | - $stub = str_replace('DummyModelNamespace', $model, $stub); |
|
| 162 | - $stub = str_replace('DummyModelClass', $class, $stub); |
|
| 163 | - $stub = str_replace('DummyModelVariable', camel_case($class), $stub); |
|
| 161 | + $stub = str_replace( 'DummyModelNamespace', $model, $stub ); |
|
| 162 | + $stub = str_replace( 'DummyModelClass', $class, $stub ); |
|
| 163 | + $stub = str_replace( 'DummyModelVariable', camel_case( $class ), $stub ); |
|
| 164 | 164 | |
| 165 | 165 | return $stub; |
| 166 | 166 | } |
@@ -171,13 +171,13 @@ discard block |
||
| 171 | 171 | * @param string $name |
| 172 | 172 | * @return string |
| 173 | 173 | */ |
| 174 | - protected function getModelNamespace(string $name):string |
|
| 174 | + protected function getModelNamespace( string $name ):string |
|
| 175 | 175 | { |
| 176 | - if ($this->option('model')) { |
|
| 177 | - return $this->option('model'); |
|
| 176 | + if ( $this->option( 'model' ) ) { |
|
| 177 | + return $this->option( 'model' ); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | - return 'App\\' . str_replace('Transformer', '', $name); |
|
| 180 | + return 'App\\' . str_replace( 'Transformer', '', $name ); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | /** |
@@ -186,8 +186,8 @@ discard block |
||
| 186 | 186 | * @param string $namespace |
| 187 | 187 | * @return string |
| 188 | 188 | */ |
| 189 | - protected function getClassFromNamespace(string $namespace):string |
|
| 189 | + protected function getClassFromNamespace( string $namespace ):string |
|
| 190 | 190 | { |
| 191 | - return last(explode('\\', $namespace)); |
|
| 191 | + return last( explode( '\\', $namespace ) ); |
|
| 192 | 192 | } |
| 193 | 193 | } |
| 194 | 194 | \ No newline at end of file |
@@ -19,6 +19,6 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | public function __construct() |
| 21 | 21 | { |
| 22 | - parent::__construct('Given serializer is not an instance of [' . SerializerAbstract::class . '].'); |
|
| 22 | + parent::__construct( 'Given serializer is not an instance of [' . SerializerAbstract::class . '].' ); |
|
| 23 | 23 | } |
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -19,8 +19,8 @@ |
||
| 19 | 19 | * |
| 20 | 20 | * @param Model $model |
| 21 | 21 | */ |
| 22 | - public function __construct(Model $model) |
|
| 22 | + public function __construct( Model $model ) |
|
| 23 | 23 | { |
| 24 | - parent::__construct('The given transformer does not exist for model [' . get_class($model) . '].'); |
|
| 24 | + parent::__construct( 'The given transformer does not exist for model [' . get_class( $model ) . '].' ); |
|
| 25 | 25 | } |
| 26 | 26 | } |
| 27 | 27 | \ No newline at end of file |
@@ -18,14 +18,14 @@ |
||
| 18 | 18 | * @param Exception $exception |
| 19 | 19 | * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
| 20 | 20 | */ |
| 21 | - public function render($request, Exception $exception) |
|
| 21 | + public function render( $request, Exception $exception ) |
|
| 22 | 22 | { |
| 23 | - $this->transformException($exception); |
|
| 23 | + $this->transformException( $exception ); |
|
| 24 | 24 | |
| 25 | - if ($exception instanceof ApiException) { |
|
| 26 | - return $this->renderApiError($exception); |
|
| 25 | + if ( $exception instanceof ApiException ) { |
|
| 26 | + return $this->renderApiError( $exception ); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - return parent::render($request, $exception); |
|
| 29 | + return parent::render( $request, $exception ); |
|
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | \ No newline at end of file |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory |
| 47 | 47 | */ |
| 48 | - public function __construct(ResponseFactory $responseFactory) |
|
| 48 | + public function __construct( ResponseFactory $responseFactory ) |
|
| 49 | 49 | { |
| 50 | 50 | $this->responseFactory = $responseFactory; |
| 51 | 51 | } |
@@ -57,15 +57,15 @@ discard block |
||
| 57 | 57 | * @param array $headers |
| 58 | 58 | * @return \Illuminate\Http\JsonResponse |
| 59 | 59 | */ |
| 60 | - public function respond(int $statusCode = null, array $headers = []):JsonResponse |
|
| 60 | + public function respond( int $statusCode = null, array $headers = [ ] ):JsonResponse |
|
| 61 | 61 | { |
| 62 | - if (! is_null($statusCode)) { |
|
| 63 | - $this->setStatus($statusCode); |
|
| 62 | + if ( ! is_null( $statusCode ) ) { |
|
| 63 | + $this->setStatus( $statusCode ); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - $data = $this->includeStatusCode($this->toArray()); |
|
| 66 | + $data = $this->includeStatusCode( $this->toArray() ); |
|
| 67 | 67 | |
| 68 | - return $this->responseFactory->json($data, $this->statusCode, $headers); |
|
| 68 | + return $this->responseFactory->json( $data, $this->statusCode, $headers ); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | * @param int $statusCode |
| 75 | 75 | * @return self |
| 76 | 76 | */ |
| 77 | - public function setStatus(int $statusCode):ResponseBuilder |
|
| 77 | + public function setStatus( int $statusCode ):ResponseBuilder |
|
| 78 | 78 | { |
| 79 | 79 | $this->statusCode = $statusCode; |
| 80 | 80 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * @param bool $includeStatusCode |
| 88 | 88 | * @return self |
| 89 | 89 | */ |
| 90 | - public function setIncludeStatusCode(bool $includeStatusCode):ResponseBuilder |
|
| 90 | + public function setIncludeStatusCode( bool $includeStatusCode ):ResponseBuilder |
|
| 91 | 91 | { |
| 92 | 92 | $this->includeStatusCode = $includeStatusCode; |
| 93 | 93 | |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public function toCollection():Collection |
| 103 | 103 | { |
| 104 | - return new Collection($this->toArray()); |
|
| 104 | + return new Collection( $this->toArray() ); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -110,9 +110,9 @@ discard block |
||
| 110 | 110 | * @param int $options |
| 111 | 111 | * @return string |
| 112 | 112 | */ |
| 113 | - public function toJson($options = 0) |
|
| 113 | + public function toJson( $options = 0 ) |
|
| 114 | 114 | { |
| 115 | - return json_encode($this->jsonSerialize(), $options); |
|
| 115 | + return json_encode( $this->jsonSerialize(), $options ); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /** |
@@ -138,12 +138,12 @@ discard block |
||
| 138 | 138 | * @param array $data |
| 139 | 139 | * @return array |
| 140 | 140 | */ |
| 141 | - protected function includeStatusCode(array $data):array |
|
| 141 | + protected function includeStatusCode( array $data ):array |
|
| 142 | 142 | { |
| 143 | - if (! $this->includeStatusCode) { |
|
| 143 | + if ( ! $this->includeStatusCode ) { |
|
| 144 | 144 | return $data; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - return array_merge(['status' => $this->statusCode], $data); |
|
| 147 | + return array_merge( [ 'status' => $this->statusCode ], $data ); |
|
| 148 | 148 | } |
| 149 | 149 | } |
| 150 | 150 | \ No newline at end of file |
@@ -24,9 +24,9 @@ discard block |
||
| 24 | 24 | * @param mixed $message |
| 25 | 25 | * @return JsonResponse |
| 26 | 26 | */ |
| 27 | - public function errorResponse(string $errorCode = null, int $statusCode = null, $message = null):JsonResponse |
|
| 27 | + public function errorResponse( string $errorCode = null, int $statusCode = null, $message = null ):JsonResponse |
|
| 28 | 28 | { |
| 29 | - return app(Responder::class)->error($errorCode, $statusCode, $message); |
|
| 29 | + return app( Responder::class )->error( $errorCode, $statusCode, $message ); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /** |
@@ -37,9 +37,9 @@ discard block |
||
| 37 | 37 | * @param array $meta |
| 38 | 38 | * @return \Illuminate\Http\JsonResponse |
| 39 | 39 | */ |
| 40 | - public function successResponse($data = null, $statusCode = null, array $meta = []):JsonResponse |
|
| 40 | + public function successResponse( $data = null, $statusCode = null, array $meta = [ ] ):JsonResponse |
|
| 41 | 41 | { |
| 42 | - return app(Responder::class)->success($data, $statusCode, $meta); |
|
| 42 | + return app( Responder::class )->success( $data, $statusCode, $meta ); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
@@ -49,8 +49,8 @@ discard block |
||
| 49 | 49 | * @param callable|string|null $transformer |
| 50 | 50 | * @return \Flugg\Responder\Http\SuccessResponse |
| 51 | 51 | */ |
| 52 | - public function transform($data = null, $transformer = null):SuccessResponseBuilder |
|
| 52 | + public function transform( $data = null, $transformer = null ):SuccessResponseBuilder |
|
| 53 | 53 | { |
| 54 | - return app(Responder::class)->transform($data, $transformer); |
|
| 54 | + return app( Responder::class )->transform( $data, $transformer ); |
|
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | \ No newline at end of file |
@@ -6,8 +6,6 @@ |
||
| 6 | 6 | use Flugg\Responder\Exceptions\InvalidSerializerException; |
| 7 | 7 | use Flugg\Responder\Exceptions\InvalidTransformerException; |
| 8 | 8 | use Flugg\Responder\ResourceFactory; |
| 9 | -use Flugg\Responder\ResourceResolver; |
|
| 10 | -use Flugg\Responder\Transformation; |
|
| 11 | 9 | use Flugg\Responder\Transformer; |
| 12 | 10 | use Illuminate\Contracts\Routing\ResponseFactory; |
| 13 | 11 | use Illuminate\Database\Eloquent\Model; |
@@ -38,14 +38,14 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @var array |
| 40 | 40 | */ |
| 41 | - protected $meta = []; |
|
| 41 | + protected $meta = [ ]; |
|
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | 44 | * The included relations. |
| 45 | 45 | * |
| 46 | 46 | * @var array |
| 47 | 47 | */ |
| 48 | - protected $relations = []; |
|
| 48 | + protected $relations = [ ]; |
|
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * The Fractal resource instance containing the data and transformer. |
@@ -75,13 +75,13 @@ discard block |
||
| 75 | 75 | * @param \Flugg\Responder\ResourceFactory $resourceFactory |
| 76 | 76 | * @param \League\Fractal\Manager $manager |
| 77 | 77 | */ |
| 78 | - public function __construct(ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager) |
|
| 78 | + public function __construct( ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager ) |
|
| 79 | 79 | { |
| 80 | 80 | $this->resourceFactory = $resourceFactory; |
| 81 | 81 | $this->manager = $manager; |
| 82 | 82 | $this->resource = $this->resourceFactory->make(); |
| 83 | 83 | |
| 84 | - parent::__construct($responseFactory); |
|
| 84 | + parent::__construct( $responseFactory ); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
@@ -90,9 +90,9 @@ discard block |
||
| 90 | 90 | * @param array $data |
| 91 | 91 | * @return self |
| 92 | 92 | */ |
| 93 | - public function addMeta(array $data):SuccessResponseBuilder |
|
| 93 | + public function addMeta( array $data ):SuccessResponseBuilder |
|
| 94 | 94 | { |
| 95 | - $this->meta = array_merge($this->meta, $data); |
|
| 95 | + $this->meta = array_merge( $this->meta, $data ); |
|
| 96 | 96 | |
| 97 | 97 | return $this; |
| 98 | 98 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | public function include($relations):SuccessResponseBuilder |
| 107 | 107 | { |
| 108 | - $this->relations = array_merge($this->relations, $relations); |
|
| 108 | + $this->relations = array_merge( $this->relations, $relations ); |
|
| 109 | 109 | |
| 110 | 110 | return $this; |
| 111 | 111 | } |
@@ -116,9 +116,9 @@ discard block |
||
| 116 | 116 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
| 117 | 117 | * @return self |
| 118 | 118 | */ |
| 119 | - public function serializer($serializer):SuccessResponseBuilder |
|
| 119 | + public function serializer( $serializer ):SuccessResponseBuilder |
|
| 120 | 120 | { |
| 121 | - $this->manager->setSerializer($this->resolveSerializer($serializer)); |
|
| 121 | + $this->manager->setSerializer( $this->resolveSerializer( $serializer ) ); |
|
| 122 | 122 | |
| 123 | 123 | return $this; |
| 124 | 124 | } |
@@ -130,13 +130,13 @@ discard block |
||
| 130 | 130 | * @return self |
| 131 | 131 | * @throws \InvalidArgumentException |
| 132 | 132 | */ |
| 133 | - public function setStatus(int $statusCode):ResponseBuilder |
|
| 133 | + public function setStatus( int $statusCode ):ResponseBuilder |
|
| 134 | 134 | { |
| 135 | - if ($statusCode < 100 || $statusCode >= 400) { |
|
| 136 | - throw new InvalidArgumentException("{$statusCode} is not a valid success HTTP status code."); |
|
| 135 | + if ( $statusCode < 100 || $statusCode >= 400 ) { |
|
| 136 | + throw new InvalidArgumentException( "{$statusCode} is not a valid success HTTP status code." ); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - return parent::setStatus($statusCode); |
|
| 139 | + return parent::setStatus( $statusCode ); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
@@ -148,25 +148,25 @@ discard block |
||
| 148 | 148 | * @param string|null $resourceKey |
| 149 | 149 | * @return self |
| 150 | 150 | */ |
| 151 | - public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder |
|
| 151 | + public function transform( $data = null, $transformer = null, string $resourceKey = null ):SuccessResponseBuilder |
|
| 152 | 152 | { |
| 153 | - $resource = $this->resourceFactory->make($data); |
|
| 153 | + $resource = $this->resourceFactory->make( $data ); |
|
| 154 | 154 | |
| 155 | - if (! is_null($resource->getData())) { |
|
| 156 | - $model = $this->resolveModel($resource->getData()); |
|
| 157 | - $transformer = $this->resolveTransformer($model, $transformer); |
|
| 158 | - $resourceKey = $this->resolveResourceKey($model, $resourceKey); |
|
| 155 | + if ( ! is_null( $resource->getData() ) ) { |
|
| 156 | + $model = $this->resolveModel( $resource->getData() ); |
|
| 157 | + $transformer = $this->resolveTransformer( $model, $transformer ); |
|
| 158 | + $resourceKey = $this->resolveResourceKey( $model, $resourceKey ); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - if ($transformer instanceof Transformer) { |
|
| 162 | - $this->include($relations = $this->resolveNestedRelations($resource->getData())); |
|
| 161 | + if ( $transformer instanceof Transformer ) { |
|
| 162 | + $this->include( $relations = $this->resolveNestedRelations( $resource->getData() ) ); |
|
| 163 | 163 | |
| 164 | - if ($transformer->allRelationsAllowed()) { |
|
| 165 | - $transformer->setRelations($relations); |
|
| 164 | + if ( $transformer->allRelationsAllowed() ) { |
|
| 165 | + $transformer->setRelations( $relations ); |
|
| 166 | 166 | } |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - $this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey); |
|
| 169 | + $this->resource = $resource->setTransformer( $transformer )->setResourceKey( $resourceKey ); |
|
| 170 | 170 | |
| 171 | 171 | return $this; |
| 172 | 172 | } |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | public function toArray():array |
| 180 | 180 | { |
| 181 | - return $this->serialize($this->getResource()); |
|
| 181 | + return $this->serialize( $this->getResource() ); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | /** |
@@ -188,14 +188,14 @@ discard block |
||
| 188 | 188 | */ |
| 189 | 189 | public function getResource():ResourceInterface |
| 190 | 190 | { |
| 191 | - $this->manager->parseIncludes($this->relations); |
|
| 191 | + $this->manager->parseIncludes( $this->relations ); |
|
| 192 | 192 | $transformer = $this->resource->getTransformer(); |
| 193 | 193 | |
| 194 | - if ($transformer instanceof Transformer && $transformer->allRelationsAllowed()) { |
|
| 195 | - $this->resource->setTransformer($transformer->setRelations($this->manager->getRequestedIncludes())); |
|
| 194 | + if ( $transformer instanceof Transformer && $transformer->allRelationsAllowed() ) { |
|
| 195 | + $this->resource->setTransformer( $transformer->setRelations( $this->manager->getRequestedIncludes() ) ); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - return $this->resource->setMeta($this->meta); |
|
| 198 | + return $this->resource->setMeta( $this->meta ); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | /** |
@@ -215,13 +215,13 @@ discard block |
||
| 215 | 215 | * @return \League\Fractal\Serializer\SerializerAbstract |
| 216 | 216 | * @throws \Flugg\Responder\Exceptions\InvalidSerializerException |
| 217 | 217 | */ |
| 218 | - protected function resolveSerializer($serializer):SerializerAbstract |
|
| 218 | + protected function resolveSerializer( $serializer ):SerializerAbstract |
|
| 219 | 219 | { |
| 220 | - if (is_string($serializer)) { |
|
| 220 | + if ( is_string( $serializer ) ) { |
|
| 221 | 221 | $serializer = new $serializer; |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - if (! $serializer instanceof SerializerAbstract) { |
|
| 224 | + if ( ! $serializer instanceof SerializerAbstract ) { |
|
| 225 | 225 | throw new InvalidSerializerException(); |
| 226 | 226 | } |
| 227 | 227 | |
@@ -235,15 +235,15 @@ discard block |
||
| 235 | 235 | * @return \Illuminate\Database\Eloquent\Model |
| 236 | 236 | * @throws \InvalidArgumentException |
| 237 | 237 | */ |
| 238 | - protected function resolveModel($data):Model |
|
| 238 | + protected function resolveModel( $data ):Model |
|
| 239 | 239 | { |
| 240 | - if ($data instanceof Model) { |
|
| 240 | + if ( $data instanceof Model ) { |
|
| 241 | 241 | return $data; |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - $model = array_values($data)[0]; |
|
| 245 | - if (! $model instanceof Model) { |
|
| 246 | - throw new InvalidArgumentException('You can only transform data containing Eloquent models.'); |
|
| 244 | + $model = array_values( $data )[ 0 ]; |
|
| 245 | + if ( ! $model instanceof Model ) { |
|
| 246 | + throw new InvalidArgumentException( 'You can only transform data containing Eloquent models.' ); |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | return $model; |
@@ -256,15 +256,15 @@ discard block |
||
| 256 | 256 | * @param \Flugg\Responder\Transformer|callable|null $transformer |
| 257 | 257 | * @return \Flugg\Responder\Transformer|callable |
| 258 | 258 | */ |
| 259 | - protected function resolveTransformer(Model $model, $transformer = null) |
|
| 259 | + protected function resolveTransformer( Model $model, $transformer = null ) |
|
| 260 | 260 | { |
| 261 | - $transformer = $transformer ?: $this->resolveTransformerFromModel($model); |
|
| 261 | + $transformer = $transformer ?: $this->resolveTransformerFromModel( $model ); |
|
| 262 | 262 | |
| 263 | - if (is_string($transformer)) { |
|
| 263 | + if ( is_string( $transformer ) ) { |
|
| 264 | 264 | $transformer = new $transformer; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - return $this->parseTransformer($transformer, $model); |
|
| 267 | + return $this->parseTransformer( $transformer, $model ); |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | /** |
@@ -274,10 +274,10 @@ discard block |
||
| 274 | 274 | * @param \Illuminate\Database\ELoquent\Model $model |
| 275 | 275 | * @return \Flugg\Responder\Transformer|callable |
| 276 | 276 | */ |
| 277 | - protected function resolveTransformerFromModel(Model $model) |
|
| 277 | + protected function resolveTransformerFromModel( Model $model ) |
|
| 278 | 278 | { |
| 279 | - if (! $model instanceof Transformable) { |
|
| 280 | - return function ($model) { |
|
| 279 | + if ( ! $model instanceof Transformable ) { |
|
| 280 | + return function ( $model ) { |
|
| 281 | 281 | return $model->toArray(); |
| 282 | 282 | }; |
| 283 | 283 | } |
@@ -293,14 +293,14 @@ discard block |
||
| 293 | 293 | * @return \Flugg\Responder\Transformer|callable |
| 294 | 294 | * @throws \InvalidTransformerException |
| 295 | 295 | */ |
| 296 | - protected function parseTransformer($transformer, Model $model) |
|
| 296 | + protected function parseTransformer( $transformer, Model $model ) |
|
| 297 | 297 | { |
| 298 | - if ($transformer instanceof Transformer) { |
|
| 299 | - $relations = $transformer->allRelationsAllowed() ? $this->resolveRelations($model) : $transformer->getRelations(); |
|
| 300 | - $transformer = $transformer->setRelations($relations); |
|
| 298 | + if ( $transformer instanceof Transformer ) { |
|
| 299 | + $relations = $transformer->allRelationsAllowed() ? $this->resolveRelations( $model ) : $transformer->getRelations(); |
|
| 300 | + $transformer = $transformer->setRelations( $relations ); |
|
| 301 | 301 | |
| 302 | - } elseif (! is_callable($transformer)) { |
|
| 303 | - throw new InvalidTransformerException($model); |
|
| 302 | + } elseif ( ! is_callable( $transformer ) ) { |
|
| 303 | + throw new InvalidTransformerException( $model ); |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | return $transformer; |
@@ -312,9 +312,9 @@ discard block |
||
| 312 | 312 | * @param \Illuminate\Database\Eloquent\Model $model |
| 313 | 313 | * @return array |
| 314 | 314 | */ |
| 315 | - protected function resolveRelations(Model $model):array |
|
| 315 | + protected function resolveRelations( Model $model ):array |
|
| 316 | 316 | { |
| 317 | - return array_keys($model->getRelations()); |
|
| 317 | + return array_keys( $model->getRelations() ); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -323,22 +323,22 @@ discard block |
||
| 323 | 323 | * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $data |
| 324 | 324 | * @return array |
| 325 | 325 | */ |
| 326 | - protected function resolveNestedRelations($data):array |
|
| 326 | + protected function resolveNestedRelations( $data ):array |
|
| 327 | 327 | { |
| 328 | - if (is_null($data)) { |
|
| 329 | - return []; |
|
| 328 | + if ( is_null( $data ) ) { |
|
| 329 | + return [ ]; |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | - $data = $data instanceof Model ? [$data] : $data; |
|
| 332 | + $data = $data instanceof Model ? [ $data ] : $data; |
|
| 333 | 333 | |
| 334 | - return collect($data)->flatMap(function ($model) { |
|
| 335 | - $relations = collect($model->getRelations()); |
|
| 334 | + return collect( $data )->flatMap( function ( $model ) { |
|
| 335 | + $relations = collect( $model->getRelations() ); |
|
| 336 | 336 | |
| 337 | - return $relations->keys()->merge($relations->flatMap(function ($relation, $key) { |
|
| 338 | - return collect($this->resolveNestedRelations($relation))->map(function ($nestedRelation) use ($key) { |
|
| 337 | + return $relations->keys()->merge( $relations->flatMap( function ( $relation, $key ) { |
|
| 338 | + return collect( $this->resolveNestedRelations( $relation ) )->map( function ( $nestedRelation ) use ($key) { |
|
| 339 | 339 | return $key . '.' . $nestedRelation; |
| 340 | 340 | }); |
| 341 | - })); |
|
| 341 | + }) ); |
|
| 342 | 342 | })->toArray(); |
| 343 | 343 | } |
| 344 | 344 | |
@@ -349,13 +349,13 @@ discard block |
||
| 349 | 349 | * @param string|null $resourceKey |
| 350 | 350 | * @return string |
| 351 | 351 | */ |
| 352 | - protected function resolveResourceKey(Model $model, string $resourceKey = null):string |
|
| 352 | + protected function resolveResourceKey( Model $model, string $resourceKey = null ):string |
|
| 353 | 353 | { |
| 354 | - if (! is_null($resourceKey)) { |
|
| 354 | + if ( ! is_null( $resourceKey ) ) { |
|
| 355 | 355 | return $resourceKey; |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | - if (method_exists($model, 'getResourceKey')) { |
|
| 358 | + if ( method_exists( $model, 'getResourceKey' ) ) { |
|
| 359 | 359 | return $model->getResourceKey(); |
| 360 | 360 | } |
| 361 | 361 | |
@@ -368,8 +368,8 @@ discard block |
||
| 368 | 368 | * @param ResourceInterface $resource |
| 369 | 369 | * @return array |
| 370 | 370 | */ |
| 371 | - protected function serialize(ResourceInterface $resource):array |
|
| 371 | + protected function serialize( ResourceInterface $resource ):array |
|
| 372 | 372 | { |
| 373 | - return $this->manager->createData($resource)->toArray(); |
|
| 373 | + return $this->manager->createData( $resource )->toArray(); |
|
| 374 | 374 | } |
| 375 | 375 | } |
| 376 | 376 | \ No newline at end of file |