|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: frowhy |
|
5
|
|
|
* Date: 2017/12/6 |
|
6
|
|
|
* Time: δΈε10:30 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Modules\Core\Abstracts; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Support\Arr; |
|
12
|
|
|
use Illuminate\Support\Str; |
|
13
|
|
|
use Modules\Core\Contracts\Repository\Filter; |
|
14
|
|
|
use Modules\Core\Supports\Response; |
|
15
|
|
|
use League\Fractal\TransformerAbstract as BaseTransformerAbstract; |
|
16
|
|
|
|
|
17
|
|
|
abstract class TransformerAbstract extends BaseTransformerAbstract |
|
18
|
|
|
{ |
|
19
|
|
|
protected $transform; |
|
20
|
|
|
protected $field; |
|
21
|
|
|
protected $filter; |
|
22
|
|
|
|
|
23
|
|
|
abstract public function fields($attribute); |
|
24
|
|
|
|
|
25
|
|
|
public function transform($transform) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->transform = $transform; |
|
28
|
|
|
$this->field = $this->fields($transform); |
|
29
|
|
|
$this->parseRequestedFields(); |
|
30
|
|
|
$this->parseExcludeFields(); |
|
31
|
|
|
$this->filter = app(Filter::class); |
|
32
|
|
|
|
|
33
|
|
|
return $this->field; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function parseRequestedFields() |
|
37
|
|
|
{ |
|
38
|
|
|
$class = class_basename($this->transform); |
|
39
|
|
|
$param = Response::param('requested_fields') ?? $this->filter->requestedFields[$class]; |
|
40
|
|
|
|
|
41
|
|
|
if (!is_null($param)) { |
|
42
|
|
|
|
|
43
|
|
|
if (is_array($param)) { |
|
44
|
|
|
$requestedFields = $param; |
|
45
|
|
|
} else { |
|
46
|
|
|
$requestedFields = explode(',', $param); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
foreach ($requestedFields as $requestedField) { |
|
50
|
|
|
if ($this instanceof TransformerAbstract) { |
|
51
|
|
|
$this->field = Arr::only($this->field, $this->getFilterField($requestedField)); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function parseExcludeFields() |
|
58
|
|
|
{ |
|
59
|
|
|
$class = class_basename($this->transform); |
|
60
|
|
|
$param = Response::param('exclude_fields') ?? $this->filter->excludeFields[$class]; |
|
61
|
|
|
|
|
62
|
|
|
if (!is_null($param)) { |
|
63
|
|
|
|
|
64
|
|
|
if (is_array($param)) { |
|
65
|
|
|
$excludeFields = $param; |
|
66
|
|
|
} else { |
|
67
|
|
|
$excludeFields = explode(',', $param); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
foreach ($excludeFields as $excludeField) { |
|
71
|
|
|
if ($this instanceof TransformerAbstract) { |
|
72
|
|
|
$this->field = Arr::except($this->field, $this->getFilterField($excludeField)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function getFilterField(string $field): ?string |
|
79
|
|
|
{ |
|
80
|
|
|
$scope = null; |
|
81
|
|
|
|
|
82
|
|
|
if (Str::contains($field, '.')) { |
|
83
|
|
|
$fieldArray = explode('.', $field); |
|
84
|
|
|
$length = count($fieldArray); |
|
85
|
|
|
$field = Arr::last($fieldArray); |
|
86
|
|
|
Arr::forget($fieldArray, $length - 1); |
|
87
|
|
|
$scope = implode('.', $fieldArray); |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($scope === $this->getCurrentScope()->getIdentifier()) { |
|
92
|
|
|
return $field; |
|
93
|
|
|
} else { |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|