NullAdapter::getSlice()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Pagerfanta package.
5
 *
6
 * (c) Pablo Díez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pagerfanta\Adapter;
13
14
/**
15
 * NullAdapter.
16
 *
17
 * @author Benjamin Dulau <[email protected]>
18
 */
19
class NullAdapter implements AdapterInterface
20
{
21
    private $nbResults;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param integer $nbResults Total item count.
27
     */
28 6
    public function __construct($nbResults = 0)
29
    {
30 6
        $this->nbResults = (int) $nbResults;
31 6
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function getNbResults()
37
    {
38 1
        return $this->nbResults;
39
    }
40
41
    /**
42
     * The following methods are derived from code of the Zend Framework
43
     * Code subject to the new BSD license (http://framework.zend.com/license/new-bsd).
44
     *
45
     * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
46
     *
47
     * {@inheritdoc}
48
     */
49 4
    public function getSlice($offset, $length)
50
    {
51 4
        if ($offset >= $this->nbResults) {
52 2
            return array();
53
        }
54
55 2
        $nullArrayLength = $this->calculateNullArrayLength($offset, $length);
56
57 2
        return $this->createNullArray($nullArrayLength);
58
    }
59
60 2
    private function calculateNullArrayLength($offset, $length)
61
    {
62 2
        $remainCount = $this->remainCount($offset);
63 2
        if ($length > $remainCount) {
64 1
            return $remainCount;
65
        }
66
67 1
        return $length;
68
    }
69
70 2
    private function remainCount($offset)
71
    {
72 2
        return $this->nbResults - $offset;
73
    }
74
75 2
    private function createNullArray($length)
76
    {
77 2
        return array_fill(0, $length, null);
78
    }
79
}
80