Completed
Push — master ( ded833...69ca06 )
by Nate
06:38
created

ArrayHelper::insertSequential()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 26
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 6
nop 3
crap 30
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\helpers;
10
11
/**
12
 * @author Flipbox Factory <[email protected]>
13
 * @since 1.0.0
14
 */
15
class ArrayHelper extends \craft\helpers\ArrayHelper
16
{
17
    /**
18
     * Filters null values from an array.
19
     *
20
     * @param array $arr
21
     *
22
     * @return array
23
     */
24
    public static function filterNullValuesFromArray(array $arr): array
25
    {
26
        return array_filter($arr, function ($value): bool {
27
            return $value !== null;
28
        });
29
    }
30
31
    /**
32
     * Filters null values from an array.
33
     *
34
     * @param array $arr
35
     *
36
     * @return array
37
     */
38
    public static function filterEmptyAndNullValuesFromArray(array $arr): array
39
    {
40
        return array_filter($arr, function ($value): bool {
41
            return $value !== null && $value !== '';
42
        });
43
    }
44
45
    /**
46
     * @param array $sourceArray The source array which the target is to be inserted into.  The
47
     * key represents a unique identifier, while the value is the sort order.
48
     *
49
     * As an example if this is the $sourceArray
50
     *
51
     * ```
52
     * [
53
     *      111 => 1,
54
     *      343 => 2,
55
     *      545 => 3,
56
     *      'foo' => 4,
57
     *      'bar' => 5
58
     * ]
59
     * ```
60
     *
61
     * And your $targetKey is 'fooBar' with a $targetOrder of 4, the result would be
62
     *
63
     * ```
64
     * [
65
     *      111 => 1,
66
     *      343 => 2,
67
     *      545 => 3,
68
     *      'fooBar' => 4,
69
     *      'foo' => 5,
70
     *      'bar' => 6
71
     * ]
72
     * ```
73
     *
74
     * @param string|int $targetKey
75
     * @param int $targetOrder
76
     * @return array|bool
77
     */
78
    public static function insertSequential(array $sourceArray, $targetKey, int $targetOrder)
79
    {
80
        // Append exiting types after position
81
        if (false === ($indexPosition = array_search($targetKey, array_keys($sourceArray)))) {
82
            return false;
83
        }
84
85
        // All types that are affected by re-ordering
86
        $affectedTypes = array_slice($sourceArray, $indexPosition, null, true);
87
88
        // Remove the current type (we're going to put it back in later)
89
        $currentPosition = (int)ArrayHelper::remove($affectedTypes, $targetKey);
90
91
        // Already in that position?
92
        if ($currentPosition === $targetOrder) {
93
            return true;
94
        }
95
96
        $startingSortOrder = $targetOrder;
97
        if ($indexPosition++ < $targetOrder) {
98
            $startingSortOrder = $indexPosition;
99
        }
100
101
        // Prepend current type
102
        $order = [$targetKey => $targetOrder];
103
104
        // Assemble order
105
        if (false !== ($position = array_search($targetOrder, array_values($affectedTypes)))) {
106
            $position++;
107
108
            $order = array_slice($affectedTypes, 0, $position, true) +
109
                $order +
110
                array_slice($affectedTypes, $position, null, true);
111
        }
112
113
        return array_combine(
114
            range($startingSortOrder, count($order) + 1),
115
            array_keys($order)
116
        );
117
    }
118
}
119