2017 © Pedro Peláez
 

package exporter

Export the attributes you need from all your objects and arrays.

image

mathieutu/exporter

Export the attributes you need from all your objects and arrays.

  • Monday, February 19, 2018
  • by mathieutu
  • Repository
  • 0 Watchers
  • 38 Stars
  • 231 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 4 Versions
  • 57 % Grown

The README.md

Exporter: Export the attributes you need from all your objects and arrays.

Build Status Quality Score Code Coverage Total Downloads Latest Stable Version License, (*1)

Installation

Require this package with composer:, (*2)

composer require mathieutu/exporter

Use cases

Because pictures are worth thousands words:, (*3)

The Exporter package let you write this:, (*4)

Exporter use case: After , (*5)

instead of that:, (*6)

Exporter use case: Before , (*7)

For example, I use it a lot with Laravel Eloquent Resources, or as an easier alternative of Symfony Normalizer:, (*8)

Exporter use case: Resource , (*9)

Usage

Use the \MathieuTu\Exporter\Exporter trait on your classes. You also can use directly the \MathieuTu\Exporter\ExporterService::exportFrom($exportable, $attributes) static method on basic arrays or objects, or if you can't add the trait., (*10)

You can export from arrays, objects with ArrayAccess interface, or any standard objects., (*11)

The response will be a Laravel Collection (but you absolutely don't need Laravel, this package is totally framework agnostic). If you don't know how to use collections, you can use it exactly like an array, or use toArray() method to get a real one., (*12)

Examples

(You can find all this examples and more in the package tests), (*13)

For the examples, and to cover all the possible ways to use this package, we'll consider this object as input:, (*14)

$object = new class
{
    use \MathieuTu\Exporter\Exporter;

    public $foo = 'testFoo';
    private $bar = ['bar1' => 'testBar1', 'bar2' => 'testBar2', 'bar3' => 'testBar3'];
    public $baz = [
        (object) ['baz1' => 'baz1A', 'baz2' => 'baz2A', 'baz3' => 'baz3A'],
        (object) ['baz1' => 'baz1B', 'baz2' => 'baz2B', 'baz3' => 'baz3B'],
        (object) ['baz1' => 'baz1C', 'baz2' => 'baz2C', 'baz3' => 'baz3C'],
    ];

    public function testWithParam(string $param): string
    {
        return 'test' . $param;
    }

    public function test(): string
    {
        return 'test' . date("l");
    }

    public function getBar(): array
    {
        return $this->bar;
    }
};

and a standard array as output (in comment), instead of a Collection (result from the $collection->toArray() method)., (*15)

Export public and private (with getter) root attributes

$object->export(['foo']); // ['foo' => testFoo]
$object->export(['foo', 'bar']); 
/* 
[
    'foo' => testFoo,
    'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
]
*/

Export from nested array/object

  • ##### In an array:
$object->export(['bar' => ['bar2', 'bar3']]);
/* 
[
    'bar' => [
        'bar2' => testBar2',
        'bar3' => testBar3',
    ],
]
*/
  • ##### Only one attribute:
$object->export(['bar' => 'bar1']); // ['bar' => 'testBar1']
  • ##### With dot notation:
$object->export(['bar.bar1']); // ['bar.bar1' => 'testBar1']
  • ##### Using a wildcard to export from lists:
$object->export(['baz' => ['*' => ['baz1', 'baz3']]]); 
/* 
[
    'baz' => [
        ['baz1' => 'baz1A', 'baz3' => 'baz3A'],
        ['baz1' => 'baz1B', 'baz3' => 'baz3B'],
        ['baz1' => 'baz1C', 'baz3' => 'baz3C'],
    ],
]
*/        

Set an alias as key:

$object->export(['foo', 'bar.bar2 as secondBar']); 
/* 
[
    'foo' => testFoo,
    'secondBar' => 'testBar2',
]
*/

Export result of a function

$object->export(['testWithParam(Mathieu)']); // ['testWithParam' => testMathieu]
$object->export(['test()']); // ['test' => testFriday]

License

This Exporter package is an open-sourced software licensed under the MIT license., (*16)

Contributing

Issues and PRs are obviously welcomed and encouraged, both for bugs and new features as well as documentation. Each piece of code added should be fully tested, but we can do that all together, so please don't be afraid by that., (*17)

The Versions

19/02 2018

dev-master

9999999-dev

Export the attributes you need from all your objects and arrays.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu TUDISCO

19/02 2018

2.0.0

2.0.0.0

Export the attributes you need from all your objects and arrays.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu TUDISCO

18/02 2018

1.1.0

1.1.0.0

Export the attributes you need from all your objects and arrays.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu TUDISCO

04/01 2018

1.0.0

1.0.0.0

Export the attributes you need from all your objects and arrays.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu TUDISCO