MediaWiki REL1_31
ExplodeIterator.php
Go to the documentation of this file.
1<?php
30class ExplodeIterator implements Iterator {
31 // The subject string
33
34 // The delimiter
36
37 // The position of the start of the line
38 private $curPos;
39
40 // The position after the end of the next delimiter
41 private $endPos;
42
44 private $current;
45
51 public function __construct( $delim, $subject ) {
52 $this->subject = $subject;
53 $this->delim = $delim;
54
55 // Micro-optimisation (theoretical)
56 $this->subjectLength = strlen( $subject );
57 $this->delimLength = strlen( $delim );
58
59 $this->rewind();
60 }
61
62 public function rewind() {
63 $this->curPos = 0;
64 $this->endPos = strpos( $this->subject, $this->delim );
65 $this->refreshCurrent();
66 }
67
68 public function refreshCurrent() {
69 if ( $this->curPos === false ) {
70 $this->current = false;
71 } elseif ( $this->curPos >= $this->subjectLength ) {
72 $this->current = '';
73 } elseif ( $this->endPos === false ) {
74 $this->current = substr( $this->subject, $this->curPos );
75 } else {
76 $this->current = substr( $this->subject, $this->curPos, $this->endPos - $this->curPos );
77 }
78 }
79
80 public function current() {
81 return $this->current;
82 }
83
87 public function key() {
88 return $this->curPos;
89 }
90
94 public function next() {
95 if ( $this->endPos === false ) {
96 $this->curPos = false;
97 } else {
98 $this->curPos = $this->endPos + $this->delimLength;
99 if ( $this->curPos >= $this->subjectLength ) {
100 $this->endPos = false;
101 } else {
102 $this->endPos = strpos( $this->subject, $this->delim, $this->curPos );
103 }
104 }
105 $this->refreshCurrent();
106
107 return $this->current;
108 }
109
113 public function valid() {
114 return $this->curPos !== false;
115 }
116}
An iterator which works exactly like:
__construct( $delim, $subject)
Construct a DelimIterator.
string false $current
The current token.