BaseTransformer.php
11.4 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 数据转换层基类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Controllers
* @package App\Admin\Controllers
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @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\User\User;
use App\Transformers\Enterprise\EnterpriseTransformer;
use App\Transformers\Product\ProductTransformer;
use League\Fractal\TransformerAbstract;
/**
* Class BaseTransformer
*
* @category App\Admin\Controllers
* @package App\Admin\Controllers
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class BaseTransformer extends TransformerAbstract
{
/**
* 转换内容中的路径
*
* @param string $content 内容
*
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function transformContent($content)
{
foreach ($content as &$vo) {
$vo = is_image($vo) ? splice_file_path((string)$vo) : $vo;
}
return $content;
}
/**
* 将多选的字段数组转换成中文显示,用逗号进行分割
*
* @param string $class class
* @param string $value 值
* @param string $key 键
*
* @return string
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function transformConfigSingle($class, $value, $key)
{
$config_values = Config::getConfigValueByKey($class::CONFIG_OPTIONS, $key);
$name = '' ;
if ($config_values) {
foreach ($config_values as $config_value) {
if ($value == $config_value['value']) {
$name = $config_value['name'];
}
}
}
return $name ;
}
/**
* 将多选的字段数组转换成中文显示,用逗号进行分割
*
* @param string $class class
* @param string $values 值
* @param string $key 键
*
* @return string
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function transformConfigMultiple($class, $values, $key)
{
$config_values = Config::getConfigValueByKey($class::CONFIG_OPTIONS, $key);
$name = [] ;
if ($config_values && $values) {
foreach ($values as $value) {
foreach ($config_values as $config_value) {
if ($value == $config_value['value']) {
$name[] = $config_value['name'];
}
}
}
}
if ($name) {
return implode(',', $name);
}
return '';
}
/**
* 转换作者:列表页作者转换,
*
* @param User $user 用户
*
* @param bool $followed
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function transformUserList($user, $followed = false)
{
if ($user) {
$output = [
'id' => (int) $user->id,
'username' => (string)$user->username,
'truename' => (string)$user->truename,
'mobile' => (string)$user->mobile,
'nickname' => (string)$user->nickname,
'avatar' => splice_file_path($user->avatar),
'wx_number' => (string)$user->wx_number,
'wx_avatar' => splice_file_path($user->wx_avatar),
'wx_nickname' => (string)$user->wx_nickname,
'gender' => (int)$user->gender,
'rating' => format_rating($user->rating),
'role' => (string)$user->role,
];
// 获取查询的字段
$attributes = $user->getAttributes();
// 获取key
$keys = array_keys($attributes);
$return = [];
foreach ($output as $key => $value) {
if (in_array($key, $keys)) {
$return[$key] = $value;
}
}
if (array_key_exists('gender', $return)) {
$return['gender_show'] = $this->transformConfigSingle(User::class, $user->gender, 'gender');
}
if (array_key_exists('role', $return)) {
$return['role_zh'] = (string)$user->role_zh;
$return['role_type'] = (string)$user->role_type;
}
if ($followed) {
$return['followed'] = $user->isFollowed();
}
return $return;
}
}
/**
* 转换作者
*
* @param User $user 用户
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function transformUserSimple($user)
{
if (!$user) {
return null;
}
return [
'id' => (int) $user->id,
'username' => (string)$user->username,
'name' => (string)$user->name,
'mobile' => (string)$user->mobile,
'nickname' => (string)$user->nickname,
'avatar' => splice_file_path($user->avatar),
'gender' => (int)$user->gender,
'gender_show' => (string)$user->gender_show,
'role' => (string)$user->role,
// 'role_id' => (int)$user->role->role_id,
'role_show' => (string)$user->role_show,
];
}
/**
* 转换评论
*
* @param array $comments 评论
*
* @return array
*/
public function transformComments($comments)
{
$return = [];
$transformer = new CommentTransformer();
if (!empty($comments)) {
foreach ($comments as $key => $comment) {
$return[] = $transformer->transform($comment);
}
}
return $return;
}
/**
* 转换性别
*
* @param integer $gender 性别
*
* @return string
*/
public function generateGender($gender)
{
$html = '<i class="fa fa-venus-mars ml-2"></i>';
if ($gender === 1) {
$html = '<i class="fa fa-venus ml-2"></i>';
} else {
$html = '<i class="fa fa-mars ml-2"></i>';
}
return $html;
}
/**
* 转换商家信息
*
* @param $model
* @return array
*/
public function transformEnterpriseSimple($model)
{
if (!$model) {
return null;
}
return [
'id' => (int) $model->id,
'user_id' => (int)$model->user_id,
'name' => (string)$model->name,
'logo' => splice_file_path($model->logo),
];
}
/**
* 转换商家信息
*
* @param $enterprise
* @return string
*/
public function transformEnterprise($enterprise)
{
if ($enterprise) {
return app(EnterpriseTransformer::class)->transform($enterprise);
}
}
/**
* 转换作者
*
* @param $model
* @return array
*/
public function transformCustomer($model)
{
if (!$model) {
return null;
}
return [
'id' => (int) $model->id,
/* place your other model properties here */
'user_id' => (int)$model->user_id,
'name' => (string)optional($model->user)->name,
'company_name' => (string)$model->company_name,
// 'province_code' => (string)$model->province_code,
// 'province_name' => (string)optional($model->province)->name,
// 'city_code' => (string)$model->city_code,
// 'city_name' => (string)optional($model->city)->name,
// 'district_code' => (string)$model->district_code,
// 'district_name' => (string)optional($model->district)->name,
// 'address' => (string)$model->address,
];
}
/**
* 转换产品信息
*
* @param $products
* @return array
*/
public function transformProducts($products)
{
$products = $products->map(function ($model) {
$this->transformProduct($model);
})->toArray();
return array_filter($products);
}
/**
* 转换产品信息
*
* @param $model
* @return array
*/
public function transformProductSimple($model)
{
if (!$model) {
return null;
}
return [
'id' => (int) $model->id,
/* place your other model properties here */
'user_id' => (int)$model->user_id,
'enterprise_id' => (int)$model->enterprise_id,
'series_id' => (int)$model->series_id,
// 'series' => $model->series ? app(ProductSeriesTransformer::class)->transform($model->series) : null,
// 'series_name' => optional($model->series)->name,
'category_id' => (int)$model->category_id,
'level' => (int)$model->level,
'name' => (string)$model->name,
'number' => (string)$model->number,
'specification' => (string)$model->specification,
'composition' => (string)$model->composition,
'weight' => (string)$model->weight,
'fabric_texture'=> (string)$model->fabric_texture,
'door_width' => (string)$model->door_width,
'finishing' => (string)$model->finishing,
'cover_image' => splice_file_path($model->cover_image),
];
}
/**
* 设置 通用字段
*
* @param $output
* @param $model
* @return array
*/
public function setOutputFields($output, $model): array
{
// 获取查询的字段
$attributes = $model->getAttributes();
// 获取key
$keys = array_keys($attributes);
$return = [];
foreach ($output as $key => $value) {
if (in_array($key, $keys)) {
$return[$key] = $value;
}
}
// 设置 专用字段
$this->setSpecialFields($return, $model);
// 设置 通用字段
$this->setCommonFields($return, $model);
return $return;
}
/**
* 设置 专用字段
*
* @param $return
* @param $model
*/
public function setSpecialFields(&$return, $model)
{
//
}
/**
* 设置 通用字段
*
* @param $return
* @param $model
*/
public function setCommonFields(&$return, $model)
{
$return['sort'] = (int)$model->sort;
$return['status'] = (int)$model->status;
$return['status_show'] = (string)$model->status_show;
$return['created_at'] = format_date($model->created_at);
$return['updated_at'] = format_date($model->updated_at);
}
}