postfriday/castable 问题修复 & 功能扩展

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

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

postfriday/castable

最新稳定版本:1.0.2

Composer 安装命令:

composer require postfriday/castable

包简介

Castable – Flexible Attribute-Based Data Casting for PHP

README 文档

README

Castable is a lightweight and extensible PHP library that enables seamless attribute-based data transformation and casting. Designed for modern PHP applications, it allows you to define custom casters via attributes in constructor arguments, ensuring precise and automatic data formatting.

Features

  • ✅ Attribute-Based Casting – Define how your properties should be formatted directly in the constructor using PHP attributes.
  • ✅ Extensible Casters – Easily create and register custom casters for different data types.
  • ✅ Seamless Object to Array Conversion – Automatically apply transformations when converting objects to arrays.
  • ✅ Support for DateTime, Boolean, Enum, and Custom Formats – Flexible handling of various data types.
  • ✅ PSR-Friendly & Framework-Agnostic – Works with any PHP project, including Laravel, Symfony, and standalone applications.

Example Usage

use Postfriday\Castable\Traits\ToArray;
use Postfriday\Castable\Attributes\CastWith;
use Postfriday\Castable\Casters\DateTimeCaster;
use Postfriday\Castable\Casters\BooleanCaster;

class Example
{
    use ToArray;

    public DateTimeInterface $createdAt;
    public DateTimeInterface $updatedAt;
    public bool $isActive;

    public function __construct(
        #[CastWith(DateTimeCaster::class, ['d.m.Y H:i'])] 
        DateTimeInterface $createdAt,

        #[CastWith(DateTimeCaster::class, ['Y-m-d H:i:s'])]
        DateTimeInterface $updatedAt,

        #[CastWith(BooleanCaster::class)]
        bool $isActive
    ) {
        $this->createdAt = $createdAt;
        $this->updatedAt = $updatedAt;
        $this->isActive = $isActive;
    }
}

$example = new Example(
    new \DateTime('2024-02-21 12:00:00'),
    new \DateTime('2025-02-21 18:30:00'),
    true
);

print_r($example->toArray());

Installation

composer require postfriday/castable

Why Use Castable?

If you're tired of manually formatting your objects when converting them to arrays, Castable provides a clean, declarative, and reusable way to handle data transformation.

👉 Simplify your PHP data handling with Castable! 🚀

统计信息

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

GitHub 信息

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

其他信息

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