AccessRecordTransformer.php 2.0 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\System;

use App\Models\Area\Area;
use App\Models\System\AccessRecord;
use App\Transformers\BaseTransformer;
use Illuminate\Support\Arr;

/**
 * Class AccessRecordTransformer
 *
 * @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 AccessRecordTransformer extends BaseTransformer
{

    public function transform(AccessRecord $model)
    {
        return [

            'id'        => (int) $model->id,

            'user_id'       => (int)$model->user_id,
            'user'          => $this->transformUserSimple($model->user),
            'accessable_type'  => (string)$model->accessable_type,
            'accessable_id'  => (string)$model->accessable_id,
            'accessable'    => $this->transformAccessable($model->accessable_type, $model->accessable),
            'created_at'    => format_date($model->created_at),
        ];
    }

    /**
     *
     * @param $type
     * @param $accessable
     * @return string
     */
    public function transformAccessable($type, $accessable)
    {
        switch ($type) {
            case Product::OBJ_NAME:
                return app(ProductTransformer::class)->transform($accessable);

        }
    }
}