Completed
Pull Request — master (#42)
by Vladimir
02:25
created

ArrayUtilities::associative_array_split()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Utilities;
9
10
abstract class ArrayUtilities
0 ignored issues
show
Coding Style introduced by
ArrayUtilities does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
11
{
12 6
    public static function is_multidimensional(array &$array)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
Coding Style introduced by
function is_multidimensional() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
13
    {
14 6
        foreach ($array as $element)
15
        {
16 6
            if (is_array($element))
17 6
            {
18 1
                return true;
19
            }
20 5
        }
21
22 5
        return false;
23
    }
24
25
    /**
26
     * @param  array  $array1
27
     * @param  array  $array2
28
     * @param  string $keyField
29
     *
30
     * @return array
31
     */
32 27
    public static function array_merge_defaults(array &$array1, array &$array2, $keyField)
0 ignored issues
show
Coding Style introduced by
function array_merge_defaults() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
33
    {
34 27
        $merged = $array1;
35
36 27
        foreach ($array2 as $key => &$value)
37
        {
38 14
            $valueMerged = false;
39
40 14
            foreach ($merged as $mergedKey => &$item)
41
            {
42 14
                if (is_array($item) && array_key_exists($keyField, $item) && $item[$keyField] == $value[$keyField])
43 14
                {
44
                    $item = array_merge($item, $value);
45
                    $valueMerged = true;
46
47
                    break;
48
                }
49 14
                elseif ($mergedKey == $key)
50
                {
51 14
                    if (is_numeric($mergedKey))
52 14
                    {
53
                        $merged[] = $value;
54
                    }
55 14
                    elseif (is_array($item))
56
                    {
57 14
                        $item = array_unique(array_merge($item, $value));
58 14
                    }
59
                    else
60
                    {
61 14
                        $item = $value;
62
                    }
63
64 14
                    $valueMerged = true;
65
66 14
                    break;
67
                }
68 14
            }
69
70 14
            if (!$valueMerged)
71 14
            {
72 14
                $merged[$key] = $value;
73 14
            }
74 27
        }
75
76 27
        return $merged;
77
    }
78
79
    /**
80
     * @param  string $key
81
     * @param  array  $array
82
     * @param  bool   $considerOffset
83
     *
84
     * @return array
85
     */
86
    public static function associative_array_split($key, array &$array, $considerOffset = true)
0 ignored issues
show
Coding Style introduced by
function associative_array_split() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
87
    {
88
        $offset = array_search($key, array_keys($array)) + (int)$considerOffset;
89
        $result = array();
90
91
        $result[0] = array_slice($array, 0, $offset, true);
92
        $result[1] = array_slice($array, $offset, null, true);
93
94
        return $result;
95
    }
96
}
97