PHPackages                             shiyan/iterator-regex - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. shiyan/iterator-regex

Abandoned → [shiyan/iterate](/?search=shiyan%2Fiterate)Library[Utility &amp; Helpers](/categories/utility)

shiyan/iterator-regex
=====================

Iterates iterators by a scenario.

2.0.0(8y ago)021MITPHPPHP ^7.1

Since Dec 2Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mikeshiyan/iterator-regex)[ Packagist](https://packagist.org/packages/shiyan/iterator-regex)[ RSS](/packages/shiyan-iterator-regex/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (5)Used By (0)

Iterate
=======

[](#iterate)

[![Build Status](https://camo.githubusercontent.com/131af5be5bcb5448f46a5158dedc445f25347ce67672063d6c2789abf4575aab/68747470733a2f2f7472617669732d63692e6f72672f6d696b6573686979616e2f697465726174652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mikeshiyan/iterate)

PHP classes and interfaces to iterate `\Iterator`s by a scenario or to perform a regular expression match on iterator elements by a scenario.

A Scenario is a class that implements the `ScenarioInterface` or extends either `BaseScenario` or `BaseRegexScenario` abstract class. It receives an `Iterator`instance and executes the logic in certain moments of the iteration process - for example, while regex matching is performed - when some pattern matched a subject, or none matched, or before/after performing a search, etc.

Think of it as of [`array_walk()`](http://php.net/manual/function.array-walk.php)for iterators or [`iterator_apply()`](http://php.net/manual/function.iterator-apply.php), but instead of a single callback you provide an object (Scenario) with several methods. And you can use these Scenarios as plugins.

Iterator is simply any object implementing the [`\Iterator`](http://php.net/manual/class.iterator.php)interface.

The main class, which runs the process, is `Iterate`. It just needs to be instantiated and invoked with an iterator and a scenario objects.

Best suited for use as a [Composer](https://getcomposer.org) library.

Requirements
------------

[](#requirements)

- PHP ≥ 7.1

Installation
------------

[](#installation)

To add this library to your Composer project:

```
composer require shiyan/iterate

```

Usage
-----

[](#usage)

There are 2 base scenarios included:

- The very basic one to execute some logic on each element of the iterator unconditionally.
- And the regex based one, which performs a regular expression match (using one or more patterns) on iterator elements (casted to strings), and allows to execute different logic depending on what pattern matched or if no patterns matched.

### Example

[](#example)

Assuming there is an array of strings, `$array`. Iterate over it, convert all empty strings to 0 (zero), print all strings containing "PHP" and simply count all other elements.

First, create a scenario class:

```
use Shiyan\Iterate\Scenario\BaseRegexScenario;

class ExampleScenario extends BaseRegexScenario {

  const PATTERN_EMPTY = '/^$/';
  const PATTERN_PHP = '/PHP/';

  public $counter;

  public function getPatterns(): array {
    return [self::PATTERN_EMPTY, self::PATTERN_PHP];
  }

  public function preRun(): void {
    $this->counter = 0;
  }

  public function onMatch(array $matches, string $pattern): void {
    switch ($pattern) {
      case self::PATTERN_EMPTY:
        $this->iterator[$this->iterator->key()] = 0;
        break;

      case self::PATTERN_PHP:
        print $this->iterator->current() . "\n";
        break;
    }
  }

  public function ifNotMatched(): void {
    ++$this->counter;
  }

}

```

And then:

```
use Shiyan\Iterate\Iterate;

// Convert our array of strings to the iterator object.
$iterator = new ArrayIterator($array);
$scenario = new ExampleScenario();
$iterate = new Iterate();

// Invoke iterate with our scenario.
$iterate($iterator, $scenario);
print "Found {$scenario->counter} non-empty, non-PHP strings.\n";

// Let's check that empty strings were converted to zeros.
print_r($iterator->getArrayCopy());

// If we invoke Iterate with the same scenario once again, it won't find empty
// strings anymore. Instead, we'll have a higher number in the $counter property
// of the $scenario object, because the "0" doesn't match our patterns.
$iterate($iterator, $scenario);
print "Found {$scenario->counter} non-empty, non-PHP strings.\n";

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity61

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~42 days

Total

4

Last Release

2959d ago

Major Versions

1.0.2 → 2.0.02018-04-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/0eef0320768b8d176b9014442390bfa3bfec9369cba3b9e663a66dac57ac0084?d=identicon)[mikeshiyan](/maintainers/mikeshiyan)

---

Tags

composeriteratormatchoopphpregexscenariophpiteratorregexMatchOOPscenario

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/shiyan-iterator-regex/health.svg)

```
[![Health](https://phpackages.com/badges/shiyan-iterator-regex/health.svg)](https://phpackages.com/packages/shiyan-iterator-regex)
```

###  Alternatives

[gherkins/regexpbuilderphp

PHP port of thebinarysearchtree/regexpbuilderjs

1.4k163.0k1](/packages/gherkins-regexpbuilderphp)[selvinortiz/flux

Fluent regular expressions in PHP.

3372.0k1](/packages/selvinortiz-flux)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
