Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id$
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Pager_Range_Jumping
24
 *
25
 * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
26
 * @package     Doctrine
27
 * @subpackage  Pager
28
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29
 * @version     $Revision$
30
 * @link        www.doctrine-project.org
31
 * @since       0.9
32
 */
33
class Doctrine_Pager_Range_Jumping extends Doctrine_Pager_Range
34
{
35
    /**
36
     * @var int $_chunkLength     Chunk length to be returned
37
     */
38
    private $_chunkLength;
39
 
40
    /**
41
     * _initialize
42
     *
43
     * Initialize Doctrine_Pager_Range_Jumping and does custom assignments
44
     *
45
     * @return void
46
     */
47
    protected function _initialize()
48
    {
49
        if (isset($this->_options['chunk'])) {
50
            $this->_setChunkLength($this->_options['chunk']);
51
        } else {
52
            throw new Doctrine_Pager_Exception('Missing parameter \'chunk\' that must be define in options.');
53
        }
54
    }
55
 
56
    /**
57
     * getChunkLength
58
     *
59
     * Returns the size of the chunk defined
60
     *
61
     * @return int        Chunk length
62
     */
63
    public function getChunkLength()
64
    {
65
        return $this->_chunkLength;
66
    }
67
 
68
    /**
69
     * _setChunkLength
70
     *
71
     * Defines the size of the chunk
72
     *
73
     * @param $chunkLength       Chunk length
74
     * @return void
75
     */
76
    protected function _setChunkLength($chunkLength)
77
    {
78
        $this->_chunkLength = $chunkLength;
79
    }
80
 
81
    /**
82
     * rangeAroundPage
83
     *
84
     * Calculate and returns an array representing the range around the current page
85
     *
86
     * @return array
87
     */
88
    public function rangeAroundPage()
89
    {
90
        $pager = $this->getPager();
91
 
92
        if ($pager->getExecuted()) {
93
            $page = $pager->getPage();
94
 
95
            // Define initial assignments for StartPage and EndPage
96
            $startPage = $page - ($page - 1) % $this->getChunkLength();
97
            $endPage = ($startPage + $this->getChunkLength()) - 1;
98
 
99
            // Check for EndPage out-range
100
            if ($endPage > $pager->getLastPage()) {
101
                $endPage = $pager->getLastPage();
102
            }
103
 
104
            // No need to check for out-range in start, it will never happens
105
 
106
            return range($startPage, $endPage);
107
        }
108
 
109
        throw new Doctrine_Pager_Exception(
110
            'Cannot retrieve the range around the page of a not yet executed Pager query'
111
        );
112
    }
113
}