PaginatedResource   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 16
c 6
b 0
f 0
lcom 1
cbo 0
dl 0
loc 213
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setPageCursor() 0 4 1
A setData() 0 14 3
A setLinks() 0 8 2
A setPageOffset() 0 4 1
A setPageOffsetLimit() 0 4 1
A setCurrentPage() 0 4 1
A setPageSize() 0 4 1
A setTotal() 0 4 1
A setTotalPages() 0 10 2
A setMeta() 0 4 1
B jsonSerialize() 0 30 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/21/15
5
 * Time: 9:04 AM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi\Http;
11
12
use JsonSerializable;
13
14
/**
15
 * Class PaginatedResource.
16
 */
17
class PaginatedResource implements JsonSerializable
18
{
19
    /**
20
     * @var int
21
     */
22
    protected $total;
23
    /**
24
     * @var int
25
     */
26
    protected $pages;
27
    /**
28
     * @var int
29
     */
30
    protected $currentPage;
31
    /**
32
     * @var int
33
     */
34
    protected $pageSize;
35
    /**
36
     * @var string
37
     */
38
    protected $offsetLimit;
39
40
    /**
41
     * @var string
42
     */
43
    protected $offset;
44
45
    /**
46
     * @var string
47
     */
48
    protected $cursor;
49
50
    /**
51
     * @var string
52
     */
53
    protected $data = [];
54
    /**
55
     * @var array
56
     */
57
    protected $include = [];
58
    /**
59
     * @var array
60
     */
61
    protected $links = [];
62
    /**
63
     * @var array
64
     */
65
    protected $meta = [];
66
67
    /**
68
     * @param string $elements
69
     * @param int    $currentPage
70
     * @param int    $pageSize
71
     * @param null   $total
72
     * @param array  $links
73
     */
74
    public function __construct($elements, $currentPage = null, $pageSize = null, $total = null, array $links = [])
75
    {
76
        $this->setData($elements);
0 ignored issues
show
Documentation introduced by
$elements is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        $this->setPageSize($pageSize);
78
        $this->setCurrentPage($currentPage);
79
        $this->setTotal($total);
80
        $this->setTotalPages($total, $pageSize);
81
        $this->setLinks($links);
82
    }
83
84
    /**
85
     * @param string $cursor
86
     *
87
     * @return $this
88
     */
89
    public function setPageCursor($cursor)
90
    {
91
        $this->cursor = $cursor;
92
    }
93
94
    /**
95
     * @param array $data
96
     *
97
     * @throws \InvalidArgumentException
98
     *
99
     * @return $this
100
     */
101
    protected function setData($data)
102
    {
103
        $data = (array) json_decode($data, true);
104
105
        if (false === array_key_exists('data', $data)) {
106
            throw new \InvalidArgumentException('Provided JSON has no `data` member defined');
107
        }
108
109
        $this->data = $data['data'];
110
111
        if (!empty($data['included'])) {
112
            $this->include = $data['included'];
113
        }
114
    }
115
116
    /**
117
     * @param array $links
118
     *
119
     * @return $this
120
     */
121
    public function setLinks(array $links)
122
    {
123
        foreach ($links as &$href) {
124
            $href = ['href' => $href];
125
        }
126
127
        $this->links = $links;
128
    }
129
130
    /**
131
     * @param string $offset
132
     *
133
     * @return $this
134
     */
135
    public function setPageOffset($offset)
136
    {
137
        $this->offset = $offset;
138
    }
139
140
    /**
141
     * @param string $offsetLimit
142
     *
143
     * @return $this
144
     */
145
    public function setPageOffsetLimit($offsetLimit)
146
    {
147
        $this->offsetLimit = $offsetLimit;
148
    }
149
150
    /**
151
     * @param $currentPage
152
     */
153
    protected function setCurrentPage($currentPage)
154
    {
155
        $this->currentPage = (int) $currentPage;
156
    }
157
158
    /**
159
     * @param $pageSize
160
     */
161
    protected function setPageSize($pageSize)
162
    {
163
        $this->pageSize = (int) $pageSize;
164
    }
165
    /**
166
     * @param $total
167
     */
168
    protected function setTotal($total)
169
    {
170
        $this->total = (int) $total;
171
    }
172
173
    /**
174
     * @param $total
175
     * @param $pageSize
176
     */
177
    protected function setTotalPages($total, $pageSize)
178
    {
179
        if (0 == $pageSize) {
180
            $this->pages = 0;
181
182
            return;
183
        }
184
185
        $this->pages = (int) ceil($total / $pageSize);
186
    }
187
188
    /**
189
     * @param array $meta
190
     */
191
    public function setMeta(array $meta)
192
    {
193
        $this->meta = $meta;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function jsonSerialize()
200
    {
201
        $callable = function ($v) {
202
            return $v !== null;
203
        };
204
205
        $data = array_filter([
206
                'data' => array_filter($this->data, $callable),
207
                'included' => array_filter($this->include),
208
                'links' => array_filter($this->links),
209
                'meta' => array_filter(
210
                    array_merge([
211
                            'page' => array_filter([
212
                                    'total' => $this->total,
213
                                    'last' => $this->pages,
214
                                    'number' => $this->currentPage,
215
                                    'size' => $this->pageSize,
216
                                    'limit' => $this->offsetLimit,
217
                                    'offset' => $this->offset,
218
                                    'cursor' => $this->cursor,
219
                                ], $callable),
220
                        ],
221
                        $this->meta
222
                    )
223
                ),
224
                'jsonapi' => ['version' => '1.0'],
225
            ], $callable);
226
227
        return $data;
228
    }
229
}
230