Resource   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 22
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toCollection() 0 3 1
A collection() 0 5 2
1
<?php
2
3
namespace Shamaseen\Repository\Utility;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Http\Resources\Json\JsonResource;
7
use JsonSerializable;
8
use Shamaseen\Repository\Utility\Resources\AnonymousResourceCollection;
9
10
class Resource extends JsonResource
11
{
12
    /**
13
     * The array to check when a collection is called.
14
     */
15
    public function toCollection(\Illuminate\Http\Request $request): array|Arrayable|JsonSerializable
16
    {
17
        return $this->toArray($request);
18
    }
19
20
    /**
21
     * Create a new anonymous resource collection.
22
     *
23
     * @param mixed $resource
24
     *
25
     * @return AnonymousResourceCollection
26
     */
27
    public static function collection($resource)
28
    {
29
        return tap(new AnonymousResourceCollection($resource, static::class), function ($collection) {
30
            if (property_exists(static::class, 'preserveKeys')) {
31
                $collection->preserveKeys = true === (new static([]))->preserveKeys;
32
            }
33
        });
34
    }
35
}
36