Pagination::getOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace VGirol\JsonApiFaker\Laravel\Helpers;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * This class provides a lot of tools to fake pagination
9
 */
10
class Pagination
11
{
12
    /**
13
     * Get all the options needed for pagination.
14
     *
15
     * Returns an array with these keys :
16
     * - itemCount (integer)
17
     * - pageCount (integer)
18
     * - page (integer)
19
     * - itemPerPage (integer)
20
     * - routeParameters (array)
21
     *
22
     * @param array $options
23
     *
24
     * @return array
25
     */
26 24
    public static function getOptions($options = []): array
27
    {
28 24
        foreach (static::getDefaultOptions() as $key => $value) {
29 24
            if (!isset($options[$key])) {
30 24
                $options[$key] = $value;
31
            }
32
        }
33
34 24
        $options['pageCount'] = self::getPageCount($options['itemCount'], $options['itemPerPage']);
35
36 24
        return $options;
37
    }
38
39
    /**
40
     * Slice the given collection according to the options passed as second argument.
41
     *
42
     * @param Collection $collection
43
     * @param array      $options
44
     *
45
     * @return Collection
46
     */
47 15
    public static function sliceCollection($collection, $options)
48
    {
49 15
        if ($options['itemCount'] == 0) {
50 3
            return $collection;
51
        }
52
53 12
        $start = intval(($options['page'] - 1) * $options['itemPerPage']);
54 12
        if ($start > $options['itemCount']) {
55 3
            $start = 0;
56
        }
57
58 12
        return $collection->slice(
59 12
            $start,
60 12
            static::getItemCount($options)
61 12
        )->values();
62
    }
63
64
    /**
65
     * Gets the default options for pagination
66
     *
67
     * @return array
68
     */
69 24
    protected static function getDefaultOptions(): array
70
    {
71
        return [
72 24
            'itemCount'       => null,
73
            'pageCount'       => null,
74
            'page'            => 1,
75
            'itemPerPage'     => null,
76
            'routeParameters' => [],
77
        ];
78
    }
79
80
    /**
81
     * Get the page count
82
     *
83
     * @param int|null $colCount
84
     * @param int|null $itemPerPage
85
     *
86
     * @return int
87
     */
88 24
    private static function getPageCount(?int $colCount, ?int $itemPerPage): int
89
    {
90 24
        if (($colCount === null) || ($itemPerPage === null) || ($itemPerPage == 0)) {
91 9
            return 1;
92
        }
93
94 15
        $pageCount = intdiv($colCount, $itemPerPage);
95 15
        if ($colCount % $itemPerPage != 0) {
96 15
            $pageCount++;
97
        }
98 15
        if ($pageCount == 0) {
99
            $pageCount = 1;
100
        }
101
102 15
        return $pageCount;
103
    }
104
105
    /**
106
     * Calculate the item count
107
     *
108
     * @param array $options
109
     *
110
     * @return int
111
     */
112 12
    private static function getItemCount($options): int
113
    {
114 12
        if ($options['pageCount'] <= 1) {
115 3
            return $options['itemCount'];
116
        }
117
118 9
        if ($options['page'] == $options['pageCount']) {
119 3
            return $options['itemCount'] - ($options['page'] - 1) * $options['itemPerPage'];
120
        }
121
122 6
        return $options['itemPerPage'];
123
    }
124
}
125