2017 © Pedro Peláez
 

library fill

A helper to fill objects / dto or whatever

image

jildertmiedema/fill

A helper to fill objects / dto or whatever

  • Wednesday, October 26, 2016
  • by jildertmiedema
  • Repository
  • 0 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Fill

A easy tool to fill objects., (*1)

Usage

A helper function is provided: mapTo() will return a callable which can be used in the mapping., (*2)

    $result = array_map(mapTo(Target::class), $data);

Example

use function JildertMiedema\Fill\mapTo;

class Demo
{
    private $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function name()
    {
        return $this->name;
    }
}

$data = [
    ['name' => 'test name 1'],
    ['name' => 'test name 1'],
];

$result = array_map(mapTo(Demo::class), $data);

var_dump($result);

output:, (*3)

array(2) {
  [0] =>
  class Demo#7 (1) {
    private $name =>
    string(11) "test name 1"
  }
  [1] =>
  class Demo#6 (1) {
    private $name =>
    string(11) "test name 1"
  }
}

Customize

You can customize the behaviour. For example in Laravel you can use the CallbackNormalizer to convert snake_cased_fields to camelCase., (*4)

In this example is shown how to implement your own mapTo() function:, (*5)

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use JildertMiedema\Fill\Build\ReflectionBuilder;
use JildertMiedema\Fill\Fill;
use JildertMiedema\Fill\Normalizer\CallbackNormalizer;
use JildertMiedema\Fill\Normalizer\SimpleNormalizer;

function mapTo($targetClass): \Closure
{
    $normalizer = new CallbackNormalizer(new SimpleNormalizer());
    $normalizer->register(Model::class, function (Model $model) {
        $data = $model->toArray();
        $keys = array_map(function ($key) {
            return Str::camel($key);
        }, array_keys($data));
        $data = array_combine($keys, array_values($data));

        return $data;
    });
    $fill = new Fill(new ReflectionBuilder($targetClass), $normalizer);

    return $fill->map();
}

class DemoModel extends Model
{
    protected $guarded = [];
}

class Target
{
    private $name;
    private $otherValue;

    public function __construct($name, $otherValue)
    {
        $this->name = $name;
        $this->otherValue = $otherValue;
    }

    public function name()
    {
        return $this->name;
    }
}

$data = [
    new DemoModel(['name' => 'test', 'other_value' => 'value']),
];

$result = array_map(mapTo(Target::class), $data);

var_dump($result);

The Versions

26/10 2016

dev-master

9999999-dev

A helper to fill objects / dto or whatever

  Sources   Download

MIT

The Requires

  • php ^7.0.0

 

The Development Requires

by Jildert Miedema

26/10 2016

v0.2.0

0.2.0.0

A helper to fill objects / dto or whatever

  Sources   Download

MIT

The Requires

  • php ^7.0.0

 

The Development Requires

by Jildert Miedema

26/10 2016

v0.1

0.1.0.0

A helper to fill objects / dto or whatever

  Sources   Download

MIT

The Requires

  • php ^7.0.0

 

The Development Requires

by Jildert Miedema