MediaWiki  master
ExplodeIterator.php
Go to the documentation of this file.
1 <?php
30 class ExplodeIterator implements Iterator {
32  private $subject;
33 
35  private $subjectLength;
36 
38  private $delim;
39 
41  private $delimLength;
42 
44  private $curPos;
45 
47  private $endPos;
48 
50  private $current;
51 
57  public function __construct( $delim, $subject ) {
58  $this->subject = $subject;
59  $this->delim = $delim;
60 
61  // Micro-optimisation (theoretical)
62  $this->subjectLength = strlen( $subject );
63  $this->delimLength = strlen( $delim );
64 
65  $this->rewind();
66  }
67 
68  public function rewind() {
69  $this->curPos = 0;
70  $this->endPos = strpos( $this->subject, $this->delim );
71  $this->refreshCurrent();
72  }
73 
74  public function refreshCurrent() {
75  if ( $this->curPos === false ) {
76  $this->current = false;
77  } elseif ( $this->curPos >= $this->subjectLength ) {
78  $this->current = '';
79  } elseif ( $this->endPos === false ) {
80  $this->current = substr( $this->subject, $this->curPos );
81  } else {
82  $this->current = substr( $this->subject, $this->curPos, $this->endPos - $this->curPos );
83  }
84  }
85 
86  public function current() {
87  return $this->current;
88  }
89 
93  public function key() {
94  return $this->curPos;
95  }
96 
100  public function next() {
101  if ( $this->endPos === false ) {
102  $this->curPos = false;
103  } else {
104  $this->curPos = $this->endPos + $this->delimLength;
105  if ( $this->curPos >= $this->subjectLength ) {
106  $this->endPos = false;
107  } else {
108  $this->endPos = strpos( $this->subject, $this->delim, $this->curPos );
109  }
110  }
111  $this->refreshCurrent();
112  }
113 
117  public function valid() {
118  return $this->curPos !== false;
119  }
120 }
ExplodeIterator\key
key()
Definition: ExplodeIterator.php:93
ExplodeIterator\rewind
rewind()
Definition: ExplodeIterator.php:68
ExplodeIterator\$curPos
int $curPos
The position of the start of the line.
Definition: ExplodeIterator.php:44
ExplodeIterator
An iterator which works exactly like:
Definition: ExplodeIterator.php:30
ExplodeIterator\$subject
string $subject
Definition: ExplodeIterator.php:32
ExplodeIterator\$current
string false $current
The current token.
Definition: ExplodeIterator.php:50
ExplodeIterator\valid
valid()
Definition: ExplodeIterator.php:117
ExplodeIterator\refreshCurrent
refreshCurrent()
Definition: ExplodeIterator.php:74
ExplodeIterator\$subjectLength
int $subjectLength
Definition: ExplodeIterator.php:35
ExplodeIterator\next
next()
Definition: ExplodeIterator.php:100
ExplodeIterator\$delimLength
int $delimLength
Definition: ExplodeIterator.php:41
ExplodeIterator\$endPos
int $endPos
The position after the end of the next delimiter.
Definition: ExplodeIterator.php:47
ExplodeIterator\__construct
__construct( $delim, $subject)
Construct a DelimIterator.
Definition: ExplodeIterator.php:57
ExplodeIterator\$delim
string $delim
Definition: ExplodeIterator.php:38
ExplodeIterator\current
current()
Definition: ExplodeIterator.php:86