1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AppUtils; |
6
|
|
|
|
7
|
|
|
class ConvertHelper_Array |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Removes the specified keys from the target array, |
11
|
|
|
* if they exist. |
12
|
|
|
* |
13
|
|
|
* @param array $sourceArray |
14
|
|
|
* @param array $keys |
15
|
|
|
*/ |
16
|
|
|
public static function removeKeys(array &$sourceArray, array $keys) : void |
17
|
|
|
{ |
18
|
|
|
foreach($keys as $key) |
19
|
|
|
{ |
20
|
|
|
if(array_key_exists($key, $sourceArray)) { |
21
|
|
|
unset($sourceArray[$key]); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @param array $sourceArray The indexed or associative array |
29
|
|
|
* @param array $values Indexed list of values to remove |
30
|
|
|
* @param bool $keepKeys Whether to maintain index association |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public static function removeValues(array $sourceArray, array $values, bool $keepKeys=false) : array |
34
|
|
|
{ |
35
|
|
|
$result = array(); |
36
|
|
|
$values = array_values($values); |
37
|
|
|
|
38
|
|
|
foreach($sourceArray as $key => $value) |
39
|
|
|
{ |
40
|
|
|
if(in_array($value, $values, true)) { |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if($keepKeys) { |
45
|
|
|
$result[$key] = $value; |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$result[] = $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Removes values from the target array, while maintaining index |
57
|
|
|
* association. Returns the modified array. |
58
|
|
|
* |
59
|
|
|
* @param array $sourceArray |
60
|
|
|
* @param array $values |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public static function removeValuesAssoc(array $sourceArray, array $values) : array |
64
|
|
|
{ |
65
|
|
|
return self::removeValues($sourceArray, $values, true); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Converts an associative array to an HTML style attribute value string. |
70
|
|
|
* |
71
|
|
|
* @param array<string,mixed> $subject |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public static function toStyleString(array $subject) : string |
75
|
|
|
{ |
76
|
|
|
$tokens = array(); |
77
|
|
|
foreach($subject as $name => $value) { |
78
|
|
|
$tokens[] = $name.':'.strval($value); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return implode(';', $tokens); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Converts an associative array with attribute name > value pairs |
86
|
|
|
* to an attribute string that can be used in an HTML tag. Empty |
87
|
|
|
* attribute values are ignored. |
88
|
|
|
* |
89
|
|
|
* Example: |
90
|
|
|
* |
91
|
|
|
* array2attributeString(array( |
92
|
|
|
* 'id' => 45, |
93
|
|
|
* 'href' => 'http://www.mistralys.com' |
94
|
|
|
* )); |
95
|
|
|
* |
96
|
|
|
* Result: |
97
|
|
|
* |
98
|
|
|
* id="45" href="http://www.mistralys.com" |
99
|
|
|
* |
100
|
|
|
* @param array<string,mixed> $array |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public static function toAttributeString(array $array) : string |
104
|
|
|
{ |
105
|
|
|
$tokens = array(); |
106
|
|
|
foreach($array as $attr => $value) |
107
|
|
|
{ |
108
|
|
|
$value = strval($value); |
109
|
|
|
|
110
|
|
|
if($value === '') { |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$tokens[] = $attr.'="'.htmlspecialchars($value, ENT_QUOTES, 'UTF-8').'"'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if(empty($tokens)) { |
118
|
|
|
return ''; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return ' '.implode(' ', $tokens); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Implodes an array with a separator character, and the last item with "and". |
126
|
|
|
* |
127
|
|
|
* By default, this will create the following result: |
128
|
|
|
* |
129
|
|
|
* array('One', 'two', 'three') = "One, two and three" |
130
|
|
|
* |
131
|
|
|
* @param array $list The indexed array with items to implode. |
132
|
|
|
* @param string $sep The separator character to use. |
133
|
|
|
* @param string $conjunction The word to use as conjunction with the last item in the list. NOTE: include spaces as needed. |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public static function implodeWithAnd(array $list, string $sep = ', ', string $conjunction = '') : string |
137
|
|
|
{ |
138
|
|
|
if(empty($list)) { |
139
|
|
|
return ''; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if(empty($conjunction)) { |
143
|
|
|
$conjunction = ' '.t('and').' '; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$last = array_pop($list); |
147
|
|
|
if($list) { |
148
|
|
|
return implode($sep, $list) . $conjunction . $last; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $last; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|