Test Failed
Push — main ( e26c51...cb447d )
by Rafael
10:44
created

ArrayUtils::arrayMergeRecursiveEx()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 4
nop 2
dl 0
loc 13
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Alxarafe. Development of PHP applications in a flash!
4
 * Copyright (C) 2018-2020 Alxarafe <[email protected]>
5
 */
6
7
namespace Alxarafe\Core\Utils;
8
9
class ArrayUtils
10
{
11
    /**
12
     * Flatten an array to leave it at a single level.
13
     * Ignore the value of the indexes of the array, taking only the values.
14
     * Remove spaces from the result and convert it to lowercase.
15
     *
16
     * TODO: Tests works, but seems not to do what is explained
17
     *
18
     * @param array $array
19
     *
20
     * @return array
21
     */
22
    public static function flatArray(array $array): array
23
    {
24
        $ret = [];
25
        foreach ($array as $value) {
26
            if (is_array($value)) {
27
                $ret = array_merge($ret, self::flatArray($value));
28
            } else {
29
                $ret[] = strtolower(trim($value));
30
            }
31
        }
32
        return $ret ?? [];
33
    }
34
35
    /**
36
     * Add the elements of the 2nd array behind those of the first.
37
     *
38
     * @param array $initialArray
39
     * @param array $nextArray
40
     *
41
     * @return array
42
     */
43
    public static function addToArray(array $initialArray, array $nextArray): array
44
    {
45
        $ret = $initialArray;
46
        foreach ($nextArray as $value) {
47
            $ret[] = $value;
48
        }
49
        return $ret;
50
    }
51
52
    /**
53
     * Return true if $param is setted and is 'yes', otherwise return false.
54
     *
55
     * @param array  $param
56
     * @param string $key
57
     *
58
     * @return bool
59
     */
60
    public static function isTrue(array $param, $key): bool
61
    {
62
        return (isset($param[$key]) && in_array($param[$key], ['yes', 'true', '1', 1], true));
63
    }
64
65
    /**
66
     * Given an array of parameters, an index and a possible default value,
67
     * returns a literal of the form: index = 'value'.
68
     * It is used, for example, to assign attributes to an html statement.
69
     *
70
     * @param array       $itemsArray
71
     * @param string      $itemIndex
72
     * @param string|null $defaultValue
73
     *
74
     * @return string
75
     */
76
    public static function getItem(array $itemsArray, string $itemIndex, $defaultValue = null): string
77
    {
78
        $res = $itemsArray[$itemIndex] ?? $defaultValue;
79
        return isset($res) ? " $itemIndex = '$res'" : '';
80
    }
81
82
    /**
83
     * Array recursive merge excluding duplicate values.
84
     *
85
     * @source https://github.com/manusreload/GLFramework/blob/master/src/functions.php#L292
86
     *
87
     * @param array $array1
88
     * @param array $array2
89
     *
90
     * @return array
91
     */
92
    public static function arrayMergeRecursiveEx(array &$array1, array &$array2): array
93
    {
94
        $merged = $array1;
95
        foreach ($array2 as $key => &$value) {
96
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
97
                $merged[$key] = self::arrayMergeRecursiveEx($merged[$key], $value);
98
            } elseif (is_numeric($key) && !in_array($value, $merged, false)) {
99
                $merged[] = $value;
100
            } else {
101
                $merged[$key] = $value;
102
            }
103
        }
104
        return $merged;
105
    }
106
}
107