Skip to content

Instantly share code, notes, and snippets.

@graemetait
Forked from livkiss/example_Author.php
Last active August 30, 2015 10:13
Show Gist options
  • Save graemetait/a6f12ece0fcc58e77202 to your computer and use it in GitHub Desktop.
Save graemetait/a6f12ece0fcc58e77202 to your computer and use it in GitHub Desktop.
Example usage of Fractal with objects instead of arrays
<?php namespace Example;
class Author
{
private $id;
private $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
}
<?php namespace Example;
class Book
{
private $id;
private $name;
private $author;
public function __construct($id, $name, Author $author = null)
{
$this->id = $id;
$this->name = $name;
$this->author = $author;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getAuthor()
{
return $this->author;
}
}
<?php namespace Example\Transformers;
use League\Fractal\TransformerAbstract;
use Example\Author;
class AuthorTransformer extends TransformerAbstract
{
public function transform(Author $author)
{
return [
'id' => $author->getId(),
'name' => $author->getName(),
];
}
}
<?php namespace Example\Transformers;
use League\Fractal\TransformerAbstract;
use Example\Book;
class BookTransformer extends TransformerAbstract
{
protected $availableIncludes = [
'author',
];
public function transform(Book $book)
{
return [
'id' => $book->getId(),
'name' => $book->getName(),
];
}
public function includeAuthor(Book $book)
{
if ($book->getAuthor() === null) {
return $this->null();
}
return $this->item($book->getAuthor(), new AuthorTransformer, 'people');
}
}
<?php
require 'vendor/autoload.php';
use Example\Author;
use Example\Book;
use Example\Transformers\BookTransformer;
use League\Fractal\Manager;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Scope;
use League\Fractal\Serializer\JsonApiSerializer;
$rowling = new Author(1, 'J. K. Rowling');
$sturgeon = new Author(2, 'Phil Sturgeon');
$books = [
new Book(1, 'Harry Potter and the Prisoner of Azkaban', $rowling),
new Book(2, 'Harry Potter and the Half-Blood Prince', $rowling),
new Book(3, 'Build APIs You Won\'t Hate', $sturgeon),
new Book(4, 'The Bible', null),
];
$manager = new Manager;
$manager->setSerializer(new JsonApiSerializer);
$manager->parseIncludes('author');
$resource = new Item($books[0], new BookTransformer, 'book');
$scope = new Scope($manager, $resource);
$json = $scope->toJson();
echo $json;
{
"data": [{
"type": "books",
"id": "1",
"attributes": {
"name": "Harry Potter and the Prisoner of Azkaban"
},
"relationships": {
"author": {
"data": {
"type": "people",
"id": "1"
}
}
}
}, {
"type": "books",
"id": "2",
"attributes": {
"name": "Harry Potter and the Half-Blood Prince"
},
"relationships": {
"author": {
"data": {
"type": "people",
"id": "1"
}
}
}
}, {
"type": "books",
"id": "3",
"attributes": {
"name": "Build APIs You Won't Hate"
},
"relationships": {
"author": {
"data": {
"type": "people",
"id": "2"
}
}
}
}, {
"type": "books",
"id": "4",
"attributes": {
"name": "The Bible"
},
"relationships": {
"author": {
"data": null
}
}
}],
"included": [{
"type": "people",
"id": "1",
"attributes": {
"name": "J. K. Rowling"
}
}, {
"type": "people",
"id": "2",
"attributes": {
"name": "Phil Sturgeon"
}
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment