andrew-gos/class-builder 问题修复 & 功能扩展

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

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

andrew-gos/class-builder

Composer 安装命令:

composer require andrew-gos/class-builder

包简介

Advanced library for building objects from arrays or other types

README 文档

README

Version 1.3.0

Description

This library provides functionality to build objects from arrays or scalars.

The library allows the construction of interfaces, abstract classes, arrays of objects. It also supports the construction of object parameters whose type is an array with typed elements. There is the possibility to build variadic parameters from arrays. Additionally, you can build classes with a single required parameter from scalar values. Parameters can also be assembled where the type is an intersection or union of types.

The library gathers information about object parameters from the constructor's parameters.

Contacts

If you encounter a bug or have an idea, please write to Gostev71@outlook.com and type Telegram Library Bug or Telegram Library Idea in the email header.

Installation

To install the library, use Composer:

composer require andrew-gos/class-builder

Simple example

To build an object, write:

<?php

use AndrewGos\ClassBuilder\ClassBuilder;

class A
{
    public function __construct(
        private int $a,
    ) {
    }
}

$classBuilder = new ClassBuilder();
$a = $classBuilder->build(A::class, ['a' => 1]);

Building abstract classes and interfaces

To build an interface or an abstract class, specify the available inheritors using the AvailableInheritors attribute.
The first successfully built object will be returned.
This attribute can take array of inheritors. \

Note, that if you make class parameter type as intersection of types, then this library will build inheritors, which contained in every interface or class in type

<?php

use AndrewGos\ClassBuilder\Attribute\AvailableInheritors;

#[AvailableInheritors([B::class, C::class])]
interface A {}

class B implements A {}

class C implements A {}

Data checkers for building

To control the building of interfaces or abstract classes, you can use the BuildIf attribute for the inheriting classes.
This attribute instructs the builder to check the data before building. If the check fails, the builder will not construct the object.
BuildIf attribute can take object of CheckerInterface attribute.

<?php

use AndrewGos\ClassBuilder\Attribute\AvailableInheritors;
use AndrewGos\ClassBuilder\Attribute\BuildIf;
use AndrewGos\ClassBuilder\Checker\FieldIsChecker;

#[AvailableInheritors([B::class, C::class])]
interface A {}

#[BuildIf(new FieldIsChecker('type', 'b'))]
class B implements A {}

#[BuildIf(new FieldIsChecker('type', 'a'))]
class C implements A {}

In this example, an object of class B will be built only if the data contains a field type equal to b.

Building typed arrays

To build typed arrays, use the ArrayType attribute. The attribute can take one type, an array of types, or an ArrayType object as a parameter.

<?php

use AndrewGos\ClassBuilder\Attribute\ArrayType;

class A
{
    public function __construct(
        #[ArrayType('int')] private array $a,
    ) {
    }
}

Building objects from scalars

If an object has only one required parameter, the library allows building such objects from scalar values. You can use the CanBeBuiltFromScalar attribute.

<?php

use AndrewGos\ClassBuilder\Attribute\CanBeBuiltFromScalar;
use AndrewGos\ClassBuilder\ClassBuilder;

#[CanBeBuiltFromScalar]
class A
{
    public function __construct(
        private int $a,
    ) {
    }
}

class B
{
    public function __construct(
        private int $a,
    ) {
    }
}

$classBuilder = new ClassBuilder();
$a = $classBuilder->build(A::class, 1);
$a = $classBuilder->build(B::class, 1);

Get values from fields with custom names

If you want, you can redefine the field from which the value for the object property assembly is taken.
To do it, use Field attribute.

<?php

use AndrewGos\ClassBuilder\ClassBuilder;
use AndrewGos\ClassBuilder\Attribute\Field;

class A
{
    public function __construct(
        #[Field('b')] private int $a,
    ) {
    }
}

$classBuilder = new ClassBuilder();
$a = $classBuilder->build(A::class, ['b' => 1]);

andrew-gos/class-builder 适用场景与选型建议

andrew-gos/class-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.99k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 andrew-gos/class-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 andrew-gos/class-builder 我们能提供哪些服务?
定制开发 / 二次开发

基于 andrew-gos/class-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-03