2017 © Pedro Peláez
 

library php-simple-conversion

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

image

mcustiel/php-simple-conversion

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

  • Friday, January 5, 2018
  • by mcustiel
  • Repository
  • 1 Watchers
  • 2 Stars
  • 1,070 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 29 % Grown

The README.md

php-simple-conversion

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., (*1)

Build Status Code Coverage Scrutinizer Code Quality, (*2)

Installation

Composer:

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

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

Or just download the release and include it in your path., (*4)

How to use it?

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., (*5)

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., (*6)

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., (*7)

Registering

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

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

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

    $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., (*10)

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:, (*11)

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

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

The Versions

05/01 2018

dev-master

9999999-dev

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

The Development Requires

converter simple abstraction minimalist

05/01 2018

v1.3.1

1.3.1.0

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

The Development Requires

converter simple abstraction minimalist

05/01 2018

dev-develop

dev-develop

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

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

The Development Requires

converter simple abstraction minimalist

23/02 2017

v1.3.0

1.3.0.0

A simple and minimalistic library to ease conversion between different types

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

converter simple abstraction minimalist

14/08 2015

v1.x-dev

1.9999999.9999999.9999999-dev

A simple and minimalistic library to ease conversion between different types

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

The Development Requires

converter simple abstraction minimalist

13/08 2015

1.2.1

1.2.1.0

A simple and minimalistic library to ease conversion between different types

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

The Development Requires

converter simple abstraction minimalist

06/01 2015

1.2.0

1.2.0.0

A simple and minimalistic library to ease conversion between different types

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

The Development Requires

converter simple abstraction minimalist

07/12 2014

1.1.0

1.1.0.0

A simple and minimalistic library to ease conversion between different types

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

The Development Requires

config manager abstraction multiformat

07/12 2014

1.0

1.0.0.0

A simple and minimalistic library to ease conversion between different types

  Sources   Download

GPL-3.0+

The Requires

  • php >=5.5

 

The Development Requires

config manager abstraction multiformat