PHPackages                             postfriday/castable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. postfriday/castable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

postfriday/castable
===================

Castable – Flexible Attribute-Based Data Casting for PHP

1.0.2(1y ago)09MITPHP

Since Feb 21Pushed 1y ago1 watchersCompare

[ Source](https://github.com/postfriday/castable)[ Packagist](https://packagist.org/packages/postfriday/castable)[ RSS](/packages/postfriday-castable/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (2)Used By (0)

Castable – Flexible Attribute-Based Data Casting for PHP
========================================================

[](#castable--flexible-attribute-based-data-casting-for-php)

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
--------

[](#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 &amp; Framework-Agnostic – Works with any PHP project, including Laravel, Symfony, and standalone applications.

Example Usage
-------------

[](#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
------------

[](#installation)

```
composer require postfriday/castable
```

Why Use 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! 🚀

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance43

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

451d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bdc36553b05fd63f025c10d8d7e9218d8dc01a8a9efaca739d5e8f3c76d0e154?d=identicon)[WickedIxil](/maintainers/WickedIxil)

---

Top Contributors

[![postfriday](https://avatars.githubusercontent.com/u/888136?v=4)](https://github.com/postfriday "postfriday (21 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/postfriday-castable/health.svg)

```
[![Health](https://phpackages.com/badges/postfriday-castable/health.svg)](https://phpackages.com/packages/postfriday-castable)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
