PHPackages                             didix16/php-datatransformer - 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. didix16/php-datatransformer

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

didix16/php-datatransformer
===========================

A simple library that allows transform any kind of data to native php data or whatever

1.0.1(4y ago)08.6k1MITPHPPHP &gt;= 7.2

Since Aug 5Pushed 4y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (3)Used By (1)

PHP DataTransformer
===================

[](#php-datatransformer)

A simple library that allows transform any kind of data to native php data or whatever

Content
-------

[](#content)

- [What is a DataTransformer](#what-is-a-datatransformer)
- [Installation](#installation)
- [Usage](#usage)

### What is a DataTransformer

[](#what-is-a-datatransformer)

It is just an abstraction layer to transform data between two data types. Perfect for those API bridges data transformation between your system and incoming data.

It could handle json incoming data and then transform it to a class or whatever

### Installation

[](#installation)

```
composer require didix16/php-datatransformer
```

### Usage

[](#usage)

Example of how to use it. This is a transformer that gets a string data value and transforms it to a PHP DateTime

```
use didix16\DataTransformer\DataTransformer;

class DateTransformer extends DataTransformer {

    const FORMATS = [
        DateTimeInterface::ATOM,
        DateTimeInterface::COOKIE,
        DateTimeInterface::ISO8601,
        DateTimeInterface::RFC822,
        DateTimeInterface::RFC850,
        DateTimeInterface::RFC1036,
        DateTimeInterface::RFC1123,
        DateTimeInterface::RFC2822,
        DateTimeInterface::RFC3339,
        DateTimeInterface::RFC3339_EXTENDED,
        DateTimeInterface::RSS,
        DateTimeInterface::W3C
    ];

    /**
     * @var string
     */
    protected $fromFormat;

    /**
     * @var DateTimeZone
     */
    protected $toTimezone;

    public function __construct($fromFormat='Y-m-d', $toTimezone= 'Europe/Madrid')
    {
        $this->fromFormat = $fromFormat;
        $this->toTimezone = new DateTimeZone($toTimezone);
    }

    protected function transform(&$value)
    {
        if(empty($value)){
            $value = null;
            return;
        }

        $date = $this->convertToDate($value);
        if($this->assertClass($date, DateTime::class)){

            $this->dateToTimezone($date);
            $value = $date;
        } else {
            throw new Exception(
                sprintf("An error was ocurred while transforming the value '%s' into date:\n
                Expected to be an instance of '%s', founded '%s'", $value, DateTime::class, gettype($date))
            );
        }
    }

    /**
     * Tries to convert the passed value to a date by checking all possible DateTimeInterface formats first.
     * If no one satisfies then try using the specified format at constructor
     */
    private function convertToDate(&$value){

        foreach(self::FORMATS as $format){

            $t = DateTime::createFromFormat($format, $value);
            if($t) return $t;
        }

        try {
            return new DateTime($value);
        } catch(Exception $e){
            return DateTime::createFromFormat($this->fromFormat, $value);
        }
    }

    private function dateToTimezone(DateTime &$value){

        $currentTimezone = $value->getTimezone()->getName();
        $toTimezone = $this->toTimezone->getName();

        if($currentTimezone !== $toTimezone){
            $value->setTimezone($this->toTimezone);
        }
    }

}
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity47

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

Total

2

Last Release

1741d ago

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

phpdataSimpletransformerdatatransformerdto

### Embed Badge

![Health badge](/badges/didix16-php-datatransformer/health.svg)

```
[![Health](https://phpackages.com/badges/didix16-php-datatransformer/health.svg)](https://phpackages.com/packages/didix16-php-datatransformer)
```

###  Alternatives

[fab2s/souuid

Simple Ordered Uuid Generator in PHP

13573.2k1](/packages/fab2s-souuid)[hi-folks/data-block

Data class for managing nested arrays and JSON data.

1472.2k](/packages/hi-folks-data-block)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)

PHPackages © 2026

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