LinksTrait   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.18%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 0
loc 122
ccs 41
cts 44
cp 0.9318
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLinks() 0 4 1
A setLinks() 0 6 1
A addLink() 0 6 1
B addPaginationLinks() 0 20 6
C addPaginationLink() 0 30 8
1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
trait LinksTrait
15
{
16
    /**
17
     * The links array.
18
     *
19
     * @var array
20
     */
21
    protected $links;
22
23
    /**
24
     * Get the links.
25
     *
26
     * @return array
27
     */
28 3
    public function getLinks()
29
    {
30 3
        return $this->links;
31
    }
32
33
    /**
34
     * Set the links.
35
     *
36
     * @param array $links
37
     *
38
     * @return $this
39
     */
40
    public function setLinks(array $links)
41
    {
42
        $this->links = $links;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Add a link.
49
     *
50
     * @param string $key
51
     * @param string $value
52
     *
53
     * @return $this
54
     */
55 6
    public function addLink($key, $value)
56
    {
57 6
        $this->links[$key] = $value;
58
59 6
        return $this;
60
    }
61
62
    /**
63
     * Add pagination links (first, prev, next, and last).
64
     *
65
     * @param string $url The base URL for pagination links.
66
     * @param array $queryParams The query params provided in the request.
67
     * @param int $offset The current offset.
68
     * @param int $limit The current limit.
69
     * @param int|null $total The total number of results, or null if unknown.
70
     *
71
     * @return void
72
     */
73 3
    public function addPaginationLinks($url, array $queryParams, $offset, $limit, $total = null)
74
    {
75 3
        if (isset($queryParams['page']['number'])) {
76 3
            $offset = floor($offset / $limit) * $limit;
77 3
        }
78
79 3
        $this->addPaginationLink('first', $url, $queryParams, 0, $limit);
80
81 3
        if ($offset > 0) {
82 3
            $this->addPaginationLink('prev', $url, $queryParams, max(0, $offset - $limit), $limit);
83 3
        }
84
85 3
        if ($total === null || $offset + $limit < $total) {
86 3
            $this->addPaginationLink('next', $url, $queryParams, $offset + $limit, $limit);
87 3
        }
88
89 3
        if ($total) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $total of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
90 3
            $this->addPaginationLink('last', $url, $queryParams, floor(($total - 1) / $limit) * $limit, $limit);
91 3
        }
92 3
    }
93
94
    /**
95
     * Add a pagination link.
96
     *
97
     * @param string $name The name of the link.
98
     * @param string $url The base URL for pagination links.
99
     * @param array $queryParams The query params provided in the request.
100
     * @param int $offset The offset to link to.
101
     * @param int $limit The current limit.
102
     *
103
     * @return void
104
     */
105 3
    protected function addPaginationLink($name, $url, array $queryParams, $offset, $limit)
106
    {
107 3
        if (! isset($queryParams['page']) || ! is_array($queryParams['page'])) {
108 3
            $queryParams['page'] = [];
109 3
        }
110
111 3
        $page = &$queryParams['page'];
112
113 3
        if (isset($page['number'])) {
114 3
            $page['number'] = floor($offset / $limit) + 1;
115
116 3
            if ($page['number'] <= 1) {
117 3
                unset($page['number']);
118 3
            }
119 3
        } else {
120 3
            $page['offset'] = $offset;
121
122 3
            if ($page['offset'] <= 0) {
123 3
                unset($page['offset']);
124 3
            }
125
        }
126
127 3
        if (isset($page['limit'])) {
128 3
            $page['limit'] = $limit;
129 3
        }
130
131 3
        $queryString = http_build_query($queryParams);
132
133 3
        $this->addLink($name, $url.($queryString ? '?'.$queryString : ''));
134 3
    }
135
}
136