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

Commit d0da582

Browse files
committed
Little refactor and big performance improvement on collections
1 parent c0f15d3 commit d0da582

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/Http/Resources/CollectsWithIncludes.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Illuminate\Support\{Arr, Collection};
66

7+
/**
8+
* @property array $with
9+
*/
710
trait CollectsWithIncludes
811
{
912
/**
@@ -13,12 +16,10 @@ trait CollectsWithIncludes
1316
*/
1417
protected function withIncludes()
1518
{
16-
$this->with['included'] = [];
17-
1819
/** @var \SkoreLabs\JsonApi\Http\Resources\JsonApiResource $jsonResource */
1920
foreach ($this->collection->toArray() as $jsonResource) {
2021
if ($jsonResource->with) {
21-
$this->with['included'][] = Arr::get($jsonResource->with, 'included');
22+
$this->addIncluded(Arr::get($jsonResource->with, 'included'));
2223
}
2324
}
2425

@@ -32,7 +33,9 @@ protected function withIncludes()
3233
*/
3334
protected function uniqueIncludes()
3435
{
35-
$includedCollection = Collection::make($this->with['included'])->flatten();
36+
$includedCollection = Collection::make(
37+
Arr::get($this->with, 'included', [])
38+
)->flatten();
3639

3740
return $includedCollection->unique(static function (JsonApiResource $resource) {
3841
return implode('', $resource->getResourceIdentifier());

src/Http/Resources/JsonApiResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class JsonApiResource extends JsonResource
2424
* @param bool $authorize
2525
* @return void
2626
*/
27-
public function __construct($resource, $authorize = false)
27+
public function __construct($resource, $authorize = null)
2828
{
2929
if (gettype($authorize) === 'boolean') {
3030
$this->authorize = $authorize;
@@ -80,7 +80,7 @@ public function getResourceIdentifier()
8080
{
8181
return [
8282
$this->resource->getKeyName() => (string) $this->resource->getKey(),
83-
'type' => Str::lower(class_basename($this->resource)),
83+
'type' => Str::snake(class_basename($this->resource)),
8484
];
8585
}
8686

src/Http/Resources/RelationshipsWithIncludes.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace SkoreLabs\JsonApi\Http\Resources;
44

55
use Illuminate\Database\Eloquent\{Model, Collection, Relations\Pivot};
6-
use Illuminate\Pagination\LengthAwarePaginator;
76
use Illuminate\Support\Arr;
87

98
/**
109
* @property mixed $resource
10+
* @property array $with
11+
* @property bool $authorize
1112
*/
1213
trait RelationshipsWithIncludes
1314
{
@@ -25,11 +26,7 @@ trait RelationshipsWithIncludes
2526
*/
2627
protected function withRelationships()
2728
{
28-
if ($this->resource instanceof LengthAwarePaginator) {
29-
$this->resource->getCollection()->map(function (Model $model) {
30-
$this->attachRelations($model, true);
31-
});
32-
} else if ($this->resource instanceof Model) {
29+
if ($this->resource instanceof Model) {
3330
$this->attachRelations($this->resource);
3431
}
3532
}
@@ -72,12 +69,23 @@ protected function processModelRelation(Model $model)
7269
{
7370
$modelResource = new JsonApiResource($model, $this->authorize);
7471

75-
$this->with['included'][] = $modelResource;
76-
77-
if (Arr::has($modelResource->with, 'included')) {
78-
$this->with['included'][] = $modelResource->with['included'];
79-
}
72+
$this->addIncluded([$modelResource],
73+
Arr::get($modelResource->with, 'included', [])
74+
);
8075

8176
return $modelResource->getResourceIdentifier();
8277
}
78+
79+
/**
80+
* Set included data to resource's with.
81+
*
82+
* @param array $arrays,...
83+
* @return void
84+
*/
85+
protected function addIncluded(...$arrays)
86+
{
87+
Arr::set($this->with, 'included',
88+
array_merge(Arr::get($this->with, 'included', []), ...$arrays)
89+
);
90+
}
8391
}

0 commit comments

Comments
 (0)