Skip to content

Instantly share code, notes, and snippets.

@felixkiss
Last active August 30, 2015 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save felixkiss/165e532cd5f399283531 to your computer and use it in GitHub Desktop.
Save felixkiss/165e532cd5f399283531 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;
private $publishedBooks;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
$this->publishedBooks = [];
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getPublishedBooks()
{
return $this->publishedBooks;
}
public function publish(Book $book)
{
$publishedBook = new Book($book->getId(), $book->getTitle(), $this);
$this->publishedBooks[] = $publishedBook;
return $publishedBook;
}
}
<?php namespace Example;
class Book
{
private $id;
private $title;
private $author;
public function __construct($id, $title, Author $author = null)
{
$this->id = $id;
$this->title = $title;
$this->author = $author;
}
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function getAuthor()
{
return $this->author;
}
}
<?php namespace Example\Transformers;
use League\Fractal\TransformerAbstract;
use Example\Author;
class AuthorTransformer extends TransformerAbstract
{
protected $availableIncludes = [
'published',
];
public function transform(Author $author)
{
return [
'id' => $author->getId(),
'name' => $author->getName(),
];
}
public function includePublished(Author $author)
{
$books = $author->getPublishedBooks() ?: [];
return $this->collection($books, new BookTransformer, 'books');
}
}
<?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(),
'title' => $book->getTitle(),
];
}
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 = [
$rowling->publish(new Book(1, 'Harry Potter and the Prisoner of Azkaban')),
$rowling->publish(new Book(2, 'Harry Potter and the Half-Blood Prince')),
$sturgeon->publish(new Book(3, 'Build APIs You Won\'t Hate')),
new Book(4, 'The Bible'),
];
$manager = new Manager;
$manager->setSerializer(new JsonApiSerializer);
$manager->parseIncludes('author.published');
$resource = new Item($books[0], new BookTransformer, 'books');
$scope = new Scope($manager, $resource);
$json = $scope->toJson();
echo $json;
{
"data": {
"type": "books",
"id": "1",
"attributes": {
"title": "Harry Potter and the Prisoner of Azkaban"
},
"relationships": {
"author": {
"data": {
"type": "people",
"id": "1"
}
}
}
},
"included": [{
"type": "books",
"id": "2",
"attributes": {
"title": "Harry Potter and the Half-Blood Prince"
}
}, {
"type": "people",
"id": "1",
"attributes": {
"name": "J. K. Rowling"
},
"relationships": {
"published": {
"data": [{
"type": "books",
"id": "1"
}, {
"type": "books",
"id": "2"
}]
}
}
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment