2017 © Pedro Peláez
 

library transformers

A package for normalizing property names and types from an external data store.

image

aol/transformers

A package for normalizing property names and types from an external data store.

  • Wednesday, November 30, 2016
  • by jakeasmith
  • Repository
  • 28 Watchers
  • 37 Stars
  • 11,846 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 2 Open issues
  • 25 Versions
  • 1 % Grown

The README.md

Transformers, roll out!

Build Status Latest Stable Version Latest Unstable Version Total Downloads Code Climate, (*1)

What is this?

Aol/Transformers provides a way to quickly handle two-way data transformations. This is useful for normalizing data from external or legacy systems in your application code or even for cleaning up and limiting responses from your external HTTP API., (*2)

But why not just fix the data at the source? If you can, do it! Often though, that's not an option, and that's when you need to "fix" the data at the application layer., (*3)

There are two terms you should know:, (*4)

  • app - Short for "application" and this is the format we want to use in our application.
  • ext - Short for "external" and this is the format that is used externally.

Basic usage

In a very basic use case a new Transformer instance can be created and the transformation definitions can be defined on the fly. Take a look at the code and then we'll walk thru what's happening., (*5)

<?php

$post = ['Id' => '5', 'Title' => 'Awesome Post!', 'post_tags' => '["awesome"]'];

$transformer = new \Aol\Transformers\Transformer;
$transformer->define('id', 'Id', 'intval', 'strval');
$transformer->define('title', 'Title');
$transformer->define('tags', 'post_tags', 'json_decode', 'json_encode');

$post = $transformer->toApp($post);
// ['id' => 5, 'title' => 'Awesome Post!', 'tags' => ['awesome']];

We have a "Post" array from some external source. It could be MySql, Mongo, an API, the source doesn't really matter. The important part is that we have a post with a few issues. Here's a checklist of things we want to change:, (*6)

  1. All of the key names should be lowercase snake_case. Oh, and post_tags is kinda silly, we'll make that simply 'tags`.
  2. The post id is always numerical, lets make that an integer.
  3. The post tags are currently a JSON string, lets turn that into a PHP array.

We can make all of these changes by creating a definition for each key. The first argument is the key we want to use in the application, the second argument is the key name that has been forced upon us externally. In many cases (such as title) its enough to just define those two fields., (*7)

In cases where we want to actually change the value we can pass in two more arguments. The third argument is a callable that will be applied when we convert to the application context, and the fourth argument is a callable that will be applied when we convert to the external context., (*8)

Take a look at the define signature:, (*9)

public function define($app_name, $ext_name, callable $app_func = null, callable $ext_func, $app_args = [], $ext_args = []);
  • $app_name - This is the key name we want to use in the application.
  • $ext_name - This is the key name that has been forced upon us externally.
  • $app_func - This is an optional callable that will be applied when transforming an array to the application format.
  • $ext_func - This is an optional callable that will be applied when transforming an array to the external format.
  • $app_args - Optional arguments for the application callable
  • $ext_args - Optional arguments for the external callable

Subclass it

Often its useful to reuse a Transformer. In this create a new class that extends the Transformer class and implement the definitions in the constructor., (*10)

<?php

class Post extends \Aol\Transformers\Transformer
{
    public function __construct()
    {
        $this->define('id', 'Id', 'intval', 'strval');
        $this->define('title', 'Title');
        $this->define('tags', 'post_tags', 'json_decode', 'json_encode');
    }
}

Leveraging Traits

Many common definitions can be simplified by leveraging the wrapper methods provided by utility traits., (*11)

<?php

class Post extends \Aol\Transformers\Transformer
{
    use \Aol\Transformers\Utilities\MysqlTrait,
        \Aol\Transformers\Utilities\UtilityTrait;

    public function __construct()
    {
        $this->defineId('id', 'Id');
        $this->define('title', 'Title');
        $this->defineJson('tags', 'post_tags');
    }
}

Installation

$ composer require aol/transformers ^2, (*12)

Contributing

..., (*13)

License

This project is licensed under the MIT License - see the LICENSE file for details., (*14)

The Versions

30/11 2016

dev-master

9999999-dev http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

 

The Development Requires

mysql mongo transformation layer data store normalizing

21/11 2016

dev-naming-convention

dev-naming-convention http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

 

The Development Requires

mysql mongo transformation layer data store normalizing

11/03 2016

3.1.4

3.1.4.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

 

The Development Requires

mysql mongo transformation layer data store normalizing

24/02 2016

3.1.3

3.1.3.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

 

The Development Requires

mysql mongo transformation layer data store normalizing

16/10 2015

3.1.2

3.1.2.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

 

The Development Requires

mysql mongo transformation layer data store normalizing

12/10 2015

3.1.1

3.1.1.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

 

The Development Requires

mysql mongo transformation layer data store normalizing

12/10 2015

dev-maps

dev-maps http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

12/10 2015

3.1.0

3.1.0.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

26/08 2015

3.0.1

3.0.1.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

19/08 2015

3.0.0

3.0.0.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

06/05 2015

dev-version-2-docs

dev-version-2-docs http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

06/05 2015

2.0.0

2.0.0.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

26/02 2015

dev-cipher

dev-cipher http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

26/02 2015

dev-refactor

dev-refactor http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

26/02 2015

dev-cipher-trait

dev-cipher-trait http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

21/01 2015

dev-doc_fix

dev-doc_fix http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

25/08 2014

1.1.2

1.1.2.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

22/08 2014

1.1.1

1.1.1.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

21/08 2014

1.1.0

1.1.0.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

20/08 2014

1.0.0

1.0.0.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

18/07 2014

0.5.4

0.5.4.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

17/07 2014

0.5.3

0.5.3.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

23/06 2014

0.5.2

0.5.2.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

15/06 2014

0.5.1

0.5.1.0 http://github.com/aol/transformers

A package for normalizing property names and types from an external data store.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer data store normalizing

10/06 2014

0.5.0

0.5.0.0 http://github.com/aol/transformers

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

mysql mongo transformation layer