Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 8925e4d

Browse files
committed
wip
1 parent 7fb2f3f commit 8925e4d

File tree

7 files changed

+240
-212
lines changed

7 files changed

+240
-212
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace SkoreLabs\JsonApi\Http\Resources;
4+
5+
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
trait Authorizable
9+
{
10+
/**
11+
* Determine whether authorize to view this resource.
12+
*
13+
* @var bool
14+
*/
15+
public $authorize;
16+
17+
/**
18+
* Authorize to view this resource.
19+
*
20+
* @param mixed $resource
21+
* @return void
22+
*/
23+
protected function authorize($resource)
24+
{
25+
$this->authorize = $this->authorize
26+
?: ! $resource instanceof Model
27+
?: Auth::user()->can('view', $resource);
28+
29+
return $this->authorize;
30+
}
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace SkoreLabs\JsonApi\Http\Resources;
4+
5+
use Illuminate\Support\{Arr, Collection};
6+
7+
trait CollectsWithIncludes
8+
{
9+
/**
10+
* Attach with the collects' resource models relationships.
11+
*
12+
* @return void
13+
*/
14+
protected function withIncludes()
15+
{
16+
$this->with['included'] = [];
17+
18+
/** @var \SkoreLabs\JsonApi\Http\Resources\JsonApiResource $jsonResource */
19+
foreach ($this->collection->toArray() as $jsonResource) {
20+
if ($jsonResource->with) {
21+
$this->with['included'][] = Arr::get($jsonResource->with, 'included');
22+
}
23+
}
24+
25+
$this->with['included'] = $this->uniqueIncludes();
26+
}
27+
28+
/**
29+
* Post-process for unique relationship includes.
30+
*
31+
* @return array
32+
*/
33+
protected function uniqueIncludes()
34+
{
35+
$includedCollection = Collection::make($this->with['included'])->flatten();
36+
37+
return $includedCollection->unique(static function (JsonApiResource $resource) {
38+
return implode('', $resource->getResourceIdentifier());
39+
})->values()->all();
40+
}
41+
}

src/Http/Resources/CollectsWithRelationships.php

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace SkoreLabs\JsonApi\Http\Resources\Json;
4+
5+
use Countable;
6+
use IteratorAggregate;
7+
use Illuminate\Pagination\AbstractPaginator;
8+
use Illuminate\Http\Resources\CollectsResources;
9+
use Illuminate\Support\Facades\Auth;
10+
use SkoreLabs\JsonApi\Http\Resources\JsonApiResource;
11+
12+
class ResourceCollection extends JsonApiResource implements Countable, IteratorAggregate
13+
{
14+
use CollectsResources;
15+
16+
/**
17+
* The resource that this resource collects.
18+
*
19+
* @var string
20+
*/
21+
public $collects;
22+
23+
/**
24+
* The mapped collection instance.
25+
*
26+
* @var \Illuminate\Support\Collection
27+
*/
28+
public $collection;
29+
30+
/**
31+
* Create a new resource instance.
32+
*
33+
* @param mixed $resource
34+
* @return void
35+
*/
36+
public function __construct($resource)
37+
{
38+
parent::__construct($resource, Auth::user()->can('viewAny', get_class($resource)));
39+
40+
$this->resource = $this->collectResource($resource);
41+
}
42+
43+
/**
44+
* Return the count of items in the resource collection.
45+
*
46+
* @return int
47+
*/
48+
public function count()
49+
{
50+
return $this->collection->count();
51+
}
52+
53+
/**
54+
* Transform the resource into a JSON array.
55+
*
56+
* @param \Illuminate\Http\Request $request
57+
* @return array
58+
*/
59+
public function toArray($request)
60+
{
61+
return $this->collection->map->toArray($request)->all();
62+
}
63+
64+
/**
65+
* Create an HTTP response that represents the object.
66+
*
67+
* @param \Illuminate\Http\Request $request
68+
* @return \Illuminate\Http\JsonResponse
69+
*/
70+
public function toResponse($request)
71+
{
72+
return $this->resource instanceof AbstractPaginator
73+
? (new PaginatedResourceResponse($this))->toResponse($request)
74+
: parent::toResponse($request);
75+
}
76+
}

src/Http/Resources/JsonApiCollection.php

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace SkoreLabs\JsonApi\Http\Resources;
44

5-
use Illuminate\Http\Resources\Json\ResourceCollection;
5+
use SkoreLabs\JsonApi\Http\Resources\Json\ResourceCollection;
66

77
class JsonApiCollection extends ResourceCollection
88
{
9-
use CollectsWithRelationships;
9+
use CollectsWithIncludes;
1010

1111
/**
1212
* Create a new resource instance.
@@ -19,37 +19,7 @@ public function __construct($resource)
1919
$this->collects = JsonApiResource::class;
2020

2121
parent::__construct($resource);
22-
23-
$this->processRelations();
24-
}
25-
26-
/**
27-
* Transform the resource into an array.
28-
*
29-
* @param \Illuminate\Http\Request $request
30-
* @return array
31-
*/
32-
public function toArray($request)
33-
{
34-
//
3522

36-
return JsonApiResource::collection(
37-
$this->collection
38-
);
39-
}
40-
41-
/**
42-
* Get any additional data that should be returned with the resource array.
43-
*
44-
* @param \Illuminate\Http\Request $request
45-
* @return array
46-
*/
47-
public function with($request)
48-
{
49-
return [
50-
'included' => $this->when(
51-
$this->included, $this->included
52-
),
53-
];
23+
$this->withIncludes();
5424
}
55-
}
25+
}

0 commit comments

Comments
 (0)