MorphMapTrait.php 3.9 KB
<?php
/**
 +-----------------------------------------------------------------------------------------------------------------------
 * 多态关联 trait
 +-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Models\Traits
 * @package   App\Models\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年7月28日,14:18:25
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Models\Traits;

use App\Models\Chat\ChatRecordItem;
use App\Models\RechargeOrder;
use App\Models\User\Admin;
use App\Models\User\User;
use App\Models\User\WithdrawRecord;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;

/**
 * Trait MorphMapTrait
 *
 * @category  App\Models\Traits
 * @package   App\Models\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年7月28日,14:18:25
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */

trait MorphMapTrait
{
    public static function boot()
    {
        parent::boot();

        self::bootEloquentMorphs();
    }

    /**
     * 自定义多态关联的类型字段
     */
    private static function bootEloquentMorphs()
    {
        Relation::morphMap(self::getMorphTypeOptions());
    }

    protected static function initModel()
    {
        // 设置模型
        return [
        ];
    }

    /**
     * 获取 type options
     *
     * @return array
     */
    public static function getMorphTypeOptions()
    {
        return [
            User::OBJ_NAME     => User::class,
            RechargeOrder::OBJ_NAME => RechargeOrder::class,
            ChatRecordItem::OBJ_NAME => ChatRecordItem::class,
            WithdrawRecord::OBJ_NAME => WithdrawRecord::class,
        ];

        $classes = self::initModel();
        $rs = [];
        if ($classes) {
            foreach ($classes as $class) {
                $rs [$class::OBJ_NAME] =  $class::OBJ_NAME_ZH;
            }
        }

        return $rs;
        return [
            User::OBJ_NAME  => User::class,
        ];
    }

    /**
     * 获取 type options
     *
     * @return array
     */
    public static function getMorphTypeZHOptions()
    {
        return [
            User::OBJ_NAME     => User::OBJ_NAME_ZH,
            RechargeOrder::OBJ_NAME => RechargeOrder::OBJ_NAME_ZH,
            ChatRecordItem::OBJ_NAME => ChatRecordItem::OBJ_NAME_ZH,
            WithdrawRecord::OBJ_NAME => ChatRecordItem::OBJ_NAME_ZH,
        ];
        $classes = self::initModel();
        $rs = [];
        if ($classes) {
            foreach ($classes as $class) {
                $rs [$class::OBJ_NAME] =  $class::OBJ_NAME_ZH;
            }
        }

        return $rs;
        return [
            User::OBJ_NAME  => User::OBJ_NAME_ZH,
        ];
    }

    /**
     * 获取多态关联的类
     *
     * @param $class
     * @return string
     */
    public static function getMorphToTypes($class)
    {
        return Arr::get(array_flip(self::getMorphTypeOptions()), $class);
    }

    /**
     * 获取多态关联的类
     *
     * @param string $type
     * @return string
     */
    public static function getMorphToClass($type = '')
    {
        return Arr::get(self::getMorphTypeOptions(), $type);
    }

    /**
     * 获取多态关联的类数组
     *
     * @return array
     */
    public static function getMorphToClasses()
    {
        return array_values(self::getMorphTypeOptions());
    }

    /**
     * 获取 type options
     *
     * @param $type
     * @param null $default
     * @return array
     */
    public static function getMorphTypeZH($type, $default = null)
    {
        return Arr::get(self::getMorphTypeZHOptions(), $type, $default);
    }
}