CouponsUserBagsTransformer.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. *
  4. *
  5. * @category xxx
  6. * @package PSR
  7. * @subpackage Documentation\API
  8. * @author xxx <xxx@xxx.com>
  9. * @license GPL https://xxx.com
  10. * @link https://xxx.com
  11. * @ctime: 2020/4/7 13:55
  12. */
  13. namespace App\Transformers;
  14. use App\Models\Coupon;
  15. use App\Models\CouponsUserBag;
  16. use Carbon\Carbon;
  17. use League\Fractal\TransformerAbstract;
  18. class CouponsUserBagsTransformer extends TransformerAbstract
  19. {
  20. public function transform(CouponsUserBag $model){
  21. $coupon = $model->coupons_data;
  22. $valid_days_end_time = null;
  23. if($model->valid_type == CouponsUserBag::VALID_TYPE_RELATIVE){
  24. $valid_days_end_time = Carbon::make($model->created_at)->addDays($model->valid_days)->format('Y-m-d');
  25. }
  26. return [
  27. 'id' => $model->id,
  28. 'get_type_name' => CouponsUserBag::$typeMaps[$model->type],
  29. 'type' => $coupon['type'],
  30. 'type_name' => Coupon::$typeMaps[$coupon['type']],
  31. 'valid_type_name' => CouponsUserBag::$validTypeMaps[$model->valid_type],
  32. 'valid_type' => $model->valid_type,
  33. 'valid_start_time' => Carbon::make($model->valid_start_time)->format('Y-m-d'),
  34. 'valid_end_time' => Carbon::make($model->valid_end_time)->format('Y-m-d'),
  35. 'valid_days' => $model->valid_days,
  36. 'valid_days_end_time' => $valid_days_end_time,
  37. 'created_at' => Carbon::make($model->created_at)->format('Y-m-d'),
  38. 'with_amount' => $coupon['with_amount'],
  39. 'used_amount' => $coupon['used_amount'],
  40. 'discount' => $coupon['discount']/10,
  41. 'order_type' => $model->order_type,
  42. 'order_type_name' => CouponsUserBag::$orderTypeMaps[$model->order_type],
  43. ];//$model->toArray();
  44. }
  45. }