{"id":19779238,"url":"https://github.com/isaeken/loops","last_synced_at":"2025-04-30T21:31:27.534Z","repository":{"id":45200557,"uuid":"412142727","full_name":"isaeken/loops","owner":"isaeken","description":"Simple Loop Class","archived":false,"fork":false,"pushed_at":"2022-02-09T06:44:09.000Z","size":72,"stargazers_count":20,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T04:51:09.552Z","etag":null,"topics":["php"],"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/isaeken.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null},"funding":{"github":"isaeken"}},"created_at":"2021-09-30T16:36:33.000Z","updated_at":"2023-12-19T12:21:29.000Z","dependencies_parsed_at":"2022-09-02T07:00:20.770Z","dependency_job_id":null,"html_url":"https://github.com/isaeken/loops","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaeken%2Floops","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaeken%2Floops/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaeken%2Floops/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaeken%2Floops/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/isaeken","download_url":"https://codeload.github.com/isaeken/loops/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251785428,"owners_count":21643479,"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":["php"],"created_at":"2024-11-12T05:33:56.620Z","updated_at":"2025-04-30T21:31:27.272Z","avatar_url":"https://github.com/isaeken.png","language":"PHP","funding_links":["https://github.com/sponsors/isaeken"],"categories":[],"sub_categories":[],"readme":"# Loops\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/isaeken/loops.svg?style=flat-square)](https://packagist.org/packages/isaeken/loops)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/isaeken/loops/run-tests?label=tests)](https://github.com/isaeken/loops/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/isaeken/loops/Check%20\u0026%20fix%20styling?label=code%20style)](https://github.com/isaeken/loops/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/isaeken/loops.svg?style=flat-square)](https://packagist.org/packages/isaeken/loops)\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require isaeken/loops\n```\n\n## Usage\n\n### Basic Usage\n\n````php\nloop(5, function (\\IsaEken\\Loops\\Index $index) {\n    return $index-\u003eodd;\n}); // [false, true, false, true, false]\n````\n\n### Async Usage\n\n````php\n$loop = async_loop(5, function (\\IsaEken\\Loops\\Index $index) {\n    return $index-\u003eeven;\n});\n\n// ...\n\nawait($loop); // [true, false, true, false, true]\n````\n\n### Using with class method\n\n```php\n$callback = new class implements \\IsaEken\\Loops\\Contracts\\LoopCallback {\n    public function __invoke(Index $index, Loop $loop = null): int\n    {\n        return $index-\u003eindex;\n    }\n};\n\n$loop = new Loop(2, $callback);\n$loop-\u003erun();\n$loop-\u003eresults(); // [0, 1]\n```\n\n### Get current loop\n\n````php\nloop(2 ,function (\\IsaEken\\Loops\\Index $index, \\IsaEken\\Loops\\Loop $loop) {\n    return [\n        'iteration' =\u003e $index-\u003eiteration,\n        'index'     =\u003e $index-\u003eindex,\n        'remaining' =\u003e $index-\u003eremaining,\n        'count'     =\u003e $index-\u003ecount,\n        'first'     =\u003e $index-\u003efirst,\n        'last'      =\u003e $index-\u003elast,\n        'odd'       =\u003e $index-\u003eodd,\n        'even'      =\u003e $index-\u003eeven,\n    ];\n});\n// [\n//  [\n//    'iteration' =\u003e 1,\n//    'index' =\u003e 0,\n//    'remaining' =\u003e 1,\n//    'count' =\u003e 2,\n//    'first' =\u003e true,\n//    'last' =\u003e false,\n//    'odd' =\u003e false,\n//    'even' =\u003e true,\n//  ],\n//  [\n//    'iteration' =\u003e 2,\n//    'index' =\u003e 1,\n//    'remaining' =\u003e 0,\n//    'count' =\u003e 2,\n//    'first' =\u003e false,\n//    'last' =\u003e true,\n//    'odd' =\u003e true,\n//    'even' =\u003e false,\n//  ]\n// ]\n````\n\n### Break the loop\n\n````php\nloop(3, function (\\IsaEken\\Loops\\Index $index, \\IsaEken\\Loops\\Loop $loop) {\n    if ($index-\u003eindex \u003e 1) {\n        $loop-\u003ebreak();\n    }\n    \n    return $index-\u003eindex;\n}); // [0, 1]\n````\n\n### Loop random times\n\n````php\nloop_random(function (\\IsaEken\\Loops\\Index $index, \\IsaEken\\Loops\\Loop $loop) {\n    return $index-\u003eindex;\n}); // executed random times.\n\n$min = 5;\n$max = 10;\n\nloop_random(function (\\IsaEken\\Loops\\Index $index, \\IsaEken\\Loops\\Loop $loop) {\n    return $index-\u003eindex;\n}, $min, $max);\n````\n\n### Loop random with seed\n\n```php\nloop_random(function (\\IsaEken\\Loops\\Index $index) {\n    return $index-\u003eeven;\n}, seed: 123456789);\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Isa Eken](https://github.com/isaeken)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisaeken%2Floops","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fisaeken%2Floops","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisaeken%2Floops/lists"}