d249b8a1d97d883df2a6a476062c9a12e168d84e
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class PlArrayIterator
implements PlIterator
27 private $_its = array();
34 public function __construct(array &$array, $depth = 1)
36 $this->array =& $array;
37 $this->depth
= $depth;
38 $this->_total
= $this->count($array, $depth - 1);
40 $this->_first
= false
;
43 for ($i = 0 ; $i < $depth ; ++
$i) {
45 $this->_its
[] = $array;
47 $this->_its
[] = current($this->_its
[$i - 1]);
49 reset($this->_its
[$i]);
53 private function count(array &$array, $depth)
59 foreach ($array as &$item) {
60 $sum +
= $this->count($item, $depth - 1);
66 private function nextArray($depth)
71 $this->_its
[$depth] = next($this->_its
[$depth - 1]);
72 if ($this->_its
[$depth] === false
) {
73 $this->nextArray($depth - 1);
74 if ($this->_its
[$depth - 1] === false
) {
77 $this->_its
[$depth] = current($this->_its
[$depth - 1]);
79 reset($this->_its
[$depth]);
82 public function next()
85 $this->_first
= ($this->_total
> 0 && $this->_pos
== 1);
86 $this->_last
= ($this->_pos
== $this->_total
);
87 if ($this->_pos
> $this->_total
) {
91 $val = current($this->_its
[$this->depth
- 1]);
93 $this->nextArray($this->depth
- 1);
94 $val = current($this->_its
[$this->depth
- 1]);
100 for ($i = 0 ; $i < $this->depth
; ++
$i) {
101 $keys[] = key($this->_its
[$i]);
103 next($this->_its
[$this->depth
- 1]);
104 return array('keys' => $keys,
108 public function total()
110 return $this->_total
;
113 public function first()
115 return $this->_first
;
118 public function last()
124 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: