Test Setup Failed
Push — master ( ec1289...d72af6 )
by Florian
04:48
created

ArrayPaginatorAdapter::paginate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 2
dl 0
loc 22
rs 9.8666
c 0
b 0
f 0
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;
16
17
use Cake\Datasource\QueryInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
20
/**
21
 * Paginates arrays
22
 */
23
class ArrayPaginatorAdapter implements PaginationAdapterInterface
24
{
25
    /**
26
     * Maps the params to the repository
27
     *
28
     * @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params
29
     * @param mixed $repository
30
     * @return mixed
31
     */
32
    public function paginate(PaginationParamsInterface $paginationParams, $array)
33
    {
34
        $paginationParams->setCount(count($array));
35
36
        $count = 1;
37
        $data = [];
38
        $offset = $paginationParams->getOffset();
39
        $stopAt = $offset + $paginationParams->getLimit();
40
41
        foreach ($array as $key => $value) {
42
            if ($count > $stopAt) {
43
                break;
44
            }
45
46
            if ($count > $paginationParams->getOffset()) {
47
                $data[$key] = $value;
48
            }
49
50
            $count++;
51
        }
52
53
        return $data;
54
    }
55
}
56