Skip to content

Commit 15516d5

Browse files
committed
Add internal mapping for custom update fields
1 parent 80dc21a commit 15516d5

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/Lodash/Http/Resources/TransformsData.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use LogicException;
88
use Longman\LaravelLodash\Support\Str;
99

10+
use function array_replace;
1011
use function call_user_func_array;
1112
use function in_array;
1213
use function is_array;
@@ -17,16 +18,22 @@ trait TransformsData
1718
{
1819
/**
1920
* Fields transform mapping.
20-
* In array key should be a column name from database,
21+
* In array key should be a column name from the database,
2122
* value can be just name (if getter exists for that name, or array [fieldName => getterMethod]).
2223
* If static getterMethod is defined in the resource class, it will be called and as a first argument will be passed TransformableContract $model,
23-
* Otherwise, model's method will be used.
24+
* Otherwise, the model's method will be used.
2425
*/
2526
protected static array $transformMapping = [];
2627

28+
/**
29+
* Fields list for merging with general mapping during transformation o internal. Used for updating some fields.
30+
* Array values should be a column name from the database.
31+
*/
32+
protected static array $internalMapping = [];
33+
2734
/**
2835
* Fields list for hiding in output.
29-
* Array values should be a column name from database,
36+
* Array values should be a column name from the database.
3037
*/
3138
protected static array $hideInOutput = [];
3239

@@ -35,6 +42,11 @@ public static function getTransformFields(): array
3542
return static::$transformMapping;
3643
}
3744

45+
public static function getInternalFields(): array
46+
{
47+
return static::$internalMapping;
48+
}
49+
3850
public static function getHideInOutput(): array
3951
{
4052
return static::$hideInOutput;
@@ -65,8 +77,11 @@ public static function transformToApi(TransformableContract $model): array
6577

6678
public static function transformToInternal(array $fields): array
6779
{
80+
$transformFields = static::getTransformFields();
81+
$transformFields = array_replace($transformFields, static::getInternalFields());
82+
6883
$modelTransformedFields = [];
69-
foreach (static::getTransformFields() as $key => $transformField) {
84+
foreach ($transformFields as $key => $transformField) {
7085
if (is_array($transformField)) {
7186
$modelTransformedFields[key($transformField)] = $key;
7287
} else {

0 commit comments

Comments
 (0)