Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 183 lines (166 sloc) 4.456 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
<?php
/**
* DoctrineExtensions Paginate
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE. This license can also be viewed
* at http://hobodave.com/license.txt
*
* @category DoctrineExtensions
* @package DoctrineExtensions\Paginate
* @author David Abdemoulaie <dave@hobodave.com>
* @copyright Copyright (c) 2010 David Abdemoulaie (http://hobodave.com/)
* @license http://hobodave.com/license.txt New BSD License
*/

namespace DoctrineExtensions\Paginate;

use Doctrine\ORM\Query;

/**
* Implements the Zend_Paginator_Adapter_Interface for use with Zend_Paginator
*
* Allows pagination of Doctrine\ORM\Query objects and DQL strings
*
* @category DoctrineExtensions
* @package DoctrineExtensions\Paginate
* @author David Abdemoulaie <dave@hobodave.com>
* @copyright Copyright (c) 2010 David Abdemoulaie (http://hobodave.com/)
* @license http://hobodave.com/license.txt New BSD License
*/
class PaginationAdapter implements \Zend_Paginator_Adapter_Interface
{
    /**
* The SELECT query to paginate
*
* @var Query
*/
    protected $query = null;

    /**
* Total item count
*
* @var integer
*/
    protected $rowCount = null;

    /**
* Use Array Result
*
* @var boolean
*/
    protected $arrayResult = false;

    /**
* Namespace to use for bound parameters
* If you use :pgid_# as a parameter, then
* you must change this.
*
* @var string
*/
    protected $namespace = 'pgid';

    /**
* Constructor
*
* @param Query $query
* @param string $ns Namespace to prevent named parameter conflicts
*/
    public function __construct(Query $query, $ns = 'pgid')
    {
        $this->query = $query;
        $this->namespace = $ns;
    }

    /**
* Set use array result flag
*
* @param boolean $flag True to use array result
*/
    public function useArrayResult($flag = true)
    {
        $this->arrayResult = $flag;
    }

    /**
* Sets the total row count for this paginator
*
* Can be either an integer, or a Doctrine\ORM\Query object
* which returns the count
*
* @param Query|integer $rowCount
* @return void
*/
    public function setRowCount($rowCount)
    {
        if ($rowCount instanceof Query) {
            $this->rowCount = $rowCount->getSingleScalarResult();
        } else if (is_integer($rowCount)) {
            $this->rowCount = $rowCount;
        } else {
            throw new \InvalidArgumentException("Invalid row count");
        }
    }

    /**
* Sets the namespace to be used for named parameters
*
* Parameters will be in the format 'namespace_1' ... 'namespace_N'
*
* @param string $ns
* @return void
* @author David Abdemoulaie
*/
    public function setNamespace($ns)
    {
        $this->namespace = $ns;
    }

    /**
* Gets the current page of items
*
* @param string $offset
* @param string $itemCountPerPage
* @return void
* @author David Abdemoulaie
*/
    public function getItems($offset, $itemCountPerPage)
    {
        $ids = $this->createLimitSubquery($offset, $itemCountPerPage)
            ->getScalarResult();

        $ids = array_map(
            function ($e) { return current($e); },
            $ids
        );

        if ($this->arrayResult) {
            return $this->createWhereInQuery($ids)->getArrayResult();
        } else {
            return $this->createWhereInQuery($ids)->getResult();
        }
    }

    /**
* @param Query $query
* @return int
*/
    public function count()
    {
        if (is_null($this->rowCount)) {
            $this->setRowCount(
                $this->createCountQuery()
            );
        }
        return $this->rowCount;
    }

    /**
* @return Query
*/
    protected function createCountQuery()
    {
        return Paginate::createCountQuery($this->query);
    }

    /**
* @return Query
*/
    protected function createLimitSubquery($offset, $itemCountPerPage)
    {
        return Paginate::createLimitSubQuery($this->query, $offset, $itemCountPerPage);
    }

    /**
* @return Query
*/
    protected function createWhereInQuery($ids)
    {
        return Paginate::createWhereInQuery($this->query, $ids, $this->namespace);
    }
}
Something went wrong with that request. Please try again.