thephpleague/fractal — pull request #45 by jasonlewis (2014-05-03 09:34:39+00:00)
Title: Implementation of configurable serializers.

This is a PR in relation to #24 and #18.

Seeing as @joshuajabbour doesn't have the time at the moment to continue working on the PR I've had a crack at an implementation and would love some feedback.

### The Implementation

At its core is the `SerializerInterface` which has 5 method signatures declared. Each serializer implements these methods to return the data in a format of their desire. Included is a basic `ArraySerializer`, which returns the data in what I consider an untouched form. Resulting JSON from the `ArraySerializer` would be:

```json
{
"id": 1,
"name": "Bob",
"age": 32,
"embeds": [
"posts"
]
}
```

Benefit here is for systems returning pre-keyed data (above would have the user data keyed to `user`, for example).

The default used serializer would be the (current) `DataArraySerializer` which keys the top level data to `data`.

```json
{
"data": {
"id": 1,
"name": "Bob",
"age": 32
},
"embeds": [
"posts"
]
}
```

### Meta Data

This PR also includes meta data support for resources. Instead of defining a method on transformers I've then opted for a (hopefully) better approach by setting the meta data on the resource.

```php
$user = new User('Bob', '23');

$resource = new League\Fractal\Resource\Item($user, new UserTransformer);

$user->setMeta(array('token' => 'kan1$isdd098hjasd89hsa#$9q'));
```

The `ArraySerializer` simply merges with the top level much like it does with the standard data and the `DataArraySerializer` would key it to `meta` and merge to the top level.

### Tests

I've not done any tests yet merely patched the existing tests that failed due to the new meta data key. Will do tests once some more feedback is received on the approach and implementation.

Cheers.