sarala-io /
sarala-laravel
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | declare(strict_types=1); |
||||||
| 4 | |||||||
| 5 | namespace Sarala\Transformer; |
||||||
| 6 | |||||||
| 7 | use Illuminate\Support\Arr; |
||||||
| 8 | use League\Fractal\TransformerAbstract as BaseTransformerAbstract; |
||||||
| 9 | use Sarala\Links; |
||||||
| 10 | use Sarala\Query\Fields; |
||||||
| 11 | |||||||
| 12 | abstract class TransformerAbstract extends BaseTransformerAbstract |
||||||
| 13 | { |
||||||
| 14 | const REQUIRED_FIELDS = [ |
||||||
| 15 | 'id', |
||||||
| 16 | ]; |
||||||
| 17 | |||||||
| 18 | public function transform($data): array |
||||||
| 19 | { |
||||||
| 20 | $user = auth(config('sarala.guard'))->user(); |
||||||
| 21 | |||||||
| 22 | $links = $this->links($data, $user)->all(); |
||||||
| 23 | $meta = $this->meta($data, $user); |
||||||
| 24 | $data = $this->filterFields($this->data($data)); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 25 | |||||||
| 26 | if (! empty($links)) { |
||||||
| 27 | $data['links'] = $links; |
||||||
| 28 | } |
||||||
| 29 | |||||||
| 30 | if (! empty($meta)) { |
||||||
| 31 | $data['meta'] = $meta; |
||||||
| 32 | } |
||||||
| 33 | |||||||
| 34 | return $data; |
||||||
| 35 | } |
||||||
| 36 | |||||||
| 37 | protected function links($model, $user = null): Links |
||||||
|
0 ignored issues
–
show
The parameter
$user is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$model is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 38 | { |
||||||
| 39 | return Links::make(); |
||||||
| 40 | } |
||||||
| 41 | |||||||
| 42 | protected function meta($model, $user = null): array |
||||||
|
0 ignored issues
–
show
The parameter
$user is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$model is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 43 | { |
||||||
| 44 | return []; |
||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | private function filterFields(array $data): array |
||||||
| 48 | { |
||||||
| 49 | $resourceName = $this->getCurrentScope()->getResource()->getResourceKey(); |
||||||
| 50 | /** @var Fields $fields */ |
||||||
| 51 | $fields = request()->fields(); |
||||||
| 52 | |||||||
| 53 | if (! $fields->has($resourceName)) { |
||||||
| 54 | return $data; |
||||||
| 55 | } |
||||||
| 56 | |||||||
| 57 | $fields = array_merge($fields->get($resourceName), self::REQUIRED_FIELDS); |
||||||
|
0 ignored issues
–
show
It seems like
$fields->get($resourceName) can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 58 | |||||||
| 59 | return Arr::only($data, $fields); |
||||||
| 60 | } |
||||||
| 61 | } |
||||||
| 62 |