Skip to content

metabitco/transform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is this package?

This package is meant to take an array of data and "transform" it to match the pattern of another array.

How do I use it?

use Kregel\Transform\Transformer;

$dataTransformer = new Transformer([
    'NAME' => 'Austin Kregel
], [
    'NAME' => 'full_name'
]);

$dataTransformer->transform();

or

use Kregel\Transform\Transformer;

$dataTransformer = new Transformer;

$dataTransformer->setOriginalFeed([
    'NAME' => 'Austin Kregel'
])

$dataTransformer->setKeys([
    'NAME' => 'full_name'
]);

$dataTransformer->transform();

Then transform will return an array

[
    'full_name' => 'Austin Kregel'
]

If you're using laravel you can use the LaravelModelTransformer to get the same functionality for your models.

class User extends LaravelModelTransformer 
{
    public $fillable = [
        'full_name'
    ];
    
    public function posts()
    {
        return $this->hasMany(Post::class);
    }

    public function getKeys()
    {
        return [
            'full_name' => 'name',
            'posts' => 'blogPosts'
        ];
    }
}

Then you could use

$user = User::first();

$user->transform();

What could this be used for?

Well, what you could use it for is a list unmeasurable... What I plan on doing with it, is build an API that gracefully ages. Much like the way Stripe supports older versions of their api. This allows us as developers to change the data we receive and put it into the format we need. So long as we control it, the api could be upgraded as many times as we need, and we only need to remap the inputs/outputs to the respective values for it to work.

About

Map array keys to other array keys

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages