Completed
Push — master ( 03a792...ea49f3 )
by Alberto
02:34
created

lib/Kumbia/ActiveRecord/Paginator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * KumbiaPHP web & app Framework.
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Kumbia
16
 *
17
 * @copyright  2005 - 2016  Kumbia Team (http://www.kumbiaphp.com)
18
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
19
 */
20
namespace Kumbia\ActiveRecord;
21
22
/**
23
 * Implementación de paginador.
24
 */
25
class Paginator implements \IteratorAggregate, \Countable
26
{
27
    /**
28
     * Número de página actual.
29
     *
30
     * @var int
31
     */
32
    protected $page;
33
34
    /**
35
     * Cantidad de items por página.
36
     *
37
     * @var int
38
     */
39
    protected $perPage;
40
41
    /**
42
     * Número de páginas totales.
43
     *
44
     * @var int
45
     */
46
    protected $totalPages;
47
48
    /**
49
     * Cantidad de items totales.
50
     *
51
     * @var int
52
     */
53
    protected $count;
54
55
    /**
56
     * Nombre del modelo a usar.
57
     *
58
     * @var string
59
     */
60
    protected $_model;
61
62
    /**
63
     * Cadena SQL a ejecutar.
64
     *
65
     * @var string
66
     */
67
    protected $_sql;
68
69
    /**
70
     * Párametros de la consulta.
71
     *
72
     * @var array
73
     */
74
    protected $_values;
75
76
    /**
77
     * Items de pagina.
78
     *
79
     * @var array de objetos
80
     */
81
    private $items;
82
83
    /**
84
     * Constructor.
85
     *
86
     * @param string $model   nombre de clase de modelo
87
     * @param string $sql     consulta select sql
88
     * @param int    $page    numero de pagina
89
     * @param int    $perPage cantidad de items por pagina
90
     * @param mixed  $values  valores
91
     */
92
    public function __construct($model, $sql, $page, $perPage, $values = null)
93
    {
94
        $this->perPage = (int) $perPage;
95
        $this->page = (int) $page;
96
97
        /*validacion*/
98
        $this->validPage();
99
100
        $this->_model = $model;
101
102
        // Valores para consulta
103
        $this->_values = ($values !== null && !is_array($values)) ?
0 ignored issues
show
Documentation Bug introduced by
It seems like $values !== null && !is_...et_args(), 4) : $values can be null. However, the property $_values is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
104
                    array_slice(func_get_args(), 4) : $values;
105
106
        $this->count = $this->countQuery($model, $sql);
107
        $this->totalPages = (int) max(1, ceil($this->count / $this->perPage));
108
        $this->validCurrent();
109
        // Establece el limit y offset
110
        $this->_sql = QueryGenerator::query($model::getDriver(), 'limit', $sql, $perPage, ($page - 1) * $perPage);
111
        $this->items = $model::query($this->_sql, $this->_values)->fetchAll();
112
    }
113
114
    /**
115
     * Verifica que la pagina sea válida.
116
     */
117
    private function validPage()
118
    {
119
        //Si la página o por página es menor de 1 (0 o negativo)
120
        if ($this->page < 1 || $this->perPage < 1) {
121
            throw new \RangeException("La página $this->page no existe", 404);
122
        }
123
    }
124
125
    /**
126
     * Valida que la página actual.
127
     */
128
    private function validCurrent()
129
    {
130
        if ($this->page > $this->totalPages) {
131
            throw new \RangeException("La página $this->page no existe", 404);
132
        }
133
    }
134
135
    /**
136
     * (non-PHPdoc).
137
     *
138
     * @see IteratorAggregate::getIterator()
139
     */
140
    public function getIterator()
141
    {
142
        return new \ArrayIterator($this->items);
143
    }
144
145
    /**
146
     * Cuenta el número de resultados totales.
147
     *
148
     * @param string $model
149
     * @param string $sql
150
     *
151
     * @return int total de resultados
152
     */
153
    protected function countQuery($model, $sql)
154
    {
155
        $query = $model::query("SELECT COUNT(*) AS count FROM ($sql) AS t", $this->_values)->fetch();
156
157
        return (int) $query->count;
158
    }
159
160
    /**
161
     * Total de items.
162
     *
163
     * @return int
164
     */
165
    public function totalItems()
166
    {
167
        return $this->count;
168
    }
169
170
    /**
171
     * Total de páginas.
172
     *
173
     * @return int
174
     */
175
    public function totalPages()
176
    {
177
        return $this->totalPages;
178
    }
179
180
    /**
181
     * Calcula el valor de la próxima página.
182
     *
183
     * @return int
184
     */
185
    public function nextPage()
186
    {
187
        return ($this->totalPages > $this->page) ? ($this->page + 1) : null;
188
    }
189
190
    /**
191
     * Calcula el valor de la página anterior.
192
     *
193
     * @return int
194
     */
195
    public function prevPage()
196
    {
197
        return ($this->page > 1) ? ($this->page - 1) : null;
198
    }
199
200
    /**
201
     * Items devueltos.
202
     *
203
     * @see Countable::countable()
204
     *
205
     * @return int
206
     */
207
    public function count()
208
    {
209
        return count($this->items);
210
    }
211
212
    /**
213
     * Página actual de paginador.
214
     *
215
     * @return int
216
     */
217
    public function page()
218
    {
219
        return $this->page;
220
    }
221
}
222