ArrayHelper::prevKey()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 2
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-helpers
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\helpers;
9
10
use yii\helpers\BaseArrayHelper;
11
12
/**
13
 * Extended default Array helper.
14
 *
15
 * @author Vladimir Kuprienko <[email protected]>
16
 * @since 1.0
17
 */
18
class ArrayHelper extends BaseArrayHelper
19
{
20
    /**
21
     * This class should not be instantiate.
22
     */
23
    private function __construct()
24
    {
25
    }
26
27
    /**
28
     * Returns next key of element in array.
29
     *
30
     * @param int|string    $currentKey
31
     * @param array         $array
32
     *
33
     * @return int|string|null Returns null if element with current key was last.
34
     */
35
    public static function nextKey($currentKey, array $array)
36
    {
37
        reset($array);
38
        do {
39
            $tmpKey = key($array);
40
            if (false === next($array)) {
41
                return null;
42
            }
43
        } while ($tmpKey !== $currentKey);
44
45
        return key($array);
46
    }
47
48
    /**
49
     * Returns prev key of element in array.
50
     *
51
     * @param int|string    $currentKey
52
     * @param array         $array
53
     *
54
     * @return int|string|null Returns null if element with current key was first.
55
     */
56
    public static function prevKey($currentKey, array $array)
57
    {
58
        end($array);
59
        do {
60
            $tmpKey = key($array);
61
            if (false === prev($array)) {
62
                return null;
63
            }
64
        } while ($tmpKey !== $currentKey);
65
66
        return key($array);
67
    }
68
}
69