Issues (995)

app/Http/Resources/MovieCollection.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Http\Resources;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Resources\Json\ResourceCollection;
7
8
/**
9
 * Movie Collection Resource for API responses.
10
 */
11
class MovieCollection extends ResourceCollection
12
{
13
    /**
14
     * The resource that this resource collects.
15
     *
16
     * @var string
17
     */
18
    public $collects = MovieResource::class;
19
20
    /**
21
     * Transform the resource collection into an array.
22
     *
23
     * @return array<int|string, mixed>
24
     */
25
    public function toArray(Request $request): array
26
    {
27
        return [
28
            'data' => $this->collection,
29
            'meta' => [
30
                'total' => $this->total(),
31
                'per_page' => $this->perPage(),
32
                'current_page' => $this->currentPage(),
33
                'last_page' => $this->lastPage(),
34
            ],
35
            'links' => [
36
                'first' => $this->url(1),
37
                'last' => $this->url($this->lastPage()),
38
                'prev' => $this->previousPageUrl(),
39
                'next' => $this->nextPageUrl(),
40
            ],
41
        ];
42
    }
43
44
    /**
45
     * Get total items count when available.
46
     */
47
    protected function total(): int
48
    {
49
        if (method_exists($this->resource, 'total')) {
50
            return $this->resource->total();
51
        }
52
53
        return $this->collection->count();
0 ignored issues
show
The method count() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        return $this->collection->/** @scrutinizer ignore-call */ count();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
    }
55
56
    /**
57
     * Get items per page when available.
58
     */
59
    protected function perPage(): int
60
    {
61
        if (method_exists($this->resource, 'perPage')) {
62
            return $this->resource->perPage();
63
        }
64
65
        return $this->collection->count();
66
    }
67
68
    /**
69
     * Get current page when available.
70
     */
71
    protected function currentPage(): int
72
    {
73
        if (method_exists($this->resource, 'currentPage')) {
74
            return $this->resource->currentPage();
75
        }
76
77
        return 1;
78
    }
79
80
    /**
81
     * Get last page when available.
82
     */
83
    protected function lastPage(): int
84
    {
85
        if (method_exists($this->resource, 'lastPage')) {
86
            return $this->resource->lastPage();
87
        }
88
89
        return 1;
90
    }
91
92
    /**
93
     * Get URL for a specific page.
94
     */
95
    protected function url(int $page): ?string
96
    {
97
        if (method_exists($this->resource, 'url')) {
98
            return $this->resource->url($page);
99
        }
100
101
        return null;
102
    }
103
104
    /**
105
     * Get previous page URL.
106
     */
107
    protected function previousPageUrl(): ?string
108
    {
109
        if (method_exists($this->resource, 'previousPageUrl')) {
110
            return $this->resource->previousPageUrl();
111
        }
112
113
        return null;
114
    }
115
116
    /**
117
     * Get next page URL.
118
     */
119
    protected function nextPageUrl(): ?string
120
    {
121
        if (method_exists($this->resource, 'nextPageUrl')) {
122
            return $this->resource->nextPageUrl();
123
        }
124
125
        return null;
126
    }
127
}
128
129