ArrayPaginator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 70
ccs 17
cts 22
cp 0.7727
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A paginate() 0 24 4
A setSortHandler() 0 5 1
A sort() 0 9 2
1
<?php
2
/**
3
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
4
 *
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
10
 * @link          https://github.com/Phauthentic
11
 * @license       https://opensource.org/licenses/mit-license.php MIT License
12
 */
13
declare(strict_types = 1);
14
15
namespace Phauthentic\Pagination\Paginator;
16
17
use Phauthentic\Pagination\PaginationParams;
18
use Phauthentic\Pagination\PaginationParamsInterface;
19
20
/**
21
 * Paginates arrays
22
 */
23
class ArrayPaginator implements PaginatorInterface
24
{
25
    /**
26
     * Sort Handler callback
27
     *
28
     * @var callable
29
     */
30
    protected $sortHandler;
31
32
    /**
33
     * Maps the params to the repository
34
     *
35
     * @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params
36
     * @param array $array
37
     * @return array
38
     */
39 2
    public function paginate($array, PaginationParamsInterface $paginationParams)
40
    {
41 2
        $paginationParams->setCount(count($array));
42
43 2
        $count = 1;
44 2
        $data = [];
45 2
        $offset = $paginationParams->getOffset();
46 2
        $stopAt = $offset + $paginationParams->getLimit();
47
48 2
        $array = $this->sort($array, $paginationParams);
49
50 2
        foreach ($array as $key => $value) {
51 2
            if ($count > $stopAt) {
52 1
                break;
53
            }
54
55 2
            if ($count > $paginationParams->getOffset()) {
56 2
                $data[$key] = $value;
57
            }
58
59 2
            $count++;
60
        }
61
62 2
        return $data;
63
    }
64
65
    /**
66
     * Sorts the array by a callback
67
     *
68
     * @param array $array Array
69
     * @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params
70
     * @return array
71
     */
72 2
    protected function sort($array, PaginationParams $paginationParams)
73
    {
74 2
        if (empty($this->setSortHandler)) {
0 ignored issues
show
Bug introduced by
The property setSortHandler does not exist on Phauthentic\Pagination\Paginator\ArrayPaginator. Did you mean sortHandler?
Loading history...
75 2
            return $array;
76
        }
77
78
        $handler = $this->sortHandler;
79
80
        return $handler($array, $paginationParams);
81
    }
82
83
    /**
84
     * Sets the sort handler callback
85
     *
86
     * @return $this
87
     */
88
    public function setSortHandler(callable $handler): self
89
    {
90
        $this->sortHandler = $handler;
91
92
        return $this;
93
    }
94
}
95