-
-
Save graemetait/a6f12ece0fcc58e77202 to your computer and use it in GitHub Desktop.
Example usage of Fractal with objects instead of arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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