cybex/laravel-reflector

提供数据模型的结构信息。

v1.1.1 2024-06-07 13:13 UTC

README

Latest Version on Packagist

此包允许您获取数据模型的结构信息。

需求

  • Illuminate/support: ^8.0
  • PHP: ^8.0

安装

您可以通过composer安装此包

composer require cybex/laravel-reflector

使用方法

getModelRelations()

getModelRelations() 方法返回一个包含模型所有关系的 Collection,其中包含额外的信息,如关系的名称、关系类型、相关类以及给定模型的空 Eloquent 基础实例。

ModelReflector::getModelRelations(User::class);

// Returns 

Illuminate\Support\Collection {#5236
    all: [
        "Images" => [
             "relation" => "Images",
             "returnType" => "Illuminate\Database\Eloquent\Relations\HasMany",
             "relatedClass" => "App\Models\Image",
             "relatedModel" => App\Models\Image {#5237},
             "relatedTable" => "images",
             "foreignKeyName" => "id",
             "qualifiedForeignKeyName" => "images.id",
             "isRelationParent" => false,
         ]
    ]
}

getRelationByTarget()

getRelationByTarget() 方法返回模型和目标之间的关系的名称。

$user = new User;
$image = new Image;

ModelReflector::getRelationByTarget($user, $image);

// Returns 

'Images'

hasRelation()

hasRelation() 方法返回 true,如果模型具有特定的关系。

ModelReflector::hasRelation(User::class, 'Images');

// Returns

true

getMethodReturnType()

getMethodReturnType() 方法返回给定对象或模型类中特定方法的类型。如果方法不存在,则返回 false。如果没有指定返回类型,则返回 null。

$user = new User;

ModelReflector::getMethodReturnType($user, 'Images');

// Returns 

'Illuminate\Database\Eloquent\Relations\HasMany'

getModelInstance()

getModelInstance() 方法检查给定的模型是否是模型的一个实例或模型的全限定类名,并返回模型或给定类的空 Eloquent 基础模型。

ModelReflector::getModelInstance('App\Models\User');

// Returns an empty object of the User class

getModelClass()

getModelClass() 方法检查给定的模型是否是模型的一个实例或模型的全限定类名,并返回给定模型的类。

$user = new User;

ModelReflector::getModelClass($user);

// Returns

'App\Models\User'

resolveModelObject()

resolveModelObject() 方法根据给定的模型或类和相应的标识符解析模型。如果没有提供标识符,则返回空的 Builder-Model。如果找不到所需的模型,则返回 null。

ModelReflector::resolveModelObject(Image::class, 10);

// Returns the Image object with the key 10

resolveRelatedModel()

resolveRelatedModel() 方法通过源和给定的关系解析相关模型。目前,我们只支持 HasOne 或 BelongsTo-关系,因为那些只返回单个模型或 null。

ModelReflector::resolveRelatedModel($image, 'User');

// Returns the User object that the Image object belongs to

resolveRelatedModelByTarget()

resolveRelatedModelByTarget() 方法通过源和给定的目标模型解析相关模型。

ModelReflector::resolveRelatedModelByTarget($image, User::class);

// Returns the User object that the Image object belongs to 

getModelShortName()

getModelShortName() 方法返回模型的简短名称。

$image = new Image;

ModelReflector::getModelShortName($image);

// Returns

'Image'

getAllModels()

getAllModels() 方法通过文件系统返回所有可用的模型 Collection。

ModelReflector::getAllModels();

// Returns 

Illuminate\Support\Collection {#384
    all: [
    "App\Models\User\Image",
    "App\Models\User",
    ],
}

getAllInstantiatableModels()

getAllInstantiatableModels() 方法返回所有可实例化模型类的 Collection,这些类不是抽象的。它以完全限定的类名作为键,相应的简短名称作为值。

ModelReflector::getAllInstantiatableModels();

// Returns all kinds of empty objects

Illuminate\Support\Collection {#4929
    all: [
    "App\Models\User\Image" => App\Models\User\Image {#4928},
    "App\Models\User" => App\Models\User {#4921},
    ],
}

getInstantiatableModelStructureInformation()

getInstantiatableModelStructureInformation() 方法返回所有可实例化模型类的结构信息 Collection,包括父类和子类的完全限定名称。

ModelReflector::getInstantiatableModelStructureInformation();

// Returns 

Illuminate\Support\Collection {#1469
    all: [
        "App\Models\User" => Illuminate\Support\Collection {#1482
            all: [
                "parentClass" => null,
                "childClasses" => [
                 "App\Models\User\Image",
                ],
            ],
        },
        "App\Models\User\Image" => Illuminate\Support\Collection {#1483
            all: [
                "parentClass" => "App\Models\User",
                "childClasses" => [],
            ],
        }
    ],
}

getClassFromMorphMap()

getClassFromMorphMap() 方法从形态映射别名(反向查找)返回类名,别名或 null(如果严格为 true)。

ModelReflector::getClassFromMorphMap('user');

// Returns

'App\Models\User'

getMorphAliasForClass()

getMorphAliasForClass() 方法返回指定模型的形态别名。

ModelReflector::getMorphAliasForClass('App\Models\User');

// Returns

'user'

modelHasTraits()

modelHasTraits() 方法验证模型是否实现了一个或多个特定特质。

ModelReflector::modelHasTraits(User::class, 'Illuminate\Database\Eloquent\Concerns\HasAttributes');

// Returns 

true