1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash\Http\Resources; |
6
|
|
|
|
7
|
|
|
use LogicException; |
8
|
|
|
use Longman\LaravelLodash\Support\Str; |
9
|
|
|
|
10
|
|
|
use function call_user_func_array; |
11
|
|
|
use function get_class; |
12
|
|
|
use function in_array; |
13
|
|
|
use function is_array; |
14
|
|
|
use function key; |
15
|
|
|
use function method_exists; |
16
|
|
|
|
17
|
|
|
trait TransformsData |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Fields transform mapping. |
21
|
|
|
* In array key should be a column name from database, |
22
|
|
|
* value can be just name (if getter exists for that name, or array [fieldName => getterMethod]). |
23
|
|
|
* If static getterMethod is defined in the resource class, it will be called and as a first argument will be passed TransformableContract $model, |
24
|
|
|
* Otherwise, model's method will be used. |
25
|
|
|
*/ |
26
|
|
|
protected static array $transformMapping = []; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Fields list for hiding in output. |
30
|
|
|
* Array values should be a column name from database, |
31
|
|
|
*/ |
32
|
|
|
protected static array $hideInOutput = []; |
33
|
|
|
|
34
|
3 |
|
public static function getTransformFields(): array |
35
|
|
|
{ |
36
|
3 |
|
return static::$transformMapping; |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
public static function getHideInOutput(): array |
40
|
|
|
{ |
41
|
3 |
|
return static::$hideInOutput; |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
public static function transformToApi(TransformableContract $model): array |
45
|
|
|
{ |
46
|
3 |
|
$fields = static::getTransformFields(); |
47
|
3 |
|
$hiddenProperties = $model->getHidden(); |
48
|
3 |
|
$hideInOutput = static::getHideInOutput(); |
49
|
3 |
|
$transformed = []; |
50
|
3 |
|
foreach ($fields as $internalField => $transformValue) { |
51
|
3 |
|
if (in_array($internalField, $hiddenProperties, true)) { |
52
|
1 |
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
if (in_array($internalField, $hideInOutput, true)) { |
56
|
1 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
[$key, $value] = self::parseKeyValue($internalField, $transformValue, $model); |
60
|
|
|
|
61
|
3 |
|
$transformed[$key] = $value; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $transformed; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
public static function transformToInternal(array $fields): array |
68
|
|
|
{ |
69
|
3 |
|
$modelTransformedFields = []; |
70
|
3 |
|
foreach (self::getTransformFields() as $key => $transformField) { |
71
|
3 |
|
if (is_array($transformField)) { |
72
|
3 |
|
$modelTransformedFields[key($transformField)] = $key; |
73
|
|
|
} else { |
74
|
3 |
|
$modelTransformedFields[$transformField] = $key; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
$transformed = []; |
79
|
3 |
|
foreach ($fields as $fieldKey => $postValue) { |
80
|
3 |
|
if (isset($modelTransformedFields[$fieldKey])) { |
81
|
3 |
|
$transformed[$modelTransformedFields[$fieldKey]] = $postValue; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
return $transformed; |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
private static function parseKeyValue(string $internalField, $transformValue, TransformableContract $model): array |
89
|
|
|
{ |
90
|
3 |
|
if (is_array($transformValue)) { |
91
|
3 |
|
$key = key($transformValue); |
92
|
3 |
|
$method = $transformValue[$key]; |
93
|
3 |
|
if (method_exists(static::class, $method)) { // Check if getter exists in the resource class |
94
|
|
|
$value = call_user_func_array([static::class, $method], ['model' => $model]); |
95
|
3 |
|
} elseif (method_exists($model, $method)) { // Check if getter exists in the model class |
96
|
3 |
|
$value = $model->$method(); |
97
|
|
|
} else { |
98
|
3 |
|
throw new LogicException('Method ' . $method . ' does not available not for resource ' . static::class . ', not for model ' . get_class($model)); |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
// Try to find getter for external field |
102
|
3 |
|
$method = 'get' . Str::snakeCaseToCamelCase($transformValue); |
103
|
3 |
|
if (method_exists($model, $method)) { |
104
|
3 |
|
$key = $transformValue; |
105
|
3 |
|
$value = $model->$method(); |
106
|
|
|
} else { |
107
|
|
|
// Call getter for internal field |
108
|
|
|
$method = 'get' . Str::snakeCaseToCamelCase($internalField); |
109
|
|
|
if (! method_exists($model, $method)) { |
110
|
|
|
throw new LogicException('Field ' . $internalField . ' getter (' . $method . ') does not available for model ' . get_class($model)); |
111
|
|
|
} |
112
|
|
|
$key = $transformValue; |
113
|
|
|
$value = $model->$method(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
return [$key, $value]; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|