PHPackages                             earc/cast - 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. [Framework](/categories/framework)
4. /
5. earc/cast

ActiveLibrary[Framework](/categories/framework)

earc/cast
=========

eArc - the explicit architecture framework - cast component

1.1(5y ago)015MITPHPPHP &gt;=8.0

Since Feb 24Pushed 5y ago1 watchersCompare

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

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

eArc-cast
=========

[](#earc-cast)

earc/cast is a lightweight zero dependencies flat data mapper to cast data represented by objects/arrays onto other objects/arrays. Thus helping to implement the adapter pattern in nearly no time.

It uses property reflection to access the objects and supports user defined mappings.

earc/cast can be used everywhere without dependency injection even in vanilla functions.

table of contents
-----------------

[](#table-of-contents)

- [install](#install)
- [bootstrap](#bootstrap)
- [basic usage](#basic-usage)
    - [array to array](#array-to-array)
    - [array to object](#array-to-object)
    - [object to array](#object-to-array)
    - [object to object](#object-to-object)
    - [reverse casting](#reverse-casting)
    - [generate mapping](#generate-mapping)
- [advanced usage](#advanced-usage)
- [architectural considerations](#architectural-considerations)
- [releases](#releases)
    - [release 0.0](#release-00)
    - [release 1.0](#release-10)
    - [release 1.1](#release-11)

install
-------

[](#install)

```
$ composer require earc/cast
```

bootstrap
---------

[](#bootstrap)

Place the following code snippets in the section where your script/framework is bootstrapped.

1. Make use of the composer namespace driven autoloading.

```
require_once '/path/to/your/vendor/autoload.php';
```

2. Then bootstrap earc/cast to declare the `cast`, `cast_simple` and `cast_reverse`functions.

```
use eArc\Cast\Initializer;

Initializer::init();
```

basic usage
-----------

[](#basic-usage)

earc/cast supports four types of casting: [array to array](#array-to-array), [array to object](#array-to-object), [object to array](#object-to-array) and [object to object](#object-to-object).

### array to array

[](#array-to-array)

If the mapping is omitted, values with identical keys (property names) are assigned from the origin to the target.

```
use function eArc\Cast\cast_simple;

$origin = [10, 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D'];
$target = [1, 2, 3, 'd' => 'X'];

$result = cast_simple($origin, $target);

// $result = [10, 2, 3, 'd' => 'D']
```

In the case of a supplied mapping the keys of the mapping are used for the origin, and the values are used for the target.

Hint: If the value is null, the key is used as value.

```
use function eArc\Cast\cast_simple;

$origin = [10, 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D'];
$target = [1, 2, 3, 'd' => 'X'];
$mapping = ['a' => null, 'c' => 'c', 'b' => 'd'];

$result = cast_simple($origin, $target, $mapping);

// $result = [1, 2, 3, 'a' => 'A', 'd' => 'B', 'c' => 'C'];
```

Only if a mapping is supplied not existing keys will be created in the target array.

Hint: If a mapping is supplied identical keys (property names) that are not present in the mapping are not used to cast values.

### array to object

[](#array-to-object)

If the target is an object you can either supply a real object or a class name. In the case of a class name the object is created via reflection without calling the constructor.

```
use function eArc\Cast\cast_simple;
class MyTargetsParent
{
    public string $a = 'parent_A';
    protected string $b = 'parent_B';
    private string $c = 'parent_C';
    public string $d = 'parent_D';
    protected string $e = 'parent_E';
    private string $f = 'parent_F';
}
class MyTarget extends MyTargetsParent
{
    public function __construct() {
        $this->b = 'constructed_B';
    }
    public string $a = 'A';
    protected string $b = 'B';
    private string $c = 'C';
}

$origin = ['a' => 'array_A', 'c' => 'array_C', 'e' => 'array_E', 'f' => 'array_F'];
$target = new MyTarget();

$result = cast_simple($origin, $target);

// result:
// MyTargetsParent...
//    public string $a = 'array_A';  null, 'b' => null, 'c' => null, 'd' => null, 'e' => null, 'f' => null];

$result = cast_simple($origin, $target);

// $result = ['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'parent_D', 'e' => 'parent_E', 'f' => 'parent_F']
```

### object to object

[](#object-to-object)

All that was said about objects in the [array to object](#array-to-object) and [object to array](#object-to-array) cases applies.

### reverse casting

[](#reverse-casting)

Sometimes you need to map some properties apply a service or two and map the object back to the original data representation.

If the target is an object, you can use reverse casting to keep track of the origin and the mapping. All you have to do is use `cast` instead of `simple_cast`.

```
use function eArc\Cast\cast;
use function eArc\Cast\cast_reverse;

cast($origin, $target, $mapping);

//.. do something with the target;

$origin = cast_reverse($target);
```

Hint: If you call `cast` multiple times with the **same** target object, the origin and mapping of the last call is used by `cast_reverse`.

### generate mapping

[](#generate-mapping)

A common use case is to use earc/cast to generate a json representation for a frontend component using all public, protected and private properties. Unfortunately earc/cast uses the target to auto generate a mapping, thus casting an object to an empty array leaves the array empty. The `generate_mapping` function is the solution.

```
use function eArc\Cast\cast_simple;
use function eArc\Cast\generate_mapping;

return json_encode(cast_simple($object, [], generate_mapping($object)));
```

advanced usage
--------------

[](#advanced-usage)

To overwrite oder rewrite any logic, you have to pass a class implementing the `CastServiceInterface` to the `Initializer`. You may extend the original CastService.

```
use eArc\Cast\CastService;
use eArc\Cast\Initializer;

class MyCastService extends CastService
{
    // extend/rewrite logic
}

Initializer::init(new MyCastService());
```

architectural considerations
----------------------------

[](#architectural-considerations)

If you follow the dependency inversion principle in your software design, application modules does not depend directly on one another, but on abstractions. This is better than hard coupled components but leaves the problem that the modules have to know and use the same abstraction. You cannot develop components independently, and you cannot reuse components freely.

Think of a slider in a shop that slides product boxes. Let the slider be a part of a content management system that integrates in a shop platform. Then the product box slider introduces a dependency of the content management system from the product selling platform of the shop. That is why you cannot evolve the two big structures cms and product selling platform independently of another. If you cannot break big structures, you should not even consider breaking small parts into independent applications. You end up with a monolithic architecture and thousands of (abstract) dependencies right through the whole application. A nightmare to evolve and maintain.

earc/cast provides an easy way to implement the adapter pattern. This gives you the freedom that comes with runtime interface polymorphism. The product box slider can use a different interface for the product than the selling platform. All that matters is the underlying data. If the product data of the selling platform is sufficient to the product box sliders product interface, an adapter can be written and thus it can be mapped. Now we have true decoupling and can evolve and maintain the cms independently of the selling platform. Moreover, you can break these two applications into smaller parts. All that has to be kept in sync is the adapter which is reduced in the case of earc/cast to a simple one-dimensional array (mapping).

releases
--------

[](#releases)

### release 1.1

[](#release-11)

- new function `generate_mapping`
- Documentation of all features

### release 1.0

[](#release-10)

- PHP &gt;= 8.0

### release 0.0

[](#release-00)

- PHP &gt;=5.6

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 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

Every ~33 days

Total

3

Last Release

1841d ago

Major Versions

0.0 → 1.02021-02-24

PHP version history (2 changes)0.0PHP &gt;=5.6

1.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7308f2797252cace014cfec12c1b5978c1bf5608be78d7a188ff690192959f3?d=identicon)[Thomas Koudela](/maintainers/Thomas%20Koudela)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/earc-cast/health.svg)

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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