PaginateTrait::offset()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 2
b 0
f 0
nc 16
nop 1
dl 0
loc 28
ccs 12
cts 12
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
/**
6
 * @property \CI_Config           $config
7
 * @property \CI_Input            $input
8
 * @property \CI_Loader           $load
9
 * @property array<string, mixed> $pagee
10
 * @property \CI_Pagination       $pagination
11
 * @property \CI_URI              $uri
12
 *
13
 * @package Wildfire
14
 *
15
 * @author Rougin Gutib <[email protected]>
16
 */
17
trait PaginateTrait
18
{
19
    /**
20
     * Limits the data based on given configuration and generates pagination links.
21
     *
22
     * @param integer              $limit
23
     * @param integer              $total
24
     * @param array<string, mixed> $config
25
     *
26
     * @return array<integer, integer|string>
27 3
     */
28
    public function paginate($limit, $total, $config = array())
29 3
    {
30
        $this->load->library('pagination');
31 3
32
        $pagination = $this->pagination;
33 3
34
        $config = $this->prepare($config);
35 3
36
        $config['per_page'] = (int) $limit;
37 3
38
        $config['total_rows'] = $total;
39 3
40
        $offset = $this->offset($config);
41 3
42
        $pagination->initialize($config);
43 3
44
        $links = $pagination->create_links();
45 3
46
        return array($offset, $links);
47
    }
48
49
    /**
50
     * Returns the offset from the defined configuration.
51
     *
52
     * @param array<string, mixed> $config
53
     *
54
     * @return integer
55 3
     */
56
    protected function offset($config)
57 3
    {
58
        /** @var integer */
59 3
        $segment = $config['uri_segment'];
60 3
61
        /** @var integer */
62 3
        $limit = $config['per_page'];
63 3
64
        /** @var integer */
65 3
        $offset = $this->uri->segment($segment);
66
67 3
        if (array_key_exists('page_query_string', $config))
68
        {
69
            /** @var string */
70
            $segment = $config['query_string_segment'];
71
72
            /** @var integer */
73
            $offset = $this->input->get($segment);
74
        }
75
76 3
        $hasOffset = $offset !== 0 && $offset !== null;
77
78 3
        /** @var boolean */
79
        $useNumbers = $config['use_page_numbers'];
80 3
81
        $numbers = $useNumbers && $hasOffset;
82 3
83 3
        return $numbers ? ($limit * $offset) - $limit : $offset;
84 3
    }
85 3
86
    /**
87 3
     * Returns the pagination configuration.
88 3
     *
89 3
     * @param array<string, mixed> $config
90 3
     *
91 3
     * @return array<string, mixed>
92 3
     */
93 3
    protected function prepare($config)
94
    {
95 3
        if (isset($this->pagee))
96
        {
97
            $config = array_merge($config, $this->pagee);
98
        }
99
100
        $this->load->helper('url');
101
102
        $items = array('uri_segment' => 3);
103
104
        $items['page_query_string'] = false;
105
        $items['query_string_segment'] = 'per_page';
106
        $items['use_page_numbers'] = false;
107
108
        return array_merge($items, $config);
109
    }
110
}
111