for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace loophp\iterators;
use Closure;
use Generator;
use IteratorAggregate;
* @implements IteratorAggregate<int, string>
final class StringIteratorAggregate implements IteratorAggregate
{
private Closure $callable;
private string $data;
private string $delimiter;
public function __construct(string $data, string $delimiter = '')
$this->callable =
* @return Generator<int, string>
static function (string $input, string $delimiter): Generator {
$offset = 0;
while (
mb_strlen($input) > $offset
&& false !== $nextOffset = '' !== $delimiter ? mb_strpos($input, $delimiter, $offset) : 1 + $offset
) {
yield mb_substr($input, $offset, $nextOffset - $offset);
$offset = $nextOffset + mb_strlen($delimiter);
}
if ('' !== $delimiter) {
yield mb_substr($input, $offset);
};
$this->data = $data;
$this->delimiter = $delimiter;
public function getIterator(): Generator
yield from new ClosureIteratorAggregate($this->callable, [$this->data, $this->delimiter]);