PHPackages                             mcustiel/php-simple-conversion - 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. mcustiel/php-simple-conversion

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

mcustiel/php-simple-conversion
==============================

A simple and minimalistic library to ease conversion between different types and classes

v1.3.1(8y ago)22.2kGPL-3.0+PHPPHP &gt;=5.5

Since Dec 7Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mcustiel/php-simple-conversion)[ Packagist](https://packagist.org/packages/mcustiel/php-simple-conversion)[ RSS](/packages/mcustiel-php-simple-conversion/feed)WikiDiscussions master Synced today

READMEChangelog (7)Dependencies (1)Versions (9)Used By (0)

php-simple-conversion
=====================

[](#php-simple-conversion)

What is it?
-----------

[](#what-is-it)

php-simple-conversion is a minimalistic conversion service for PHP. It's meant to be performant and easy to use. It allows developers to register a series of converter classes that converts from one type to another. The converters are instantiated only when they are needed, minimizing memory use and avoiding unnecessary processing.

[![Build Status](https://camo.githubusercontent.com/85d5087d53e816b4322cf5b97c945c0b03476262ff46f33d69c55128d0ee39aa/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f7068702d73696d706c652d636f6e76657273696f6e2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/php-simple-conversion/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/8f1396fca87650a2d4324e77af3e5bda28cdc6a34211744a351920dbfd74f51f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f7068702d73696d706c652d636f6e76657273696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/php-simple-conversion/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6d1281b0d139137228e30497d31b83add9ad47be3727bf3513f1eb544f93803f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f7068702d73696d706c652d636f6e76657273696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/php-simple-conversion/?branch=master)

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

[](#installation)

### Composer:

[](#composer)

This project is published in packagist, so you just need to add it as a dependency in your composer.json:

```
"require": {
        // ...
        "mcustiel/php-simple-conversion": "*"
    }
```

Or just download the release and include it in your path.

How to use it?
--------------

[](#how-to-use-it)

### Define your converters

[](#define-your-converters)

First of all, you have to define the converters you will use. As an example, supose you have two classes that represents a Person: one of them represents it in the format it is persisted in db, the other represents it as used in application's logic.

```
class DatabaseRegisterRepresentationForPerson
{
    private $id;
    private $jsonString;

    public function __construct($id, $jsonString)
    {
        $this->id = $id;
        $this->jsonString = $jsonString;
    }
    // ... Getters and setters
}

class DomainRepresentationForPerson
{
    private $id;
    private $firstName;
    private $lastName;
    private $age;

    // ... Getters and setters
}
```

You need a service that abstracts the access to database from the logic, in that service you will use a converter to convert from the class returned from the DAO to the class used by the logic and return it. Following there is an example of the converter your service should use.

```
use Mcustiel\PhpSimpleConversion\Converter;

// class DBPersonToLogicPersonConverter (MUST implement Converter interface)
class DBPersonToLogicPersonConverter implements Converter
{
    public function convert($a)
    {
        if (! ($a instanceof DatabaseRegisterRepresentationForPerson)) {
            throw new \InvalidArgumentException("Should convert only from DatabaseRegisterRepresentationForPerson");
        }
        $return = new DomainRepresentationForPerson();

        $return->setId($a->getId());
        $object = json_decode($a->getJsonString());
        $return->setFirstName($object->firstName);
        $return->setLastName($object->lastName);
        $return->setAge($object->age);

        return $return;
    }
}
```

You can define all the converters you want and then register them.

### Registering

[](#registering)

In your bootstrap file (or some startup script) you must register all the converters you have defined.

```
use Mcustiel\PhpSimpleConversion\ConversionService;
use Mcustiel\PhpSimpleConversion\ConverterBuilder;

$conversionService = new ConversionService();
// ...
$converter = ConverterBuilder::get()
    ->from(DatabaseRegisterRepresentationForPerson::class)
    ->to(DomainRepresentationForPerson::class)
    ->withImplementation(DBPersonToLogicPersonConverter::class); // Implementation could be the name of the class or an instance
$conversionService->registerConverter($converter);
```

### Convert

[](#convert)

Then you just need to inject the ConversionService to any class where you want to do conversions and call it as follows:

```
    $dbPerson = $personDao->getPerson('alice');
    $logicPerson = $conversionService->convert($dbPerson, DomainRepresentationForPerson::class);
```

The library will automatically take care of resolving the registered service and calling it to convert your object to the desired one.

#### Converting from parent class

[](#converting-from-parent-class)

Optionally, you can tell the conversion service to search for a converter configured for a parent class. Supose you have a class B that inherits from class C, a converter from C to A, and you want to convert from B to A. This is possible by telling the converter to search for converters for parent classes:

```
use Mcustiel\PhpSimpleConversion\ConversionService;
use Mcustiel\PhpSimpleConversion\ConverterBuilder;

$conversionService = new ConversionService();
// ...
$converter = ConverterBuilder::get()
    ->from(C::class)
    ->to(A::class)
    ->withImplementation(CtoAConverter::class);
$conversionService->registerConverter($converter);
$b = new B();
$a = $conversionService->convert($b, A::class, ConversionService::ALLOW_PARENTS);
```

Notes
-----

[](#notes)

Currently you can only set 'string', 'array' or a full class name as 'from' and 'to' parameters in the converter builder.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

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

Recently: every ~274 days

Total

7

Last Release

3101d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d9b3ff93206038debfa1f16a11bbfc10fca9b2f4ddfdafa00e27365d290cf0d?d=identicon)[mcustiel](/maintainers/mcustiel)

---

Top Contributors

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

---

Tags

conversionphpabstractionconverterSimpleminimalist

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mcustiel-php-simple-conversion/health.svg)

```
[![Health](https://phpackages.com/badges/mcustiel-php-simple-conversion/health.svg)](https://phpackages.com/packages/mcustiel-php-simple-conversion)
```

###  Alternatives

[simplesoftwareio/simple-qrcode

Simple QrCode is a QR code generator made for Laravel.

2.9k30.5M113](/packages/simplesoftwareio-simple-qrcode)[rinvex/countries

Rinvex Countries is a simple and lightweight package for retrieving country details with flexibility. A whole bunch of data including name, demonym, capital, iso codes, dialling codes, geo data, currencies, flags, emoji, and other attributes for all 250 countries worldwide at your fingertips.

1.7k8.0M62](/packages/rinvex-countries)[willdurand/geocoder

Common files for PHP Geocoder

17437.9M170](/packages/willdurand-geocoder)[mark-gerarts/auto-mapper-plus

An AutoMapper for PHP

5623.5M23](/packages/mark-gerarts-auto-mapper-plus)[iamcal/php-emoji

This is a PHP library for dealing with Emoji, allowing you to convert between various native formats and displaying them using HTML.

1.3k550.9k](/packages/iamcal-php-emoji)[matthiasmullie/path-converter

Relative path converter

10632.1M9](/packages/matthiasmullie-path-converter)

PHPackages © 2026

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