PHPackages                             sportuondo/eralda - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. sportuondo/eralda

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

sportuondo/eralda
=================

Dead simple object to array transformation library.

v1.0.0(7y ago)049MITPHPPHP &gt;=7.1.0

Since Feb 28Pushed 7y agoCompare

[ Source](https://github.com/sportuondo/eralda)[ Packagist](https://packagist.org/packages/sportuondo/eralda)[ Docs](https://www.kubicode.com)[ RSS](/packages/sportuondo-eralda/feed)WikiDiscussions master Synced yesterday

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

Dead simple object to array transformation library
==================================================

[](#dead-simple-object-to-array-transformation-library)

[![Latest Version on Packagist](https://camo.githubusercontent.com/52db0c6500af736b2d3bd30b7168e4c4cb2ec6a4a66a529d230192b73ac95ff0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73706f7274756f6e646f2f6572616c64612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sportuondo/eralda)

The objective of this library is to centralize the logic in charge of transforming objects (usually models) into arrays in order to expose them through JSON. Eralda can transform any type of object (PHP standard objects, Laravel Eloquent models...) into an array by just indicating the desired property mapping.

Eralda also offers the possibility of transforming property values as well as embedding related elements (e.g. author → books).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require sportuondo/eralda
```

Usage
-----

[](#usage)

Given the following objects:

```
$tolkien = new Author();
$tolkien->id = 1;
$tolkien->name = 'J.R.R. Tolkien';
$tolkien->isFreelance = false;
$tolkien->birthDate = Carbon::createFromFormat('Y-m-d', '1892-01-03');

$hobbit = new Book();
$hobbit->id = 1;
$hobbit->author = $tolkien;
$hobbit->title = 'The Hobbit';
$hobbit->year = 1937;

$lotr = new Book();
$lotr->id = 2;
$lotr->author = $tolkien;
$lotr->title = 'The Lord of the Rings';
$lotr->year = 1968;

$this->tolkien->books = [
    $hobbit,
    $lotr
];
```

We can define the following ***Transformer***:

```
class AuthorArrayTransformer extends ArrayTransformerAbstract
{
    protected $keysMap = [
        'id'          => 'id',
        'name'        => 'name',
        'isFreelance' => 'is_freelance',
        'birthDate'   => 'birth_date',
    ];

    protected function presentBirthDate($author)
    {
        return $author->birthDate->format('Y-m-d');
    }
}
```

The `$keysMap` property is used to define the mapping between the properties of the source object and the keys of the resulting array.

On the other hand, we have included a ***presenter*** to modify the resulting value of a property of the original object on the fly. The nomenclature to define *presenters* is composed of the keyword **`present`** followed by the property name of the original object in **Camel Case** format.

To execute the transformation we would do the following:

```
$authorTransformer = new AuthorArrayTransformer();
$authorArray = $authorTransformer->transformItem($tolkien);

json_encode($authorArray)
```

The resulting array would look like this:

```
{
  "id": 1,
  "name": "J.R.R. Tolkien",
  "is_freelance": false,
  "birth_date": "1892-01-03"
}
```

### Transforming collections

[](#transforming-collections)

In the same way that we transform a single object we can also transform a collection of them. Since we have an array of authors we could do the following:

```
$authorTransformer = new AuthorArrayTransformer();
$authorsArray = $authorTransformer->transformCollection($authors);

json_encode($authorsArray)
```

To get something like this:

```
{
  [
    {
      "id": 1,
      "name": "J.R.R. Tolkien",
      "is_freelance": false,
      "birth_date": "1892-01-03"
    },
    {
      "id": 2,
      "name": "Isaac Asimov",
      "is_freelance": false,
      "birth_date": "1920-01-02"
    }
  ]
}
```

### Embedding additional elements

[](#embedding-additional-elements)

We can indicate the elements that we want to embed making use of the property **`$embeds`**:

Given the following book transformer:

```
class BookArrayTransformer extends ArrayTransformerAbstract
{
    protected $keysMap = [
        'id'     => 'id',
        'title'  => 'title',
        'author' => 'author',
        'year'   => 'year',
    ];

    protected function presentAuthor($book)
    {
        return $book->author->name;
    }
}
```

We could modify our transformer of authors in the following way:

```
class AuthorArrayTransformer extends ArrayTransformerAbstract
{
    protected $embeds = [
        'books',
    ];

    protected $keysMap = [
        'id'          => 'id',
        'name'        => 'name',
        'isFreelance' => 'is_freelance',
        'birthDate'   => 'birth_date',
    ];

    protected function presentBirthDate($author)
    {
        return $author->birthDate->format('Y-m-d');
    }

    protected function embedBooks($author)
    {
        $bookTransformer = new BookArrayTransformer();
        return $bookTransformer->transformCollection($author->books);
    }
}
```

In this case we want to embed the books belonging to the author. The nomenclature to define *embeds* is composed of the keyword **`embed`** followed by the name of the element we want to embed in **Camel Case** format.

The result would be something like this:

```
{
  [
    {
      "id": 1,
      "name": "J.R.R. Tolkien",
      "is_freelance": false,
      "birth_date": "1892-01-03",
      "books": [
        {
          "id": 1,
          "title": "The Hobbit",
          "author": "J.R.R. Tolkien",
          "year": 1937
        },
        {
          "id": 2,
          "title": "The Lord of the Rings",
          "author": "J.R.R. Tolkien",
          "year": 1968
        }
      ]
    },
    {
      "id": 2,
      "name": "Isaac Asimov",
      "is_freelance": false,
      "birth_date": "1920-01-02",
      "books": [
        {
          "id": 3,
          "title": "The Caves of Steel",
          "author": "Isaac Asimov",
          "year": 1954
        }
      ]
    }
  ]
}
```

### Testing

[](#testing)

```
composer test
```

Credits
-------

[](#credits)

- [Sendoa Portuondo](https://github.com/sportuondo)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 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 ~0 days

Total

2

Last Release

2629d ago

Major Versions

0.0.1 → v1.0.02019-03-01

PHP version history (2 changes)0.0.1PHP &gt;=7.0.0

v1.0.0PHP &gt;=7.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9d34346e7e8d800b6a3fcc6e90b81c3b0adef283e4ad8af66105d6b7d4d7e410?d=identicon)[sportuondo](/maintainers/sportuondo)

---

Top Contributors

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

---

Tags

jsonsportuondosendoaportuondoeralda

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sportuondo-eralda/health.svg)

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

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k316.9M612](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30422.2M45](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15267.7M16](/packages/clue-ndjson-react)

PHPackages © 2026

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