mackrais-organization/property-transform 问题修复 & 功能扩展

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

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

mackrais-organization/property-transform

最新稳定版本:v1.0.0

Composer 安装命令:

composer require mackrais-organization/property-transform

包简介

A PHP library for transforming DTO properties using attributes with support for PHP functions, DI services, and nested object transformation.

README 文档

README

Scrutinizer Quality Score Build Status License Latest Stable Version Latest Unstable Version CodeCov StyleCI Gitter Total Downloads Monthly Downloads Daily Downloads PHP Version Require

Overview

The Property Transform library provides a powerful way to automatically transform DTO properties using PHP attributes.
This allows you to apply transformations such as trimming, formatting, and sanitization directly on properties, while also supporting nested object transformation and dependency injection.

🔥 Features

  • Transform DTO properties using PHP attributes
  • Supports multiple transformations per property (e.g., trim + lowercase)
  • Applies transformations to nested objects automatically
  • Supports callable PHP functions (trim, strtolower, etc.)
  • Works with Dependency Injection (DI) for custom transformations
  • Supports class methods as transformers ([ClassName::class, 'method'])
  • Lightweight & efficient – no runtime overhead

📌 Table of Contents

🛠 Installation

Install via Composer:

composer require mackrais-organization/property-transform

🚀 Usage

1️⃣ Basic DTO Transformation

You can use built-in PHP functions as transformers by adding attributes:

use MackRais\PropertyTransform\Transform;

class UserDto
{
    #[Transform('trim')]
    #[Transform('strtolower')]
    public string $name;

    #[Transform('intval')]
    public string $age;
}

Then, apply transformations:

$dto = new UserDto();
$dto->name = '  John Doe ';
$dto->age = '25';

$dataTransformer = new DataTransformer(new TransformerFactory($container));
$dataTransformer->transform($dto);

echo $dto->name; // Output: "john doe"
echo $dto->age; // Output: 25 (converted to integer)

🎯 Examples

2️⃣ Nested DTO Transformation

If a DTO contains another DTO, transformations will apply recursively:

class AddressDto
{
    #[Transform('trim')]
    #[Transform('strtolower')]
    public string $city;
}

class UserDto
{
    #[Transform('trim')]
    #[Transform('strtolower')]
    public string $name;

    #[Transform] // Required for nested transformation
    public AddressDto $address;
}

Usage:

$address = new AddressDto();
$address->city = '  New York ';

$dto = new UserDto();
$dto->name = '  Jane Doe ';
$dto->address = $address;

$dataTransformer->transform($dto);

echo $dto->name; // Output: "jane doe"
echo $dto->address->city; // Output: "new york"

3️⃣ Using Class Methods as Transformers

You can also define a custom transformer method inside a class:

class SecuritySanitizer
{
    public function sanitize(?string $input): string
    {
        return strip_tags((string) $input);
    }
}

And use it as a transformer:

class UserDto
{
    #[Transform([SecuritySanitizer::class, 'sanitize'])]
    public string $bio;
}

🏗 Dependency Injection Support

Transformers can be registered as public services in Symfony or any DI container.

Example using PSR-11 Container:

use Psr\Container\ContainerInterface;

$container = new SomePsr11Container();
$factory = new TransformerFactory($container);
$dataTransformer = new DataTransformer($factory);

Now, any service-based transformer (like SecuritySanitizer) will be automatically resolved.

📜 License

Property Transform is released under the MIT License. See the LICENSE.md file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-15