Download the PHP package mathieutu/exporter without Composer
On this page you can find all versions of the php package mathieutu/exporter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mathieutu/exporter
More information about mathieutu/exporter
Files in mathieutu/exporter
Package exporter
Short Description Export the attributes you need from all your objects and arrays.
License MIT
Informations about the package exporter
Exporter: Export the attributes you need from all your objects and arrays.
Installation
Require this package with composer:
Use cases
Because pictures are worth thousands words:
The Exporter package let you write this:
instead of that:
For example, I use it a lot with Laravel Eloquent Resources, or as an easier alternative of Symfony Normalizer:
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.
You can export from arrays, objects with ArrayAccess
interface, or any standard objects.
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.
Examples
(You can find all this examples and more in the package tests)
For the examples, and to cover all the possible ways to use this package, we'll consider this object as input:
and a standard array as output (in comment), instead of a Collection (result from the $collection->toArray()
method).
Export public and private (with getter) root attributes
Export from nested array/object
-
In an array:
-
Only one attribute:
-
With dot notation:
-
Using a wildcard to export from lists:
Set an alias as key:
Export result of a function
License
This Exporter package is an open-sourced software licensed under the MIT license.
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.