CoreFiles by mySites.guru
ReverseIterator.php51 lines, 1,198 bytes, from Joomla_4.4.0-alpha1-Alpha-Full_Package.zip
Raw History Compare with 4.3.0-alpha3
md5
2939cbfefc776c3b05decd26e06ed85b
sha1
78a747f119b9f1a81a069b2d4cc5df0f9e98c624
sha256
2669939e25e18df6119e3c9c3f8ef3b260600a1dc5ce7824c12dce2ea512874b
<?php

declare(strict_types=1);

namespace Jfcherng\Diff\Utility;

final class ReverseIterator
{
    public const ITERATOR_GET_VALUE = 0;
    public const ITERATOR_GET_KEY = 1 << 0;
    public const ITERATOR_GET_BOTH = 1 << 1;

    /**
     * The constructor.
     */
    private function __construct()
    {
    }

    /**
     * Iterate the array reversely.
     *
     * @param array $array the array
     * @param int   $flags the flags
     */
    public static function fromArray(array $array, int $flags = self::ITERATOR_GET_VALUE): \Generator
    {
        // iterate [key => value] pair
        if ($flags & self::ITERATOR_GET_BOTH) {
            for (\end($array); ($key = \key($array)) !== null; \prev($array)) {
                yield $key => \current($array);
            }

            return;
        }

        // iterate only key
        if ($flags & self::ITERATOR_GET_KEY) {
            for (\end($array); ($key = \key($array)) !== null; \prev($array)) {
                yield $key;
            }

            return;
        }

        // iterate only value
        for (\end($array); \key($array) !== null; \prev($array)) {
            yield \current($array);
        }
    }
}

The same content is in 56 releases, Joomla 4.3.0-alpha1 to 4.4.14. See them all, or every version of this file.