Completed
Push — master ( 3bd1c4...8d04e0 )
by Vladimir
05:20
created

ArrayHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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
     * @return int|string|null Returns null if element with current key was last.
33
     */
34
    public static function nextKey($currentKey, array $array)
35
    {
36
        reset($array);
37
        do {
38
            $tmpKey = key($array);
39
            if (next($array) === false) {
40
                return null;
41
            }
42
        } while ($tmpKey !== $currentKey);
43
44
        return key($array);
45
    }
46
47
    /**
48
     * Returns prev key of element in array.
49
     *
50
     * @param int|string $currentKey
51
     * @param array $array
52
     * @return int|string|null Returns null if element with current key was first.
53
     */
54
    public static function prevKey($currentKey, array $array)
55
    {
56
        end($array);
57
        do {
58
            $tmpKey = key($array);
59
            if (prev($array) === false) {
60
                return null;
61
            }
62
        } while ($tmpKey !== $currentKey);
63
64
        return key($array);
65
    }
66
}
67