PHPackages                             kalimero/readonly-array-support - 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. kalimero/readonly-array-support

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

kalimero/readonly-array-support
===============================

Laravel-style support for immutable toReadonlyArray pattern in DTOs and Resources.

00PHP

Since Jun 26Pushed 10mo agoCompare

[ Source](https://github.com/KalimeroMK/readonly-array-support)[ Packagist](https://packagist.org/packages/kalimero/readonly-array-support)[ RSS](/packages/kalimero-readonly-array-support/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

📦 Kalimero\\ReadonlyArraySupport
================================

[](#-kalimeroreadonlyarraysupport)

A lightweight PHP package for Laravel-style `toReadonlyArray()` support — perfect for `DTO` (Data Transfer Objects), `JsonResource`, Jobs, Events, or any class where you want to output an **immutable array** version of an object.

✨ Features
----------

[](#-features)

- ✅ `ToReadonlyArray` interface for consistent API
- ✅ `CastsToReadonlyArray` trait that automatically converts public readonly properties into array
- ✅ Built for PHP 8.2+ and Laravel-style architecture
- ✅ Ideal for Clean Architecture &amp; modular DTO-based apps
- ✅ Zero dependencies – ultra lightweight

---

🔧 Installation
--------------

[](#-installation)

```
composer require kalimero/readonly-array-support
```

> ⚠️ Requires PHP &gt;= 8.2

---

🚀 Usage
-------

[](#-usage)

### 1. Create a DTO

[](#1-create-a-dto)

```
use Kalimero\ReadonlyArray\Contracts\ToReadonlyArray;
use Kalimero\ReadonlyArray\Traits\CastsToReadonlyArray;

readonly class ProductDTO implements ToReadonlyArray
{
    use CastsToReadonlyArray;

    public function __construct(
        public int $id,
        public string $name,
        public float $price,
    ) {}
}
```

---

### 2. Use in your application

[](#2-use-in-your-application)

```
$dto = new ProductDTO(1, 'T-Shirt', 29.99);

// Get as immutable array
$data = $dto->toReadonlyArray();

// Pass to Eloquent create
Product::create($data);
```

---

### 3. Works great for:

[](#3-works-great-for)

- ✅ FormRequest -&gt; DTO -&gt; Model
- ✅ Event Broadcasting
- ✅ Logging/Audit trails
- ✅ Queue Jobs
- ✅ JsonResource transformation

---

💡 Why `toReadonlyArray`?
------------------------

[](#-why-toreadonlyarray)

Laravel supports `Arrayable`, `Responsable`, `JsonSerializable`, etc. But sometimes you want to pass **clean, readonly arrays** between layers (DTOs, Jobs, APIs). This package gives you:

- ✅ Consistent, immutable snapshot of data
- ✅ DRY code – no more `fromArray()` or manual mapping
- ✅ Works beautifully with Laravel mass-assignment

---

🧪 Testing
---------

[](#-testing)

```
vendor/bin/phpunit
```

---

📄 License
---------

[](#-license)

MIT License © 2025 [KalimeroMK](https://github.com/KalimeroMK)

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/472c4da0220f15747dd81d3a27ffbba022a06ab20f64a367ddcaacd790d812d7?d=identicon)[KalimeroMK](/maintainers/KalimeroMK)

---

Top Contributors

[![KalimeroMK](https://avatars.githubusercontent.com/u/24772657?v=4)](https://github.com/KalimeroMK "KalimeroMK (3 commits)")

### Embed Badge

![Health badge](/badges/kalimero-readonly-array-support/health.svg)

```
[![Health](https://phpackages.com/badges/kalimero-readonly-array-support/health.svg)](https://phpackages.com/packages/kalimero-readonly-array-support)
```

PHPackages © 2026

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