thephpleague/fractal — pull request #55 by benglass (2014-05-16 02:53:35+00:00)
Title: [WIP] Create TransformerInterface, add FieldFilterTransformer implementing partial field functionality

This is an example of one way that the partial field function we discussed could be implemented. This is a proof of concept and RFC.

I created a TransformerInterface which just provides the public methods that are used in the Scope class when interacting with TransformerAbstract. All the tests still pass and it doesnt make any real difference to type hint on TransformerInterface except to open up additional possibilities for implementations.

In this case we have a based implementation NestedTransformerAbstract which wraps around another transformer. Its abstract because by itself it does nothing, but FieldFilterTransformer wraps around another transformer but then filters by whitelisted fields.

Interested to hear your thoughts, there are certainly other ways to go about this but I do think that the transformer interface could be a nice way to increase flexibility of the library.

```php
class FooTransformer extends TransformerAbstract
{
public function transform($data)
{
return $data;
}
}

$transformer = new FooTransformer();
$fieldTransformer = new League\Fractal\FieldFilterTransformer($transformer);
$fieldTransformer->setFields(array('foo'));
$fieldTransformer->setPartialFields('stats', array(
'count',
'frequency'
));
$fieldTransformer->setPartials(array('stats'));

$data = array(
'foo' => 'bar',
'baz' => 'biz',
'count' => 500,
'frequency' => 0.5,
);

var_dump($fieldTransformer->transform($data));
```

Output
```shell
array(3) {
'foo' =>
string(3) "bar"
'count' =>
int(500)
'frequency' =>
double(0.5)
}
````