PHPackages                             blacktrs/data-transformer - 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. blacktrs/data-transformer

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

blacktrs/data-transformer
=========================

Zero-dependency PHP array-to-object transformer

v1.1.2(2y ago)32MITPHPPHP &gt;=8.2

Since Aug 13Pushed 2y ago1 watchersCompare

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

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

[![Scrutinizer coverage](https://camo.githubusercontent.com/3ea1a7feb8da82147db44b0dd345fcdbcf93aa01d571fd9af325dee6152c6293/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f626c61636b7472732f646174612d7472616e73666f726d65722f6d61696e)](https://camo.githubusercontent.com/3ea1a7feb8da82147db44b0dd345fcdbcf93aa01d571fd9af325dee6152c6293/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f626c61636b7472732f646174612d7472616e73666f726d65722f6d61696e) [![GitHub Workflow Status](https://camo.githubusercontent.com/f9991fc03083f93ec0bdbf70aeea8a64be248ab21eda70cdffd2819defb6f2f6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f626c61636b7472732f646174612d7472616e73666f726d65722f7068702e79616d6c)](https://camo.githubusercontent.com/f9991fc03083f93ec0bdbf70aeea8a64be248ab21eda70cdffd2819defb6f2f6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f626c61636b7472732f646174612d7472616e73666f726d65722f7068702e79616d6c) [![Packagist Version](https://camo.githubusercontent.com/74974ad5d15847e4db5358f9bbcebdb31a11fa0762841af89cdd382018139538/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f626c61636b7472732f646174612d7472616e73666f726d6572)](https://camo.githubusercontent.com/74974ad5d15847e4db5358f9bbcebdb31a11fa0762841af89cdd382018139538/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f626c61636b7472732f646174612d7472616e73666f726d6572)

Data transformer
================

[](#data-transformer)

The modern PHP library for converting arrays into objects and vice-versa.

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

[](#installation)

`composer require blacktrs/data-transformer`

Summary
-------

[](#summary)

By default, the library will try to resolve `public` properties and match the with array fields. The transformer is trying to find an object property with equal name as array field. In case, if object missing such fields, the transformer converts array keys into camel case. However, it is possible to configure needed properties with attributes.

### Features

[](#features)

- Seamless converting of objects into arrays and vice-versa
- Auto conversion of nested properties with class type-hint into array and vice-versa
- The library respects the declared property type
- The library will respect private properties unless they will be declared explicitly as writable by the attribute
- The library takes into account getters or methods with the same name as a property
- Enum conversion
- Stringable objects conversion
- Configurable custom property value resolvers
- Configurable custom object transformers

Configuration
-------------

[](#configuration)

Default transformer implementation `\Blacktrs\DataTransformer\Transformer\Transformer`allows to force private properties write by using method `setIncludePrivateProperties` or constructor parameter `$includePrivateProperties`

### Attribute `DataField`

[](#attribute-datafield)

This attribute is applicable to properties. By this attribute you can configure the behavior for each property you need

The following parameters can be specified:

- `string|null $nameIn` Field name in an input array to map with the property
- `string|null $nameOut` Field name in an output array to fill with the property data
- `ValueResolverInterface|class-string|null $valueResolver` Custom value resolver to convert property data in a needed way
- `array $valueResolverArguments` Additional arguments passed into value resolver
- `TransformerInterface|class-string|null $objectTransformer` Custom property transformer to achieve more control in case if object passed
- `bool $ignoreTransform` Skip property while transforming an array into the object
- `bool $ignoreSerialize` Skip property while serializing an object into array

### Attribute `DataObject`

[](#attribute-dataobject)

This attribute is applicable to classes and allows to use custom object serializer while converting object into other form

The following parameters can be specified:

- `ObjectSerializerInterface|class-string|null $objectTransformer` Custom object serializer

Examples
--------

[](#examples)

### Convert the array into the object

[](#convert-the-array-into-the-object)

```
use Blacktrs\DataTransformer\Attribute\DataField;
use Blacktrs\DataTransformer\Value\DateTimeValueResolver;
use Blacktrs\DataTransformer\Transformer\Transformer;

class RequestObject
{
    public readonly int $id;
    public readonly string $email;
    public readonly string $name;
    #[DataField(valueResolver: DateTimeValueResolver::class)]
    public readonly \DateTime $date;
}

$requestPayload = ['id' => 1, 'email' => 'some.email@example.com', 'name' => 'John Doe', 'date' => '2023-06-01 10:10:10'];

$transformer = new Transformer();
$requestObject = $transformer->transform(RequestObject::class, $requestPayload);

echo $requestObject->id; // 1
echo $requestObject->email; // some.email@example.com
echo $requestObject->name; // John Doe
echo $requestObject->date->format('Y-m-d'); // 2023-06-01
```

### Convert the object into the array

[](#convert-the-object-into-the-array)

```
use Blacktrs\DataTransformer\Serializer\ObjectSerializer;

class ResponseObject
{
    public function __construct(
        public readonly int $id,
        public readonly string $email,
        public readonly string $name
    ) {
    }
}

$responseObject = new ResponseObject(
    id: 1,
    email: 'some.email@example.com',
    name: 'John Doe'
);

$serializer = new ObjectSerializer();
$responseArray = $serializer->serialize($responseObject);

echo $responseArray['id']; // 1
```

### More examples

[](#more-examples)

More examples could be found in `tests`

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

Maturity59

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

Total

4

Last Release

971d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ae67184245cbfc1c24e6f3d5a2c45b87780793f042a0d7fb1bcaaa1e13a13e2d?d=identicon)[taras\_chr](/maintainers/taras_chr)

---

Top Contributors

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

---

Tags

array-transformerdata-transfer-objectphpphp-libraryphp82

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/blacktrs-data-transformer/health.svg)

```
[![Health](https://phpackages.com/badges/blacktrs-data-transformer/health.svg)](https://phpackages.com/packages/blacktrs-data-transformer)
```

###  Alternatives

[cyrkulewski/seo-url

Module converts any string to valid URL

121.2k](/packages/cyrkulewski-seo-url)

PHPackages © 2026

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