承接 phauthentic/attribute-serializer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

phauthentic/attribute-serializer

Composer 安装命令:

composer require phauthentic/attribute-serializer

包简介

README 文档

README

PHP >= 8.1 phpstan Level 8 License: MIT

Serializer class for extracting data from objects annotated with the #[Serialize] attribute.

This class provides a method, serialize, to transform an object into an associative array by extracting data from its properties and constants based on the #[Serialize] attribute.

Installation

composer require phauthentic/attribute-serializer

How to use it?

Add the #[Serialize()] attribute to the property or constant. You can rename the property in the resulting array by providing a name to the attribute #[Serialize('other-name')].

class Example {
    #[Serialize('username')]
    private $name = 'serializer';
}

var_dump((new Serializer())->serialize(new Example()));
[
    'username' => 'serializer'
]

Dot notation for deep arrays

Field names can be dynamically renamed, even into deeper array structures, by using the dot notation.

class Example2 {
    #[Serialize('first.second')]
    private $name = 'serializer';
}

var_dump((new Serializer())->serialize(new Example2());
[
    'first' => [
        'second' => 'serializer'
    ]
]

ToArrayTrait

class Example3 {
    use ToArrayTrait;

    #[Serialize('username')]
    private $name = 'serializer';
}

var_dump((new Example3)->toArray());
[
    'username' => 'serializer'
]

License

Copyright Florian Krämer

Licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-25