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 League\Fractal\TransformerAbstract as BaseTransformerAbstract; |
14
|
|
|
use Modules\Core\Contracts\Repository\Filter; |
15
|
|
|
use Modules\Core\Supports\Response; |
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->filter = app(Filter::class); |
30
|
|
|
$this->parseRequestedFields(); |
31
|
|
|
$this->parseExcludeFields(); |
32
|
|
|
|
33
|
|
|
return $this->field; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function parseRequestedFields() |
37
|
|
|
{ |
38
|
|
|
$class = class_basename($this->transform); |
39
|
|
|
|
40
|
|
|
$param = Response::param('requested_fields') ?? Arr::get($this->filter->requestedFields, $class); |
41
|
|
|
|
42
|
|
|
if (!is_null($param)) { |
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 self) { |
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') ?? Arr::get($this->filter->excludeFields, $class); |
61
|
|
|
|
62
|
|
|
if (!is_null($param)) { |
63
|
|
|
if (is_array($param)) { |
64
|
|
|
$excludeFields = $param; |
65
|
|
|
} else { |
66
|
|
|
$excludeFields = explode(',', $param); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($excludeFields as $excludeField) { |
70
|
|
|
if ($this instanceof self) { |
71
|
|
|
$this->field = Arr::except($this->field, $this->getFilterField($excludeField)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function getFilterField(string $field): ?string |
78
|
|
|
{ |
79
|
|
|
$scope = null; |
80
|
|
|
|
81
|
|
|
if (Str::contains($field, '.')) { |
82
|
|
|
$fieldArray = explode('.', $field); |
83
|
|
|
$length = count($fieldArray); |
84
|
|
|
$field = Arr::last($fieldArray); |
85
|
|
|
Arr::forget($fieldArray, $length - 1); |
86
|
|
|
$scope = implode('.', $fieldArray); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($scope === $this->getCurrentScope()->getIdentifier()) { |
90
|
|
|
return $field; |
91
|
|
|
} else { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|