NullAdapter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 61
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0
ccs 19
cts 19
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getNbResults() 0 4 1
A getSlice() 0 10 2
A calculateNullArrayLength() 0 9 2
A remainCount() 0 4 1
A createNullArray() 0 4 1
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