@@ -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; |