{"id":21394196,"url":"https://github.com/mikeshiyan/iterate","last_synced_at":"2026-05-19T11:04:56.391Z","repository":{"id":57049612,"uuid":"112781276","full_name":"mikeshiyan/iterate","owner":"mikeshiyan","description":"Iterates iterators by a scenario.","archived":false,"fork":false,"pushed_at":"2018-04-08T13:03:22.000Z","size":34,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-23T01:42:27.313Z","etag":null,"topics":["composer","iterator","match","oop","php","regex","scenario"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikeshiyan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-01T19:59:14.000Z","updated_at":"2018-04-08T13:01:00.000Z","dependencies_parsed_at":"2022-08-23T19:10:22.110Z","dependency_job_id":null,"html_url":"https://github.com/mikeshiyan/iterate","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeshiyan%2Fiterate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeshiyan%2Fiterate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeshiyan%2Fiterate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeshiyan%2Fiterate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikeshiyan","download_url":"https://codeload.github.com/mikeshiyan/iterate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243879094,"owners_count":20362546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","iterator","match","oop","php","regex","scenario"],"created_at":"2024-11-22T14:14:39.566Z","updated_at":"2026-05-19T11:04:51.356Z","avatar_url":"https://github.com/mikeshiyan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Iterate\n\n[![Build Status](https://travis-ci.org/mikeshiyan/iterate.svg?branch=master)](https://travis-ci.org/mikeshiyan/iterate)\n\nPHP classes and interfaces to iterate `\\Iterator`s by a scenario or to perform a\nregular expression match on iterator elements by a scenario.\n\nA Scenario is a class that implements the `ScenarioInterface` or extends either\n`BaseScenario` or `BaseRegexScenario` abstract class. It receives an `Iterator`\ninstance and executes the logic in certain moments of the iteration process -\nfor example, while regex matching is performed - when some pattern matched a\nsubject, or none matched, or before/after performing a search, etc.\n\nThink of it as of [`array_walk()`](http://php.net/manual/function.array-walk.php)\nfor iterators or [`iterator_apply()`](http://php.net/manual/function.iterator-apply.php),\nbut instead of a single callback you provide an object (Scenario) with several\nmethods. And you can use these Scenarios as plugins.\n\nIterator is simply any object implementing the [`\\Iterator`](http://php.net/manual/class.iterator.php)\ninterface.\n\nThe main class, which runs the process, is `Iterate`. It just needs to be\ninstantiated and invoked with an iterator and a scenario objects.\n\nBest suited for use as a [Composer](https://getcomposer.org) library.\n\n## Requirements\n\n* PHP \u0026ge; 7.1\n\n## Installation\n\nTo add this library to your Composer project:\n```\ncomposer require shiyan/iterate\n```\n\n## Usage\n\nThere are 2 base scenarios included:\n* The very basic one to execute some logic on each element of the iterator\nunconditionally.\n* And the regex based one, which performs a regular expression match (using one\nor more patterns) on iterator elements (casted to strings), and allows to\nexecute different logic depending on what pattern matched or if no patterns\nmatched.\n\n### Example\n\nAssuming there is an array of strings, `$array`. Iterate over it,\nconvert all empty strings to 0 (zero), print all strings containing \"PHP\" and\nsimply count all other elements.\n\nFirst, create a scenario class:\n```\nuse Shiyan\\Iterate\\Scenario\\BaseRegexScenario;\n\nclass ExampleScenario extends BaseRegexScenario {\n\n  const PATTERN_EMPTY = '/^$/';\n  const PATTERN_PHP = '/PHP/';\n\n  public $counter;\n\n  public function getPatterns(): array {\n    return [self::PATTERN_EMPTY, self::PATTERN_PHP];\n  }\n\n  public function preRun(): void {\n    $this-\u003ecounter = 0;\n  }\n\n  public function onMatch(array $matches, string $pattern): void {\n    switch ($pattern) {\n      case self::PATTERN_EMPTY:\n        $this-\u003eiterator[$this-\u003eiterator-\u003ekey()] = 0;\n        break;\n\n      case self::PATTERN_PHP:\n        print $this-\u003eiterator-\u003ecurrent() . \"\\n\";\n        break;\n    }\n  }\n\n  public function ifNotMatched(): void {\n    ++$this-\u003ecounter;\n  }\n\n}\n```\n\nAnd then:\n```\nuse Shiyan\\Iterate\\Iterate;\n\n// Convert our array of strings to the iterator object.\n$iterator = new ArrayIterator($array);\n$scenario = new ExampleScenario();\n$iterate = new Iterate();\n\n// Invoke iterate with our scenario.\n$iterate($iterator, $scenario);\nprint \"Found {$scenario-\u003ecounter} non-empty, non-PHP strings.\\n\";\n\n// Let's check that empty strings were converted to zeros.\nprint_r($iterator-\u003egetArrayCopy());\n\n// If we invoke Iterate with the same scenario once again, it won't find empty\n// strings anymore. Instead, we'll have a higher number in the $counter property\n// of the $scenario object, because the \"0\" doesn't match our patterns.\n$iterate($iterator, $scenario);\nprint \"Found {$scenario-\u003ecounter} non-empty, non-PHP strings.\\n\";\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeshiyan%2Fiterate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikeshiyan%2Fiterate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeshiyan%2Fiterate/lists"}