Debian Codegraph @

vendor/jfcherng/php-diff/src/Utility/ReverseIterator.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jfcherng\Diff\Utility;
6
7 final class ReverseIterator
8 {
9 public const ITERATOR_GET_VALUE = 0;
10 public const ITERATOR_GET_KEY = 1 << 0;
11 public const ITERATOR_GET_BOTH = 1 << 1;
12
13 /**
14 * The constructor.
15 */
16 private function __construct()
17 {
18 }
19
20 /**
21 * Iterate the array reversely.
22 *
23 * @param array $array the array
24 * @param int $flags the flags
25 */
26 public static function fromArray(array $array, int $flags = self::ITERATOR_GET_VALUE): \Generator
27 {
28 // iterate [key => value] pair
29 if ($flags & self::ITERATOR_GET_BOTH) {
30 for (end($array); ($key = key($array)) !== null; prev($array)) {
31 yield $key => current($array);
32 }
33
34 return;
35 }
36
37 // iterate only key
38 if ($flags & self::ITERATOR_GET_KEY) {
39 for (end($array); ($key = key($array)) !== null; prev($array)) {
40 yield $key;
41 }
42
43 return;
44 }
45
46 // iterate only value
47 for (end($array); key($array) !== null; prev($array)) {
48 yield current($array);
49 }
50 }
51 }