Completed
Pull Request — master (#615)
by Filippo
14:02 queued 03:29
created

Selectable::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 6
1
<?php
2
3
namespace DoctrineModule\Paginator\Adapter;
4
5
use Doctrine\Common\Collections\Selectable as DoctrineSelectable;
6
use Doctrine\Common\Collections\Criteria;
7
use Zend\Paginator\Adapter\AdapterInterface;
8
9
/**
10
 * Provides a wrapper around a Selectable object
11
 *
12
 * @license MIT
13
 * @link    http://www.doctrine-project.org/
14
 * @author  Michaël Gallego <[email protected]>
15
 * @author  Marco Pivetta <[email protected]>
16
 */
17
class Selectable implements AdapterInterface
18
{
19
    /**
20
     * @var DoctrineSelectable
21
     */
22
    protected $selectable;
23
24
    /**
25
     * @var \Doctrine\Common\Collections\Criteria
26
     */
27
    protected $criteria;
28
29
    /**
30
     * Create a paginator around a Selectable object. You can also provide an optional Criteria object with
31
     * some predefined filters
32
     *
33
     * @param \Doctrine\Common\Collections\Selectable    $selectable
34
     * @param \Doctrine\Common\Collections\Criteria|null $criteria
35
     */
36
    public function __construct(DoctrineSelectable $selectable, Criteria $criteria = null)
37
    {
38
        $this->selectable = $selectable;
39
        $this->criteria   = $criteria ? clone $criteria : new Criteria();
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 4
    public function getItems($offset, $itemCountPerPage)
46
    {
47 4
        $this->criteria->setFirstResult($offset)->setMaxResults($itemCountPerPage);
48
49 4
        return $this->selectable->matching($this->criteria)->toArray();
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 1
    public function count()
56
    {
57 1
        $criteria = clone $this->criteria;
58
59 1
        $criteria->setFirstResult(null);
60 1
        $criteria->setMaxResults(null);
61
62 1
        return count($this->selectable->matching($criteria));
63
    }
64
}
65