PHPackages                             mathieutu/exporter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mathieutu/exporter

ActivePackage[API Development](/categories/api)

mathieutu/exporter
==================

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

3.4.0(3mo ago)4336.1k↓29.5%21MITPHPPHP &gt;= 8.4CI failing

Since Jan 4Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/mathieutu/exporter)[ Packagist](https://packagist.org/packages/mathieutu/exporter)[ RSS](/packages/mathieutu-exporter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (14)Used By (1)

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

[](#exporter-export-the-attributes-you-need-from-all-your-objects-and-arrays)

[![Quality Score](https://camo.githubusercontent.com/ce417edca10afd7a7f8bbed1bcd215517e3142ae74f9d6d9544f54881bd9036d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d61746869657574752f6578706f727465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mathieutu/exporter)[![Code Coverage](https://camo.githubusercontent.com/2aca9ec2e5eaa1aa0ae79f6d44689df2abe4d1168629605a3ad0b337978cdb84/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d61746869657574752f6578706f727465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mathieutu/exporter)[![Total Downloads](https://camo.githubusercontent.com/1229a2022e9bf2911127f40d34aa579c56f48057be9173eb396ef7beade65c8d/68747470733a2f2f706f7365722e707567782e6f72672f6d61746869657574752f6578706f727465722f642f746f74616c2e7376673f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/mathieutu/exporter)[![Latest Stable Version](https://camo.githubusercontent.com/c635bfc86c5c33083feeccb4717d19a26c25fbc8ec9a41f5eddafc174fe5bc2e/68747470733a2f2f706f7365722e707567782e6f72672f6d61746869657574752f6578706f727465722f762f737461626c652e7376673f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/mathieutu/exporter)[![License](https://camo.githubusercontent.com/c18d625165e1bca371ed86a44f0af17ce1af0603e6746f3bf75d7c3ab654baea/68747470733a2f2f706f7365722e707567782e6f72672f6d61746869657574752f6578706f727465722f6c6963656e73652e7376673f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/mathieutu/exporter)

Installation
------------

[](#installation)

Require this package with composer:

```
composer require mathieutu/exporter
```

Use cases
---------

[](#use-cases)

Because pictures are worth thousands words:

The Exporter package let you write this:

 [ ![Exporter use case: After](./.github/assets/after.png) ](./.github/assets/after.png)

instead of that:

 [ ![Exporter use case: Before](./.github/assets/before.png) ](./.github/assets/before.png)

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

 [ ![Exporter use case: Resource](./.github/assets/resource.png) ](./.github/assets/resource.png)

Usage
-----

[](#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](https://laravel.com/docs/collections) (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.

### Strict Mode

[](#strict-mode)

By default, when you try to export an attribute that doesn't exist, the Exporter will return `null` for that attribute. However, you can enable **strict mode** to throw a `NotFoundException` instead, which helps catch typos and missing attributes during development:

```
\MathieuTu\Exporter\ExporterService::$strict = true;

$object->export(['foo', 'nonExistentProperty']);
// Throws: NotFoundException: nonExistentProperty can't be found in {...}
```

This is particularly useful during development to ensure all your exported attributes exist, and can be disabled in production if you prefer more lenient behavior.

### Examples

[](#examples)

*(You can find all this examples and more in the [package tests](./tests/ExporterServiceTest.php))*

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

```
$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).

#### Export public and private (with getter) root attributes

[](#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

[](#export-from-nested-arrayobject)

- ##### In an array:

    [](#in-an-array)

```
$object->export(['bar' => ['bar2', 'bar3']]);
/*
[
    'bar' => [
        'bar2' => testBar2',
        'bar3' => testBar3',
    ],
]
*/
```

- ##### Only one attribute:

    [](#only-one-attribute)

```
$object->export(['bar' => 'bar1']); // ['bar' => 'testBar1']
```

- ##### With dot notation:

    [](#with-dot-notation)

```
$object->export(['bar.bar1']); // ['bar.bar1' => 'testBar1']
```

- ##### Using a wildcard to export from lists:

    [](#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:

[](#set-an-alias-as-key)

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

#### Export result of a function

[](#export-result-of-a-function)

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

Complete Example
----------------

[](#complete-example)

Here's a comprehensive example that showcases the full power of the Exporter package by combining functions, aliases, nesting, and wildcards:

```
// Consider a real-world scenario: a blog post with author and comments
$blogPost = new class {
    use \MathieuTu\Exporter\Exporter;

    public int $id = 42;
    public string $title = 'Exporting Complex Data Structures with PHP Exporter';
    public DateTime $publishedAt {
        get => new DateTime('2026-02-15');
    }

    public array $author = [
        'id' => 1,
        'name' => 'Mathieu Tudisco',
        'email' => 'oss@mathieutu.dev',
        'bio' => 'PHP Developer',
    ];

    public array $comments {
        get => [
            (object)['id' => 1, 'author' => 'Alice', 'content' => 'Great article!', 'likes' => 5],
            (object)['id' => 2, 'author' => 'Bob', 'content' => 'Very helpful', 'likes' => 3],
            (object)['id' => 3, 'author' => 'Charlie', 'content' => 'Thanks for sharing', 'likes' => 7],
        ];
    }

        public function getSlug(): string
        {
            return strtolower(str_replace(' ', '-', $this->title));
        }

        public function getExcerpt(): string
        {
            return substr($this->title, 0, 17) . '...';
        }
};

// Export a complete, structured API response with all features combined
$apiResponse = $blogPost->export([
    'id',
    'title',
    'slug',                                       // Automatic getter (getSlug)
    'publishedAt' => 'format(j F Y)',           // Native nested function with parameter
    'excerpt as summary',                         // Automatic getter + alias
    'author as writer' => ['name', 'bio'],        // Nested export with alias on key
    'author' => 'name',                           // Nested export
    'author.email as contact',                    // Nested export with dot notation + alias
    'comments as feedback' => [                   // Collection with alias on key
        '*' => ['author as commenter', 'likes']   // Wildcard + alias on nested attribute
    ],
]);

/* Result:
[
    'id' => 42,
    'title' => 'Exporting Complex Data Structures with PHP Exporter',
    'slug' => 'exporting-complex-data-structures-with-php-exporter',
    'publishedAt' => '15 February 2026',
    'summary' => 'Exporting Complex...',
    'writer' => [
        'name' => 'Mathieu Tudisco',
        'bio' => 'PHP Developer',
    ],
    'author' => 'Mathieu Tudisco',
    'contact' => 'oss@mathieutu.dev',
    'feedback' => [
        ['commenter' => 'Alice', 'likes' => 5],
        ['commenter' => 'Bob', 'likes' => 3],
        ['commenter' => 'Charlie', 'likes' => 7],
    ],
]
*/
```

All these features work seamlessly together, allowing you to transform complex data structures into clean, well-structured responses with minimal code.

### License

[](#license)

This Exporter package is an open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

### Contributing

[](#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.

###  Health Score

60

—

FairBetter than 99% of packages

Maintenance82

Actively maintained with recent releases

Popularity38

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity88

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 92.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~269 days

Recently: every ~474 days

Total

12

Last Release

91d ago

Major Versions

1.1.0 → 2.0.02018-02-19

2.1.0 → 3.0.02020-12-07

PHP version history (5 changes)1.0.0PHP &gt;= 7.0.0

2.0.0PHP &gt;= 7.1.0

3.0.0PHP &gt;= 7.3.0

3.3.0PHP &gt;= 8.1

3.4.0PHP &gt;= 8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/11351322?v=4)[Mathieu TUDISCO](/maintainers/mathieutu)[@mathieutu](https://github.com/mathieutu)

---

Top Contributors

[![mathieutu](https://avatars.githubusercontent.com/u/11351322?v=4)](https://github.com/mathieutu "mathieutu (51 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (3 commits)")[![pawndev](https://avatars.githubusercontent.com/u/7567343?v=4)](https://github.com/pawndev "pawndev (1 commits)")

---

Tags

apiarrayslaravelobjectsresourcessymfonytransformer

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/mathieutu-exporter/health.svg)

```
[![Health](https://phpackages.com/badges/mathieutu-exporter/health.svg)](https://phpackages.com/packages/mathieutu-exporter)
```

###  Alternatives

[kunalvarma05/dropbox-php-sdk

Dropbox PHP API V2 SDK (Unofficial)

3633.0M18](/packages/kunalvarma05-dropbox-php-sdk)[swisnl/json-api-client

A PHP package for mapping remote JSON:API resources to Eloquent like models and collections.

211473.2k12](/packages/swisnl-json-api-client)[lkdevelopment/hetzner-cloud-php-sdk

An unofficial PHP SDK for the Hetzner Cloud API.

12692.0k1](/packages/lkdevelopment-hetzner-cloud-php-sdk)[craftpulse/craft-typesense

Craft Plugin that synchronises with Typesense

122.7k](/packages/craftpulse-craft-typesense)[vazaha-nl/mastodon-api-client

A fully typed and feature complete Mastodon API client for PHP

251.5k1](/packages/vazaha-nl-mastodon-api-client)[koot-labs/telegram-bot-dialogs

Telegram Bot API PHP SDK extension that allows to implement dialogs in bots

142.0k](/packages/koot-labs-telegram-bot-dialogs)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
