Completed
Branch dev (276354)
by Raffael
15:43
created

Pager::paging()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\AttributeDecorator;
13
14
use Generator;
15
16
class Pager
17
{
18
    /**
19
     * Decorator.
20
     *
21
     * @var AttributeDecoratorInterface
22
     */
23
    protected $decorator;
24
25
    /**
26
     * Data.
27
     *
28
     * @var iterable
29
     */
30
    protected $data;
31
32
    /**
33
     * Attributes.
34
     *
35
     * @var array
36
     */
37
    protected $attributes;
38
39
    /**
40
     * Offset.
41
     *
42
     * @var int
43
     */
44
    protected $offset;
45
46
    /**
47
     * Limit.
48
     *
49
     * @var int
50
     */
51
    protected $limit;
52
53
    /**
54
     * Total.
55
     *
56
     * @var int
57
     */
58
    protected $total;
59
60
    /**
61
     * URI.
62
     *
63
     * @var string
64
     */
65
    protected $uri;
66
67
    /**
68
     * Init.
69
     *
70
     * @param AttributeDecoratorInterface $decorator
71
     * @param iterable                    $data
72
     * @param array                       $attributes
73
     * @param int                         $offset
74
     * @param int                         $limit
75
     * @param string                      $uri
76
     * @param int                         $total
77
     */
78
    public function __construct(AttributeDecoratorInterface $decorator, Iterable $data, array $attributes, int $offset, int $limit, string $uri, ?int $total = null)
79
    {
80
        $this->decorator = $decorator;
81
        $this->data = $data;
82
        $this->attributes = $attributes;
83
        $this->offset = $offset;
84
        $this->limit = $limit;
85
        $this->total = $total;
86
        $this->uri = $uri;
87
    }
88
89
    /**
90
     * Pagin.
91
     *
92
     * @return array
93
     */
94
    public function paging(): array
95
    {
96
        $nodes = [];
97
        $count = 0;
98
99
        foreach ($this->data as $node) {
100
            ++$count;
101
            $nodes[] = $this->decorator->decorate($node, $this->attributes);
102
        }
103
104
        if ($this->total === null && $this->data instanceof Generator) {
105
            $this->total = $this->data->getReturn();
106
        }
107
108
        $data = [
109
            '_links' => $this->getLinks($count),
110
            'count' => $count,
111
            'total' => $this->total,
112
            'data' => $nodes,
113
        ];
114
115
        return $data;
116
    }
117
118
    /**
119
     * Get paging links.
120
     *
121
     * @param int $count
122
     *
123
     * @return array
124
     */
125
    protected function getLinks(int $count): array
126
    {
127
        $links = [
128
            'self' => ['href' => $this->uri.'?offset='.$this->offset.'&limit='.$this->limit],
129
        ];
130
131
        if ($this->offset > 0) {
132
            $offset = $this->offset - $this->offset;
133
            if ($offset < 0) {
134
                $offset = 0;
135
            }
136
137
            $links['prev'] = [
138
                'href' => $this->uri.'?offset='.$offset.'&limit='.$this->limit,
139
            ];
140
        }
141
142
        if ($this->offset + $count < $this->total) {
143
            $links['next'] = [
144
                'href' => $this->uri.'?offset='.($this->offset + $count).'&limit='.$this->limit,
145
            ];
146
        }
147
148
        return $links;
149
    }
150
}
151