Completed
Push — master ( 9e9c4d...878d3c )
by Babak
02:26
created

ArrayHelper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 105
ccs 28
cts 32
cp 0.875
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A serializeArray() 0 15 3
A getValue() 0 4 1
A getKeys() 0 4 1
A getKey() 0 4 1
A getDeep() 0 17 4
A isMapArray() 0 10 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alive
5
 * Date: 10/6/17
6
 * Time: 11:48 AM
7
 */
8
9
namespace Alive2212\ArrayHelper;
10
11
12
use PhpParser\Node\Expr\Cast\Double;
13
use phpseclib\Math\BigInteger;
14
15
class ArrayHelper
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $resultValue = [];
21
22
    /**
23
     * @var array
24
     */
25
    protected $resultKey = [];
26
27
    /**
28
     * ArrayHelper constructor.
29
     */
30 1
    public function __construct()
31
    {
32 1
    }
33
34
    /**
35
     * @param $array
36
     * @param int $parentCode
37
     * @param $parentKey
38
     * @return $this
39
     */
40 1
    public function serializeArray($array, $parentCode = 0, $parentKey = '')
41
    {
42 1
        $parentCode *= 100;
43 1
        $itemCode = 1;
44 1
        foreach ($array as $key => $value) {
45 1
            if (is_array($value)) {
46 1
                $this->serializeArray($value, $parentCode + $itemCode, $parentKey . '.' . $key);
47
            } else {
48 1
                $this->resultValue = array_add($this->resultValue, $parentCode + $itemCode, $value);
49 1
                $this->resultKey = array_merge($this->resultKey, [substr($parentKey . '.' . $key, 1) => $parentCode + $itemCode]);
50
            }
51 1
            $itemCode++;
52
        }
53 1
        return $this;
54
    }
55
56
    /**
57
     * @return array
58
     */
59 1
    public function getValue()
60
    {
61 1
        return $this->resultValue;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getKeys()
68
    {
69
        return $this->resultKey;
70
    }
71
72
    /**
73
     * @param $key
74
     * @return mixed
75
     */
76
    public function getKey($key)
77
    {
78
        return $this->resultKey[$key];
79
    }
80
81
    /**
82
     * @param $array
83
     * @param $keys
84
     * @param string $delimiter
85
     * @return null
86
     */
87 1
    public function getDeep($array, $keys, $delimiter = '.')
88
    {
89 1
        if (is_array($keys)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
90
91
        } else {
92 1
            $keyValues = explode($delimiter, $keys);
93
94 1
            foreach ($keyValues as $keyValue) {
95 1
                if (isset($array[$keyValue])) {
96 1
                    $array = $array[$keyValue];
97
                } else {
98 1
                    return null;
99
                }
100
            }
101
        }
102 1
        return $array;
103
    }
104
105
    /**
106
     * @param $array
107
     * @return bool
108
     */
109 1
    public function isMapArray($array)
110
    {
111 1
        $result = false;
112 1
        foreach ($array as $key => $value) {
113 1
            if (!is_numeric($key)) {
114 1
                $result = true;
115
            }
116
        }
117 1
        return $result;
118
    }
119
}