bvtterfly/cycler

This package is abandoned and no longer maintained. No replacement package was suggested.

An easy way to rotate through several values is to use cycler

Fund package maintenance!
bvtterfly

1.0.0 2022-04-12 09:18 UTC

README

🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨

I no longer use PHP and cannot justify the time needed to maintain this package. That's why I have chosen to abandon it. Feel free to fork my code and maintain your own copy.

Cycler : An easy way to rotate through several values

Latest Version on Packagist Tests Total Downloads

An easy way to rotate through several values is to use cycler, which takes any number of arguments and cycles through them.

$cycle = cycler('odd', 'even');
$numbers = [1,2,3,4,5,6];
foreach ($numbers as $number) {
    echo "{$number} is {$cycle->next()}";
}
// prints 1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even

Installation

You can install the package via composer:

composer require bvtterfly/cycler

Usage

Let's say we want to display ads every three posts on our blog's home page. Then we can use cycler:

$adsCycle = cycler(false, false, true);
foreach ($posts as $post) {
    // show post
    if ($adsCycle->next()) {
        // show add
    }
}

You can access the current item using the current method (in the above example, $adsCycle->current())

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.