wiki.techinc.nl/includes/libs/ReverseArrayIterator.php
C. Scott Ananian a7e2b5b284 HTML5 Balancer
This adds an implementation of the HTML5 Tree Builder algorithm to PHP,
along with test cases from the tree builder derived from the
html5lib-tests package on github.  The test cases were preprocessed
into JSON for the `domino` HTML5 parser, and we're using the JSON
form of the tests.

The implementation follows both the language of the HTML5 specification
and the implementation in `domino` very closely, easing updates if the
specification changes.

This code is used in follow-on commits to support an HTML5-based
"tidy" for mediawiki and the `{{#balance}}` parser function, which
ensures that a template expands to properly-balanced HTML, with all
tags closed and nothing left on the HTML active formatting elements
list.

See: https://github.com/fgnass/domino
Change-Id: I6f4d20a43510dd819776bb333b639315b19d150d
2016-07-12 14:18:04 +10:00

76 lines
2 KiB
PHP

<?php
/**
* Convenience class for iterating over an array in reverse order.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @since 1.27
*/
/**
* Convenience class for iterating over an array in reverse order.
*
* @since 1.27
*/
class ReverseArrayIterator implements Iterator, Countable {
/** @var array $array */
protected $array;
/**
* Creates an iterator which will visit the keys in $array in
* reverse order. If given an object, will visit the properties
* of the object in reverse order. (Note that the default order
* for PHP arrays and objects is declaration/assignment order.)
*
* @param array|object $array
*/
public function __construct( $array = [] ) {
if ( is_array( $array ) ) {
$this->array = $array;
} elseif ( is_object( $array ) ) {
$this->array = get_object_vars( $array );
} else {
throw new InvalidArgumentException( __METHOD__ . ' requires an array or object' );
}
$this->rewind();
}
public function current() {
return current( $this->array );
}
public function key() {
return key( $this->array );
}
public function next() {
prev( $this->array );
}
public function rewind() {
end( $this->array );
}
public function valid() {
return key( $this->array ) !== null;
}
public function count() {
return count( $this->array );
}
}