1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Admin Page Framework |
4
|
|
|
* |
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
6
|
|
|
* Copyright (c) 2013-2015 Michael Uno; Licensed MIT |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provides utility methods dealing with PHP arrays which do not use WordPress functions. |
12
|
|
|
* |
13
|
|
|
* Listed methods are to modify array contents. |
14
|
|
|
* |
15
|
|
|
* @since DEVVER |
16
|
|
|
* @package AdminPageFramework |
17
|
|
|
* @extends AdminPageFramework_Utility_String |
18
|
|
|
* @subpackage Utility |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
abstract class AdminPageFramework_Utility_ArrayGetter extends AdminPageFramework_Utility_Array { |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns a first iterated array element. |
25
|
|
|
* @since 3.6.0 |
26
|
|
|
* @since DVVER Moved from `AdminPageFramework_Utility_Array`. |
27
|
|
|
*/ |
28
|
|
|
static public function getFirstElement( array $aArray ) { |
|
|
|
|
29
|
|
|
foreach( $aArray as $_mElement ) { |
30
|
|
|
return $_mElement; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns an array element value by the given key. |
36
|
|
|
* |
37
|
|
|
* It just saves isset() conditional checks and allows a default value to be set. |
38
|
|
|
* |
39
|
|
|
* @since 3.4.0 |
40
|
|
|
* @since 3.5.3 The second parameter accepts an array representing dimensional keys. Added the fourth parameter to set values that the default value will be applied to. |
41
|
|
|
* @since DVVER Moved from `AdminPageFramework_Utility_Array`. |
42
|
|
|
* @param array $aSubject The subject array to parse. |
43
|
|
|
* @param string|array|integer $aisKey The key to check. If an array is passed, it checks dimensional keys. |
44
|
|
|
* @param mixed $mDefault The default value to return when the key is not set. |
45
|
|
|
* @param string|array $asToDefault When the returning value matches oen of the set values here, the value(s) will be discarded and the default value will be applied. |
46
|
|
|
* @return mixed The set value or the default value. |
47
|
|
|
*/ |
48
|
|
|
static public function getElement( $aSubject, $aisKey, $mDefault=null, $asToDefault=array( null ) ) { |
|
|
|
|
49
|
|
|
|
50
|
|
|
$_aToDefault = is_null( $asToDefault ) |
51
|
|
|
? array( null ) |
52
|
|
|
: self::getAsArray( $asToDefault, true ); |
53
|
|
|
$_mValue = self::getArrayValueByArrayKeys( |
54
|
|
|
$aSubject, |
55
|
|
|
self::getAsArray( $aisKey, true ), |
56
|
|
|
$mDefault |
57
|
|
|
); |
58
|
|
|
return in_array( $_mValue, $_aToDefault, true /* important! type-sensitive */ ) |
59
|
|
|
? $mDefault |
60
|
|
|
: $_mValue; |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns an array element value by the given key as an array. |
66
|
|
|
* |
67
|
|
|
* When the retrieving array element value is unknown whether it is set or not and it should be an array, |
68
|
|
|
* this method can save the lines of isset() and is_array(). |
69
|
|
|
* |
70
|
|
|
* @since 3.4.0 |
71
|
|
|
* @since 3.5.3 The second parameter accepts dimensional array keys and added the fourth parameter. |
72
|
|
|
* @since DVVER Moved from `AdminPageFramework_Utility_Array`. |
73
|
|
|
* @return array The cast retrieved element value. |
74
|
|
|
*/ |
75
|
|
|
static public function getElementAsArray( $aSubject, $aisKey, $mDefault=null, $asToDefault=array( null ) ) { |
|
|
|
|
76
|
|
|
return self::getAsArray( |
77
|
|
|
self::getElement( $aSubject, $aisKey, $mDefault, $asToDefault ), |
78
|
|
|
true // preserve an empty value |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Removes elements of non-numeric keys from the given array. |
84
|
|
|
* |
85
|
|
|
* @since 3.0.0 |
86
|
|
|
* @since 3.5.3 Changed the name from `getIntegerElements`. Added a type hint in the first parameter. |
87
|
|
|
* @since DVVER Moved from `AdminPageFramework_Utility_Array`. |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
static public function getIntegerKeyElements( array $aParse ) { |
|
|
|
|
91
|
|
|
|
92
|
|
|
foreach ( $aParse as $_isKey => $_v ) { |
93
|
|
|
|
94
|
|
|
if ( ! is_numeric( $_isKey ) ) { |
95
|
|
|
unset( $aParse[ $_isKey ] ); |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Convert string numeric value to integer or float. |
100
|
|
|
$_isKey = $_isKey + 0; |
101
|
|
|
|
102
|
|
|
if ( ! is_int( $_isKey ) ) { |
103
|
|
|
unset( $aParse[ $_isKey ] ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
return $aParse; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Removes integer keys from the array. |
112
|
|
|
* |
113
|
|
|
* @since 3.0.0 |
114
|
|
|
* @since 3.5.3 Changed the name from `getNonIntegerElements()`. |
115
|
|
|
* @since DVVER Moved from `AdminPageFramework_Utility_Array`. |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
static public function getNonIntegerKeyElements( array $aParse ) { |
|
|
|
|
119
|
|
|
|
120
|
|
|
foreach ( $aParse as $_isKey => $_v ) { |
121
|
|
|
if ( is_numeric( $_isKey ) && is_int( $_isKey+ 0 ) ) { |
122
|
|
|
unset( $aParse[ $_isKey ] ); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $aParse; |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieves an array element by the given array representing the dimensional key structure. |
131
|
|
|
* |
132
|
|
|
* e.g. The following code will yield eee. |
133
|
|
|
* <code> |
134
|
|
|
* $a = array( |
135
|
|
|
* 'a' => array( |
136
|
|
|
* 'b' => array( |
137
|
|
|
* 'c' => array( |
138
|
|
|
* 'd' => array( |
139
|
|
|
* 'e' => 'eee', |
140
|
|
|
* ), |
141
|
|
|
* ), |
142
|
|
|
* ), |
143
|
|
|
* ), |
144
|
|
|
* ); |
145
|
|
|
* $aKeys = array( 'a', 'b', 'c', 'd', 'e' ); |
146
|
|
|
* $v = getArrayValueByArrayKeys( $a, $aKeys, 'default value' ); |
147
|
|
|
* var_dump( $v ); |
148
|
|
|
* </code> |
149
|
|
|
* |
150
|
|
|
* @since 3.0.1 |
151
|
|
|
* @since DVVER Moved from `AdminPageFramework_Utility_Array`. |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
static public function getArrayValueByArrayKeys( $aArray, $aKeys, $vDefault=null ) { |
|
|
|
|
155
|
|
|
|
156
|
|
|
$_sKey = array_shift( $aKeys ); |
157
|
|
|
|
158
|
|
|
// array_key_exists( $_sKey, $aArray ) caused warnings in some occasions |
159
|
|
|
if ( isset( $aArray[ $_sKey ] ) ) { |
160
|
|
|
|
161
|
|
|
if ( empty( $aKeys ) ) { // no more keys |
162
|
|
|
return $aArray[ $_sKey ]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ( is_array( $aArray[ $_sKey ] ) ) { |
166
|
|
|
return self::getArrayValueByArrayKeys( $aArray[ $_sKey ], $aKeys, $vDefault ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// 3.5.3+ Fixes an issue that setting a non existent key resulted in null. |
170
|
|
|
// @deprecated |
171
|
|
|
// return $aArray[ $_sKey ]; |
|
|
|
|
172
|
|
|
|
173
|
|
|
// DEVVER+ When a too deep element that the subject array does not hold is searched, |
174
|
|
|
// it returns the default value. It used to return the value of the most upper dimension. |
175
|
|
|
return $vDefault; |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
return $vDefault; |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Casts array but does not create an empty element with the zero key when non-true value is given. |
184
|
|
|
* |
185
|
|
|
* @remark If `null` is passed an empty array `array()` will be returned. |
186
|
|
|
* @since 3.0.1 |
187
|
|
|
* @since 3.5.3 Added the `$bPreserveEmpty` parameter. |
188
|
|
|
* @since DVVER Moved from `AdminPageFramework_Utility_Array`. |
189
|
|
|
* @param mixed $mValue The subject value. |
190
|
|
|
* @param boolean bPreserveEmpty If `false` is given, a value that yields `false` such as `false`, an empty sttring `''`, or `0` will not create an element such as `array( false )`. It will be just `array()`. |
191
|
|
|
* @return array The cast array. |
192
|
|
|
*/ |
193
|
|
|
static public function getAsArray( $mValue, $bPreserveEmpty=false ) { |
|
|
|
|
194
|
|
|
|
195
|
|
|
if ( is_array( $mValue ) ) { |
196
|
|
|
return $mValue; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ( $bPreserveEmpty ) { |
200
|
|
|
return ( array ) $mValue; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if ( empty( $mValue ) ) { |
204
|
|
|
return array(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return ( array ) $mValue; |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Extracts array elements by giving keys. |
213
|
|
|
* |
214
|
|
|
* <h4>Example</h4> |
215
|
|
|
* <code> |
216
|
|
|
* $array = array( 'a' => 1, 'b' => 3, 'c' => 5 ); |
217
|
|
|
* $array = getArrayElementsByKeys( $array, array( 'a', 'c', ) ), |
218
|
|
|
* </code> |
219
|
|
|
* will produce |
220
|
|
|
* <code> |
221
|
|
|
* array( |
222
|
|
|
* 'a' => 1, |
223
|
|
|
* 'c' => 5, |
224
|
|
|
* ) |
225
|
|
|
* </code> |
226
|
|
|
* @since 3.5.4 |
227
|
|
|
* @since DVVER Moved from `AdminPageFramework_Utility_Array`. |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
|
|
static public function getArrayElementsByKeys( array $aSubject, array $aKeys ) { |
|
|
|
|
231
|
|
|
return array_intersect_key( |
232
|
|
|
$aSubject, |
233
|
|
|
array_flip( $aKeys ) |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.