1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JimmyOak\Utility; |
4
|
|
|
|
5
|
|
|
class ArrayUtils extends UtilsBase |
6
|
|
|
{ |
7
|
|
|
const NO_PRESERVE_KEYS = 0; |
8
|
|
|
const PRESERVE_KEYS = 1; |
9
|
|
|
const PRESERVE_ASSOCIATIVE_KEYS = 2; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param array $toFlat |
13
|
|
|
* @param int $preserveKeys |
14
|
|
|
* |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
public function flatten(array $toFlat, $preserveKeys = self::NO_PRESERVE_KEYS) |
18
|
|
|
{ |
19
|
|
|
if ($preserveKeys === self::PRESERVE_KEYS) { |
20
|
|
|
$flatten = $this->flattenPreservingKeys($toFlat); |
21
|
|
|
} elseif ($preserveKeys === self::PRESERVE_ASSOCIATIVE_KEYS) { |
22
|
|
|
$flatten = $this->flattenPreservingAssociativeKeys($toFlat); |
23
|
|
|
} else { |
24
|
|
|
$flatten = $this->flattenNotPreservingKeys($toFlat); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $flatten; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $data |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function toXmlString(array $data) |
36
|
|
|
{ |
37
|
|
|
$xml = '<?xml version="1.0" encoding="UTF-8"?>'; |
38
|
|
|
|
39
|
|
|
$xml .= $this->parseAsXml($data); |
40
|
|
|
|
41
|
|
|
return $xml; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $arrayToParseAsXml |
46
|
|
|
* |
47
|
|
|
* @return \SimpleXMLElement |
48
|
|
|
*/ |
49
|
|
|
public function toXml(array $arrayToParseAsXml) |
50
|
|
|
{ |
51
|
|
|
return simplexml_load_string($this->parseAsXml($arrayToParseAsXml)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
private function flattenPreservingKeys(array $toFlat) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$flatten = []; |
57
|
|
|
|
58
|
|
|
foreach ($toFlat as $key => $value) { |
59
|
|
|
if (is_array($value)) { |
60
|
|
|
$flatten = array_replace($flatten, $this->flatten($value, self::PRESERVE_KEYS)); |
61
|
|
|
} else { |
62
|
|
|
$flatten[$key] = $value; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $flatten; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
private function flattenNotPreservingKeys(array $toFlat) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$flatten = []; |
72
|
|
|
|
73
|
|
|
foreach ($toFlat as $value) { |
74
|
|
|
if (is_array($value)) { |
75
|
|
|
$flatten = array_merge($flatten, $this->flatten($value, self::NO_PRESERVE_KEYS)); |
76
|
|
|
} else { |
77
|
|
|
$flatten[] = $value; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $flatten; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function flattenPreservingAssociativeKeys(array $toFlat) |
85
|
|
|
{ |
86
|
|
|
$flatten = []; |
87
|
|
|
|
88
|
|
|
foreach ($toFlat as $key => $value) { |
89
|
|
|
if (is_array($value)) { |
90
|
|
|
$flatten = array_merge($flatten, $this->flatten($value, self::PRESERVE_ASSOCIATIVE_KEYS)); |
91
|
|
|
} elseif (is_int($key)) { |
92
|
|
|
$flatten[] = $value; |
93
|
|
|
} else { |
94
|
|
|
$flatten[$key] = $value; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $flatten; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function parseAsXml(array $data, $inheritedKey = null) |
102
|
|
|
{ |
103
|
|
|
$xml = ''; |
104
|
|
|
|
105
|
|
|
foreach ($data as $key => $value) { |
106
|
|
|
if (!is_scalar($value)) { |
107
|
|
|
if ($this->isAssociative($value) || !$value) { |
108
|
|
|
$xml .= $this->parseXmlKeyValue($key, $this->parseAsXml($value)); |
109
|
|
|
} else { |
110
|
|
|
$xml .= $this->parseAsXml($value, $key); |
111
|
|
|
} |
112
|
|
|
} elseif ($inheritedKey !== null) { |
113
|
|
|
$xml .= $this->parseXmlKeyValue($inheritedKey, $value); |
114
|
|
|
} else { |
115
|
|
|
$xml .= $this->parseXmlKeyValue($key, $value); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $xml; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function parseXmlKeyValue($key, $value = null) |
123
|
|
|
{ |
124
|
|
|
if (empty($value)) { |
125
|
|
|
return "<$key/>"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return "<$key>" . $value . "</$key>"; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function isAssociative(array $data) |
132
|
|
|
{ |
133
|
|
|
$keys = array_keys($data); |
134
|
|
|
|
135
|
|
|
foreach ($keys as $key) { |
136
|
|
|
if (!is_int($key)) { |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.