|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Napp\Core\Api\Transformers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
|
|
9
|
|
|
class ApiTransformer implements TransformerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $apiMapping = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param array|Model $apiMapping |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
4 |
|
public function setApiMapping($apiMapping) |
|
21
|
|
|
{ |
|
22
|
4 |
|
$this->apiMapping = []; |
|
23
|
|
|
|
|
24
|
4 |
|
if (true === $apiMapping instanceof Model && true === property_exists($apiMapping, 'apiMapping')) { |
|
25
|
|
|
$this->apiMapping = $apiMapping->apiMapping; |
|
26
|
4 |
|
} elseif (true === is_array($apiMapping)) { |
|
27
|
4 |
|
$this->apiMapping = $apiMapping; |
|
28
|
|
|
} |
|
29
|
4 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array|Arrayable $data |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
8 |
|
public function transformInput($data): array |
|
36
|
|
|
{ |
|
37
|
8 |
|
$input = []; |
|
38
|
|
|
|
|
39
|
8 |
|
$data = (true === is_array($data)) ? $data : $data->toArray(); |
|
40
|
8 |
|
foreach ($data as $key => $value) { |
|
41
|
4 |
|
$input[$this->findOriginalKey($key)] = $value; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
8 |
|
return $input; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array|Arrayable $data |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function transformOutput($data): array |
|
52
|
|
|
{ |
|
53
|
2 |
|
$output = []; |
|
54
|
|
|
|
|
55
|
2 |
|
if (true === $data instanceof Collection) { |
|
56
|
|
|
foreach ($data as $item) { |
|
57
|
|
|
$output[] = $this->transformOutput($item); |
|
58
|
|
|
} |
|
59
|
|
|
} else { |
|
60
|
2 |
|
$data = (true === is_array($data)) ? $data : $data->toArray(); |
|
61
|
2 |
|
foreach ($data as $key => $value) { |
|
62
|
2 |
|
$output[$this->findNewKey($key)] = $this->convertValueType($key, $value); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
return $output; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $newKey |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
4 |
|
protected function findOriginalKey(string $newKey) |
|
74
|
|
|
{ |
|
75
|
4 |
|
foreach ($this->apiMapping as $key => $value) { |
|
76
|
2 |
|
if (true === in_array($newKey, $value)) { |
|
77
|
2 |
|
return $key; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
return $newKey; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $originalKey |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
2 |
|
protected function findNewKey(string $originalKey): string |
|
89
|
|
|
{ |
|
90
|
2 |
|
if (true === array_key_exists($originalKey, $this->apiMapping)) { |
|
91
|
2 |
|
return $this->apiMapping[$originalKey]['newName']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
return $originalKey; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $key |
|
99
|
|
|
* @param mixed $value |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
2 |
|
protected function convertValueType(string $key, $value) |
|
103
|
|
|
{ |
|
104
|
2 |
|
$type = (true === array_key_exists($key, $this->apiMapping)) |
|
105
|
2 |
|
? $this->apiMapping[$key]['dataType'] |
|
106
|
2 |
|
: 'string'; |
|
107
|
|
|
|
|
108
|
|
|
switch ($type) { |
|
109
|
2 |
|
case 'datetime': |
|
110
|
|
|
return strtotime($value) > 0 ? date("c", strtotime($value)) : ''; |
|
111
|
2 |
|
case 'int': |
|
112
|
2 |
|
return (int) $value; |
|
113
|
2 |
|
case 'bool': |
|
114
|
2 |
|
return (bool) $value; |
|
115
|
2 |
|
case 'array': |
|
116
|
|
|
return (array) $value; |
|
117
|
2 |
|
case 'json': |
|
118
|
|
|
return json_decode($value); |
|
119
|
|
|
default: |
|
120
|
2 |
|
return $value; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|