~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to library/components/doctrine/lib/Doctrine/Pager/Range/Jumping.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.phpdoctrine.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.phpdoctrine.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
    /**
 
42
     * _initialize
 
43
     *
 
44
     * Initialize Doctrine_Pager_Range_Jumping and does custom assignments
 
45
     *
 
46
     * @return void
 
47
     */
 
48
    protected function _initialize()
 
49
    {
 
50
        if (isset($this->_options['chunk'])) {
 
51
            $this->_setChunkLength($this->_options['chunk']);
 
52
        } else {
 
53
            throw new Doctrine_Pager_Exception('Missing parameter \'chunk\' that must be define in options.');
 
54
        }
 
55
    }
 
56
 
 
57
 
 
58
    /**
 
59
     * getChunkLength
 
60
     *
 
61
     * Returns the size of the chunk defined
 
62
     *
 
63
     * @return int        Chunk length
 
64
     */
 
65
    public function getChunkLength()
 
66
    {
 
67
        return $this->_chunkLength;
 
68
    }
 
69
 
 
70
 
 
71
    /**
 
72
     * _setChunkLength
 
73
     *
 
74
     * Defines the size of the chunk
 
75
     *
 
76
     * @param $chunkLength       Chunk length
 
77
     * @return void
 
78
     */
 
79
    protected function _setChunkLength($chunkLength)
 
80
    {
 
81
        $this->_chunkLength = $chunkLength;
 
82
    }
 
83
 
 
84
 
 
85
    /**
 
86
     * rangeAroundPage
 
87
     *
 
88
     * Calculate and returns an array representing the range around the current page
 
89
     *
 
90
     * @return array
 
91
     */
 
92
    public function rangeAroundPage()
 
93
    {
 
94
        $pager = $this->getPager();
 
95
 
 
96
        if ($pager->getExecuted()) {
 
97
            $page = $pager->getPage();
 
98
 
 
99
            // Define initial assignments for StartPage and EndPage
 
100
            $startPage = $page - ($page - 1) % $this->getChunkLength();
 
101
            $endPage = ($startPage + $this->getChunkLength()) - 1;
 
102
 
 
103
            // Check for EndPage out-range
 
104
            if ($endPage > $pager->getLastPage()) {
 
105
                $endPage = $pager->getLastPage();
 
106
            }
 
107
 
 
108
            // No need to check for out-range in start, it will never happens
 
109
 
 
110
            return range($startPage, $endPage);
 
111
        }
 
112
 
 
113
        throw new Doctrine_Pager_Exception(
 
114
            'Cannot retrieve the range around the page of a not yet executed Pager query'
 
115
        );
 
116
    }
 
117
}