summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Iterators/AppendIterator.php
blob: 1fcf6cdad5b55450ee7206f3382a02e4f1732faf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

namespace SMW\Iterators;

use ArrayIterator;
use Countable;
use Iterator;
use RuntimeException;
use Traversable;

/**
 * @see Guzzle::AppendIterator
 * @see https://bugs.php.net/bug.php?id=49104
 *
 * @license GNU GPL v2+
 * @since 3.0
 */
class AppendIterator extends \AppendIterator implements Countable {

	/**
	 * @var integer
	 */
	private $count = 0;

	/**
	 * @since 3.0
	 *
	 * @param Traversable|array $iterator
	 */
	public function add( $iterable ) {

		if ( is_array( $iterable ) ) {
			$iterable = new ArrayIterator( $iterable );
		}

		if ( !$iterable instanceof Traversable ) {
			throw new RuntimeException( "AppendIterator expected an Traversable" );
		}

		$this->append( $iterable );
	}

	/**
	 * @see Countable::count
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function count() {
		return $this->count;
	}

	/**
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function append( Iterator $iterable ) {

		if ( $iterable instanceof Countable ) {
			$this->count += $iterable->count();
		}

		$this->getArrayIterator()->append( $iterable );
	}

}