Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / libs / ReverseArrayIterator.php
blob37b68c3f85eef5ddc556613e48f334a4ed82263c
1 <?php
2 /**
3 * Convenience class for iterating over an array in reverse order.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @since 1.27
24 /**
25 * Convenience class for iterating over an array in reverse order.
27 * @since 1.27
29 class ReverseArrayIterator implements Iterator, Countable {
30 /** @var array $array */
31 protected $array;
33 /**
34 * Creates an iterator which will visit the keys in $array in
35 * reverse order. If given an object, will visit the properties
36 * of the object in reverse order. (Note that the default order
37 * for PHP arrays and objects is declaration/assignment order.)
39 * @param array|object $array
41 public function __construct( $array = [] ) {
42 if ( is_array( $array ) ) {
43 $this->array = $array;
44 } elseif ( is_object( $array ) ) {
45 $this->array = get_object_vars( $array );
46 } else {
47 throw new InvalidArgumentException( __METHOD__ . ' requires an array or object' );
50 $this->rewind();
53 public function current() {
54 return current( $this->array );
57 public function key() {
58 return key( $this->array );
61 public function next() {
62 prev( $this->array );
65 public function rewind() {
66 end( $this->array );
69 public function valid() {
70 return key( $this->array ) !== null;
73 public function count() {
74 return count( $this->array );