Page::getData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\Lumen\GraphQL\Models;
4
5
class Page
6
{
7
8
    const DEFAULT_SIZE = 10;
9
10
    /**
11
     * The actual data.
12
     *
13
     * @var array
14
     */
15
    private $data;
16
17
    /**
18
     * The amount of total items on this page.
19
     *
20
     * @var int
21
     */
22
    private $total;
23
24
    /**
25
     * The index of the first index on this page. (defaults to 0)
26
     *
27
     * @var int
28
     */
29
    private $from;
30
31
    /**
32
     * Page constructor.
33
     *
34
     * @param array    $data
35
     * @param int|null $total
36
     * @param int      $from
37
     */
38
    public function __construct(array $data, $total = null, $from = 0)
39
    {
40
        $this->data  = $this->keyData($data, $from);
41
        $this->total = $total ?: count($data);
42
        $this->from  = $from;
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48
    public function getFirst()
49
    {
50
        return !empty($this->data) ? head($this->data) : null;
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    public function getData()
57
    {
58
        return $this->data;
59
    }
60
61
    /**
62
     * @return int
63
     */
64
    public function getTotal()
65
    {
66
        return $this->total;
67
    }
68
69
    /**
70
     * @return int
71
     */
72
    public function getFrom()
73
    {
74
        return $this->from;
75
    }
76
77
    /**
78
     * @param array $data
79
     * @param int   $from
80
     *
81
     * @return array
82
     */
83
    private function keyData(array $data, $from)
84
    {
85
        $newData = [];
86
87
        foreach ($data as $index => $item) {
88
            $newData[(string)($from + $index)] = $item;
89
        }
90
91
        return $newData;
92
    }
93
}
94