| 1 |
<?php |
| 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 |
class ExplodeIterator implements Iterator { |
| 31 |
|
| 32 |
private $subject; |
| 33 |
|
| 34 |
|
| 35 |
private $subjectLength; |
| 36 |
|
| 37 |
|
| 38 |
private $delim; |
| 39 |
|
| 40 |
|
| 41 |
private $delimLength; |
| 42 |
|
| 43 |
|
| 44 |
private $curPos; |
| 45 |
|
| 46 |
|
| 47 |
private $endPos; |
| 48 |
|
| 49 |
|
| 50 |
private $current; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( $delim, $subject ) { |
| 58 |
$this->subject = $subject; |
| 59 |
$this->delim = $delim; |
| 60 |
|
| 61 |
|
| 62 |
$this->subjectLength = strlen( $subject ); |
| 63 |
$this->delimLength = strlen( $delim ); |
| 64 |
|
| 65 |
$this->rewind(); |
| 66 |
} |
| 67 |
|
| 68 |
public function rewind(): void { |
| 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 |
|
| 87 |
|
| 88 |
|
| 89 |
#[\ReturnTypeWillChange] |
| 90 |
public function current() { |
| 91 |
return $this->current; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#[\ReturnTypeWillChange] |
| 98 |
public function key() { |
| 99 |
return $this->curPos; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function next(): void { |
| 106 |
if ( $this->endPos === false ) { |
| 107 |
$this->curPos = false; |
| 108 |
} else { |
| 109 |
$this->curPos = $this->endPos + $this->delimLength; |
| 110 |
if ( $this->curPos >= $this->subjectLength ) { |
| 111 |
$this->endPos = false; |
| 112 |
} else { |
| 113 |
$this->endPos = strpos( $this->subject, $this->delim, $this->curPos ); |
| 114 |
} |
| 115 |
} |
| 116 |
$this->refreshCurrent(); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function valid(): bool { |
| 123 |
return $this->curPos !== false; |
| 124 |
} |
| 125 |
} |