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)) { |
|
|
|
|
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
|
|
|
} |
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 theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.