Skip to content
This repository was archived by the owner on Jan 17, 2022. It is now read-only.

Commit 7a3446b

Browse files
committed
Merge pull request #1 from NewwayLibs/master
respect resource key + default serializer
2 parents 3fe1b6a + b03fede commit 7a3446b

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ use League\Fractal\TransformerAbstract;
118118

119119
class UserTransformer extends TransformerAbstract
120120
{
121+
/**
122+
* Resource key.
123+
*
124+
* @var string
125+
*/
126+
protected $resourceKey = null;
127+
121128
/**
122129
* Turn this item object into a generic array.
123130
*

src/Generator/stubs/controller.stub

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ use {{transformer.fullName}};
66

77
class {{controller.name}} extends Controller
88
{
9+
/**
10+
* Resource key.
11+
*
12+
* @var string
13+
*/
14+
protected $resourceKey = null;
15+
916
/**
1017
* Eloquent model.
1118
*

src/Skeleton/BaseController.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ abstract class BaseController extends LaravelController
7070
*/
7171
protected $maximumLimit = false;
7272

73+
/**
74+
* Resource key.
75+
*
76+
* @var string
77+
*/
78+
protected $resourceKey = null;
79+
7380
/**
7481
* Constructor.
7582
*
@@ -82,6 +89,8 @@ public function __construct(Request $request)
8289
$this->fractal = new Manager();
8390
$this->request = $request;
8491

92+
$this->fractal->setSerializer($this->serializer());
93+
8594
if ($this->request->has('include')) {
8695
$this->fractal->parseIncludes(camel_case($this->request->input('include')));
8796
}
@@ -101,6 +110,16 @@ abstract protected function model();
101110
*/
102111
abstract protected function transformer();
103112

113+
/**
114+
* Serializer for the current model.
115+
*
116+
* @return \League\Fractal\Serializer\SerializerAbstract
117+
*/
118+
protected function serializer()
119+
{
120+
return new Serializer();
121+
}
122+
104123
/**
105124
* Display a listing of the resource.
106125
* GET /api/{resource}.
@@ -109,6 +128,8 @@ abstract protected function transformer();
109128
*/
110129
public function index()
111130
{
131+
$this->resourceKey = empty($this->resourceKey) ? 'data' : str_plural($this->resourceKey);
132+
112133
$with = $this->getEagerLoad();
113134
$skip = (int) $this->request->input('skip', 0);
114135
$limit = $this->calculateLimit();
@@ -128,7 +149,10 @@ public function index()
128149
*/
129150
public function store()
130151
{
131-
$data = $this->request->json()->get('data');
152+
$key = empty($this->resourceKey) ? 'data' : str_singular($this->resourceKey);
153+
154+
$data = $this->request->json()->get($key);
155+
132156
if (!$data) {
133157
return $this->errorWrongArgs('Empty data');
134158
}
@@ -175,7 +199,10 @@ public function show($id)
175199
*/
176200
public function update($id)
177201
{
178-
$data = $this->request->json()->get('data');
202+
$key = empty($this->resourceKey) ? 'data' : str_singular($this->resourceKey);
203+
204+
$data = $this->request->json()->get($key);
205+
179206
if (!$data) {
180207
return $this->errorWrongArgs('Empty data');
181208
}
@@ -274,7 +301,7 @@ protected function setStatusCode($statusCode)
274301
*/
275302
protected function respondWithItem($item)
276303
{
277-
$resource = new Item($item, $this->transformer);
304+
$resource = new Item($item, $this->transformer, $this->resourceKey);
278305

279306
$rootScope = $this->prepareRootScope($resource);
280307

@@ -292,7 +319,7 @@ protected function respondWithItem($item)
292319
*/
293320
protected function respondWithCollection($collection, $skip = 0, $limit = 0)
294321
{
295-
$resource = new Collection($collection, $this->transformer);
322+
$resource = new Collection($collection, $this->transformer, $this->resourceKey);
296323

297324
if ($limit) {
298325
$cursor = new Cursor($skip, $skip + $limit, $collection->count());
@@ -495,4 +522,4 @@ protected function calculateLimit()
495522

496523
return ($this->maximumLimit && $this->maximumLimit < $limit) ? $this->maximumLimit : $limit;
497524
}
498-
}
525+
}

src/Skeleton/Serializer.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Arrilot\Api\Skeleton;
4+
5+
use League\Fractal\Resource\ResourceInterface;
6+
use League\Fractal\Serializer\JsonApiSerializer;
7+
8+
class Serializer extends JsonApiSerializer
9+
{
10+
/**
11+
* Serialize the included data.
12+
*
13+
* @param \League\Fractal\Resource\ResourceInterface $resource
14+
* @param array $data
15+
*
16+
* @return array
17+
*/
18+
public function includedData(ResourceInterface $resource, array $data)
19+
{
20+
$serializedData = parent::includedData($resource, $data);
21+
22+
return isset($serializedData['linked']) ? $serializedData['linked'] : [];
23+
}
24+
}

templates/Api/Serializers/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)