Skip to content

Commit

Permalink
Laravel collection macro (spatie#158)
Browse files Browse the repository at this point in the history
* add laravel collection macro, docs, test

* cleanup test

* StyleCI fixes
  • Loading branch information
jszobody authored and freekmurze committed Nov 28, 2017
1 parent 253248c commit 2377050
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ There's also a very short syntax available to quickly transform data:
fractal($books, new BookTransformer())->toArray();
```

You can transform directly from a Laravel collection as well:

```php
collect($books)->transformWith(new BookTransformer());
```

Transforming right from a Laravel collection is particularly useful for Eloquent results:

```php
Users::all()->transformWith(new UserTransformer())->toArray();
```

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all
our open source projects [on our website](https://spatie.be/opensource).

Expand All @@ -73,7 +85,7 @@ composer require spatie/laravel-fractal

The package will automatically register itself.

If you want to [change the default serializer](https://github.com/spatie/fractalistic#changing-the-default-serializer),
If you want to [change the default serializer](https://github.com/spatie/fractalistic#changing-the-default-serializer),
the [default paginator](https://github.com/spatie/fractalistic#using-pagination),
or the default fractal class `Spatie\Fractal\Fractal`
you must publish the config file:
Expand Down Expand Up @@ -236,7 +248,7 @@ We publish all received postcards [on our company website](https://spatie.be/en/

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie).
Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie).
All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

## License
Expand Down
13 changes: 13 additions & 0 deletions src/FractalServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Fractal;

use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Application as LumenApplication;
use Spatie\Fractal\Console\Commands\TransformerMakeCommand;
Expand All @@ -21,6 +22,8 @@ public function boot()
TransformerMakeCommand::class,
]);
}

$this->setupMacro();
}

/**
Expand All @@ -47,4 +50,14 @@ protected function setupConfig()

$this->mergeConfigFrom($source, 'fractal');
}

/**
* Add a 'transformWith' macro to Laravel's collection.
*/
protected function setupMacro()
{
Collection::macro('transformWith', function ($transformer) {
return fractal($this, $transformer);
});
}
}
23 changes: 23 additions & 0 deletions tests/LaravelCollectionMacroTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Spatie\Fractal\Test;

use Illuminate\Support\Collection;

class LaravelCollectionMacroTest extends TestCase
{
/** @test */
public function it_provides_laravel_colletion_macro()
{
$transformedData = Collection::make($this->testBooks)
->transformWith(new TestTransformer())
->toArray();

$expectedArray = ['data' => [
['id' => 1, 'author' => 'Philip K Dick'],
['id' => 2, 'author' => 'George R. R. Satan'],
]];

$this->assertEquals($expectedArray, $transformedData);
}
}

0 comments on commit 2377050

Please sign in to comment.