https://github.com/isaeken/loops
Simple Loop Class
https://github.com/isaeken/loops
php
Last synced: about 1 year ago
JSON representation
Simple Loop Class
- Host: GitHub
- URL: https://github.com/isaeken/loops
- Owner: isaeken
- License: mit
- Created: 2021-09-30T16:36:33.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-02-09T06:44:09.000Z (over 4 years ago)
- Last Synced: 2025-04-06T04:51:09.552Z (about 1 year ago)
- Topics: php
- Language: PHP
- Homepage:
- Size: 70.3 KB
- Stars: 20
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Loops
[](https://packagist.org/packages/isaeken/loops)
[](https://github.com/isaeken/loops/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/isaeken/loops/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[](https://packagist.org/packages/isaeken/loops)
## Installation
You can install the package via composer:
```bash
composer require isaeken/loops
```
## Usage
### Basic Usage
````php
loop(5, function (\IsaEken\Loops\Index $index) {
return $index->odd;
}); // [false, true, false, true, false]
````
### Async Usage
````php
$loop = async_loop(5, function (\IsaEken\Loops\Index $index) {
return $index->even;
});
// ...
await($loop); // [true, false, true, false, true]
````
### Using with class method
```php
$callback = new class implements \IsaEken\Loops\Contracts\LoopCallback {
public function __invoke(Index $index, Loop $loop = null): int
{
return $index->index;
}
};
$loop = new Loop(2, $callback);
$loop->run();
$loop->results(); // [0, 1]
```
### Get current loop
````php
loop(2 ,function (\IsaEken\Loops\Index $index, \IsaEken\Loops\Loop $loop) {
return [
'iteration' => $index->iteration,
'index' => $index->index,
'remaining' => $index->remaining,
'count' => $index->count,
'first' => $index->first,
'last' => $index->last,
'odd' => $index->odd,
'even' => $index->even,
];
});
// [
// [
// 'iteration' => 1,
// 'index' => 0,
// 'remaining' => 1,
// 'count' => 2,
// 'first' => true,
// 'last' => false,
// 'odd' => false,
// 'even' => true,
// ],
// [
// 'iteration' => 2,
// 'index' => 1,
// 'remaining' => 0,
// 'count' => 2,
// 'first' => false,
// 'last' => true,
// 'odd' => true,
// 'even' => false,
// ]
// ]
````
### Break the loop
````php
loop(3, function (\IsaEken\Loops\Index $index, \IsaEken\Loops\Loop $loop) {
if ($index->index > 1) {
$loop->break();
}
return $index->index;
}); // [0, 1]
````
### Loop random times
````php
loop_random(function (\IsaEken\Loops\Index $index, \IsaEken\Loops\Loop $loop) {
return $index->index;
}); // executed random times.
$min = 5;
$max = 10;
loop_random(function (\IsaEken\Loops\Index $index, \IsaEken\Loops\Loop $loop) {
return $index->index;
}, $min, $max);
````
### Loop random with seed
```php
loop_random(function (\IsaEken\Loops\Index $index) {
return $index->even;
}, seed: 123456789);
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Isa Eken](https://github.com/isaeken)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.