FavoriteTransformer.php 5.5 KB
<?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);

        }
    }
}