cloudbear/class-mapper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cloudbear/class-mapper

最新稳定版本:v1.1.1

Composer 安装命令:

composer require cloudbear/class-mapper

包简介

README 文档

README

Allows any array structure to be mapped to a class object.

Example

use Cloudbear\Common\Models\AutoResolvingModel;

class MyObject extends AutoResolvingModel
{
    public string $hello;
}

new MyObject(['hello' => 'world'])->hello //-> (string) world

Supported Types

  • Primitives
  • DateTime
  • stdClass (used when resulting value is unknown structure.)
  • BackedEnum
  • Any other AutoResolvingModel or AutoResolvingArrayModel class

DateTime

The date format is adjustable via static::DATE_FORMAT.

You can parse any int into a datetime as well:

use Cloudbear\Common\Models\AutoResolvingModel;

class MyObject extends AutoResolvingModel
{
    public DateTime $timestamp;
}

new MyObject(['timestamp' => 1751446865])->timestamp //-> (DateTime) 2025-07-02T09:01:05Z 

Array of Models

class MyObjectList extends AutoResolvingArrayModel
{
    /**
     * This const is used to determine the api property that's representing the array of objects. Default: 'items'.
     */
    protected const string LIST_KEY = 'objects';

    protected function getItemClass(): string
    {
        return MyObject::class;
    }
}

new MyObjectList(['objects' => [['hello' => 'world']]])->items //-> MyObject[]

Changing property name

Keys are case-sensitive, so if you're changing cases, you'll need to map app your properties.

use Cloudbear\ClassMapper\Attributes\Key;
use Cloudbear\Common\Models\AutoResolvingModel;

class MyObject extends AutoResolvingModel
{
    #[Key('hello')]
    public string $foo;
}

new MyObject(['hello' => 'world'])->foo //-> (string) world

Excluding properties

The parser expects all properties to be part of the data that you are providing.

If you want certain properties to be ignored you can add the Exclude attribute.

use Cloudbear\ClassMapper\Attributes\Exclude;
use Cloudbear\Common\Models\AutoResolvingModel;

class MyObject extends AutoResolvingModel
{
    #[Exclude]
    public string $foo;
    
    #[Exclude]
    public string $hello;
}

new MyObject(['hello' => 'world'])->foo //-> Uninitialized property exception

统计信息

  • 总下载量: 388
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-02