Skip to content

Instantly share code, notes, and snippets.

@lkmadushan
Last active March 16, 2018 13:30
Show Gist options
  • Save lkmadushan/a29c8274fe3614928b1d to your computer and use it in GitHub Desktop.
Save lkmadushan/a29c8274fe3614928b1d to your computer and use it in GitHub Desktop.
Remove data key from "thephpleague/fractal" serialize output.
<?php
namespace Serializers;
use League\Fractal\Serializer\ArraySerializer;
class JsonSerializer extends ArraySerializer
{
/**
* Serialize a collection
*
* @param string $resourceKey
* @param array $data
* @return array
**/
public function collection($resourceKey, array $data)
{
return ($resourceKey && $resourceKey !== 'data') ? array($resourceKey => $data) : $data;
}
/**
* Serialize an item
*
* @param string $resourceKey
* @param array $data
* @return array
**/
public function item($resourceKey, array $data)
{
return ($resourceKey && $resourceKey !== 'data') ? array($resourceKey => $data) : $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment