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

ArrayHelper::serializeArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 11
nc 3
nop 3
crap 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
}