FavoriteTransformer.php
5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* +--------------------------------------------------------------------------------------------------------------------
* 数据转换层: 收藏 转换类
* +--------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Transformers
* @package App\Transformers
* @author Richer <yangzi1028@163.com>
* @date 2022年1月5日10:14:05
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Transformers;
use App\Models\EpGiftCard\EpGiftCard;
use App\Models\Favorite;
use App\Models\GiftCard\GiftCard;
use App\Models\Goods\Goods;
use App\Models\Groupon\Groupon;
use App\Transformers\EpGiftCard\EpGiftCardTransformer;
use App\Transformers\GiftCard\GiftCardTransformer;
use App\Transformers\Goods\GoodsTransformer;
/**
* Class FavoriteTransformer
*
* @category App\Transformers
* @package App\Transformers
* @author Richer <yangzi1028@163.com>
* @date 2022年1月5日10:14:05
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class FavoriteTransformer extends BaseTransformer
{
public function transform(Favorite $model)
{
// $output = [
//
// 'id' => (int) $model->id,
//
// 'user_id' => (int)$model->user_id,
// 'enterprise_id' => (int)$model->enterprise_id,
//// 'product_id' => (int)$model->product_id,
// 'favoriteable_type' => (string)$model->favoriteable_type,
// 'favoriteable_id' => (string)$model->favoriteable_id,
// 'created_at' => format_date($model->created_at),
// ];
//
// if ($model->favoriteable_type === ProductColor::OBJ_NAME) {
// $records = $model->records;
// // 获取产品信息
// $product = optional(optional($records->first())->favoriteable)->product;
//// $output['product'] = $product;
// $output['product_name'] = optional($product)->name;
// $output['product_number'] = optional($product)->number;
// $output['cover_image'] = splice_file_path(optional($product)->cover_image);
// $output['series_id'] = optional($product)->series_id;
// $output['series_name'] = optional(optional($product)->series)->name;
//
// $output['records'] = $records->map(function ($record) {
// $favoriteable = $record->favoriteable;
// return [
// 'id' => $favoriteable->id,
// 'product_id' => (int)$favoriteable->product_id,
// 'color_id' => (int)$favoriteable->color_id,
// 'color_code'=> (string)$favoriteable->color_code,
// 'type' => (int)$favoriteable->type,
// 'type_show' => (string)$favoriteable->type_show,
// 'number' => (string)$favoriteable->number,
// 'name' => (string)$favoriteable->name,
// 'image' => splice_file_path($favoriteable->image),
// 'created_at' => format_date($record->created_at),
//
// ];
// });
// } else {
// $output['favoriteable'] = $this->transformFavoriteable($model->favoriteable_type, $model->favoriteable);
// }
//
// return $output;
return [
'id' => (int) $model->id,
'user_id' => (int)$model->user_id,
'favoriteable_type' => (string)$model->favoriteable_type,
'favoriteable_id' => (string)$model->favoriteable_id,
'favoriteable' => $this->transformFavoriteable($model->favoriteable_type, $model->favoriteable),
'created_at' => format_date($model->created_at),
];
}
/**
*
* @param $type
* @param $favoriteable
* @return string
*/
public function transformFavoriteable($type, $favoriteable)
{
switch ($type) {
case Goods::OBJ_NAME:
case \App\Models\PointsMall\Goods::OBJ_NAME:
case GiftCard::OBJ_NAME:
case EpGiftCard::OBJ_NAME:
return [
'id' => (int) $favoriteable->id,
'name' => (string) $favoriteable->name,
'cover_image' => splice_file_path($favoriteable->cover_image),
'price' => format_money($favoriteable->price),
];
return app(GoodsTransformer::class)->transform($favoriteable);
// case \App\Models\PointsMall\Goods::OBJ_NAME:
// return app(\App\Transformers\PointsMall\GoodsTransformer::class)->transform($favoriteable);
// case GiftCard::OBJ_NAME:
// return app(GiftCardTransformer::class)->transform($favoriteable);
// case EpGiftCard::OBJ_NAME:
// return app(EpGiftCardTransformer::class)->transform($favoriteable);
case \App\Models\Groupon\Goods::OBJ_NAME:
return [
'id' => (int) $favoriteable->id,
'name' => (string) $favoriteable->goods_name,
'cover_image' => splice_file_path($favoriteable->goods_image),
'price' => format_money($favoriteable->goods_price),
];
return app(\App\Transformers\Groupon\GoodsTransformer::class)->transform($favoriteable);
}
}
}