-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssociativeArray.phpclass
More file actions
executable file
·431 lines (317 loc) · 13.7 KB
/
AssociativeArray.phpclass
File metadata and controls
executable file
·431 lines (317 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/**************************************************************************************************************
NAME
AssociativeArray.phpclass
DESCRIPTION
Implements a case_insensitive associative array.
AUTHOR
Christian Vigh, 11/2014.
HISTORY
[Version : 1.0] [Date : 2014/11/02] [Author : CV]
Initial version.
[Version : 1.0.1] [Date : 2014/11/09] [Author : CV]
. Added a default value of empty array to the class constructor.
[Version : 1.0.2] [Date : 2014/12/04] [Author : CV]
. Added the ksort() method.
[Version : 1.0.3] [Date : 2015/04/08] [Author : CV]
. Added the pop() method.
[Version : 1.0.3] [Date : 2015/04/08] [Author : CV]
. Added the in_array(), iin_array(), in_subarray() and iin_subarray() methods.
[Version : 1.0.4] [Date : 2015/05/13] [Author : CV]
. Added the keyname() method.
[Version : 1.0.5] [Date : 2015/06/13] [Author : CV]
. Added the ReadOnly boolean property, which prevents further values to be added once the array has
been initialized.
[Version : 1.0.6] [Date : 2015/07/20] [Author : CV]
. Changed the offsetSet() method which uselessly called __rebuild_keys() when an existing array
item was assigned a value.
[Version : 2.0] [Date : 2015/10/03] [Author : CV]
. Completely restructured the stuff since it was developed "à la va-vite" to address an urgent need :
- Renamed several internal properties
- Removed the useless ReadOnly property : the original goal was not to enhance array management, but
simply give associative array case-insensitiveness on their keys without affecting too much
performance, so ReadOnly was useless in that scope.
- To access array elements using case-insensitive keys, a separate array (now named "ArrayKeys") is
managed ; the new version only rebuilds it when needed (for example, when several new elements have
been added THEN the value of one of them is retrieved).
[Version : 2.0.1] [Date : 2015/12/28] [Author : CV]
. __rebuild_keys() is now conditionnally called instead of being systematically invoked. This saves
useless function calls.
. The offsetSet() and offsetUnset() methods now directly update the $DataKeys member instead of setting
the $RebuildKeys flag saying that the next array access function should rebuild the keys first.
**************************************************************************************************************/
/*==============================================================================================================
AssociativeArray -
A class for handling associative arrays whose keys are case-insensitive.
==============================================================================================================*/
class AssociativeArray //extends Object
implements \ArrayAccess, \Countable, \IteratorAggregate
{
// Associative array whose items are to be accessed in a case-insensitive way
protected $Data ;
// Lowercase version of associative array keys. Values are the case-sensitive original key.
protected $DataKeys ;
// Set to true when $Datakeys needs to be rebuilt upon next read access
protected $RebuildKeys = false ;
/*==============================================================================================================
NAME
Constructor
PROTOTYPE
$aarray = new AssociativeArray ( $array = [] ) ;
DESCRIPTION
Builds an AssociativeArray object based on the supplied array argument.
PARAMETERS
$array (array or object) -
Either an array or an object implementing at least the ArrayAccess interface.
==============================================================================================================*/
public function __construct ( $array = [] )
{
$this -> Data = $array ;
$this -> RebuildKeys = true ;
$this -> __rebuild_keys ( ) ;
}
/*==============================================================================================================
__rebuild_keys -
Rebuilds the list of lowercase array keys after a modification has occurred.
==============================================================================================================*/
private function __rebuild_keys ( )
{
if ( $this -> RebuildKeys )
{
$keys = [] ;
foreach ( array_keys ( $this -> Data ) as $key )
$keys [ strtolower ( $key ) ] = $key ;
$this -> DataKeys = $keys ;
$this -> RebuildKeys = false ;
}
}
/*==============================================================================================================
__get, __set -
Allow access to array items as object properties.
==============================================================================================================*/
public function __get ( $member )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
$lcmember = strtolower ( $member ) ;
if ( isset ( $this -> DataKeys [ $lcmember ] ) )
return ( $this -> Data [ $this -> DataKeys [ $lcmember ] ] ) ;
else
throw ( new \Exception ( "Undefined property " . get_called_class ( ) . "::$member." ) ) ;
}
public function __set ( $member, $value )
{
$lcmember = strtolower ( $member ) ;
if ( isset ( $this -> DataKeys [ $member ] ) )
{
$this -> Data [ $this -> DataKeys [ $lcmember ] ] = $value ;
$this -> RebuildKeys = true ;
}
else
throw ( new \Exception ( "Undefined property " . get_called_class ( ) . "::$member." ) ) ;
}
/*==============================================================================================================
ensure_exists -
Ensures that, within this array, all the keys specified by the $required_keys array exist. If not, tries
to initialize them with one of the keys specified in the $default_keys array.
Returns the list of missing keys or an empty array.
==============================================================================================================*/
public function ensure_exists ( $required_keys, $default_keys )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
$missing_keys = [] ;
foreach ( $required_keys as $required_key )
{
$required_lc_key = strtolower ( $required_key ) ;
if ( ! isset ( $this -> DataKeys [ $required_lc_key ] ) )
{
$found = false ;
foreach ( $default_keys as $default_key )
{
$default_lc_key = strtolower ( $default_key ) ;
if ( isset ( $this -> DataKeys [ $default_lc_key ] ) )
{
$this [ $required_key ] = $this [ $default_key ] ;
$found = true ;
break ;
}
}
if ( ! $found )
$missing_keys [] = $required_key ;
}
}
return ( $missing_keys ) ;
}
/*==============================================================================================================
keyname -
Returns the name of the key associated to the specified numeric index.
==============================================================================================================*/
public function keyname ( $index )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
if ( is_numeric ( $index ) && $index >= 0 && $index < count ( $this -> Data ) )
{
$array = array_keys ( $this -> Data ) ;
return ( $array [ $index ] ) ;
}
else
return ( false ) ;
}
/*==============================================================================================================
in_array, iin_array, in_subarray, iin_subarray -
Checks if the specified element is in this array. iin_array() is the case-insensitive version.
in_subarray() and iin_subarray() search subitems referenced by $subitem and return the array index of
the found subitem, or false.
==============================================================================================================*/
public function in_array ( $value )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
return ( in_array ( $value, $this -> Data ) ) ;
}
public function iin_array ( $value )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
foreach ( $this -> Data as $item )
{
if ( is_scalar ( $item ) && ! strcasecmp ( $item, $value ) )
return ( true ) ;
}
return ( false ) ;
}
public function in_subarray ( $key, $value )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
foreach ( $this -> Data as $index => $item )
{
if ( isset ( $item [ $key ] ) && is_array ( $item [ $key ] ) )
{
if ( in_array ( $value, $item [ $key ] ) )
return ( $index ) ;
}
}
return ( false ) ;
}
public function iin_subarray ( $key, $value )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
$index = 0 ;
foreach ( $this -> Data as $index => $item )
{
if ( isset ( $item [ $key ] ) && is_array ( $item [ $key ] ) )
{
foreach ( $item [ $key ] as $subitem )
{
if ( is_scalar ( $subitem ) && ! strcasecmp ( $subitem, $value ) )
return ( $index ) ;
}
}
$index ++ ;
}
return ( false ) ;
}
/*==============================================================================================================
ksort -
Sorts the array by its keys.
==============================================================================================================*/
public function ksort ( )
{
ksort ( $this -> Data ) ;
}
/*==============================================================================================================
pop -
Pops the last element of the array.
==============================================================================================================*/
public function pop ( )
{
array_pop ( $this -> DataKeys ) ;
array_pop ( $this -> Data ) ;
}
/*==============================================================================================================
ToArray -
Returns the real array representation of the class.
==============================================================================================================*/
public function &ToArray ( )
{ return $this -> Data ; }
/*==============================================================================================================
Countable interface implementation.
==============================================================================================================*/
public function count ( )
{
return ( count ( $this -> Data ) ) ;
}
/*==============================================================================================================
IteratorAggregate interface implementation.
==============================================================================================================*/
public function getIterator ( )
{
return ( new \ArrayIterator ( $this -> Data ) ) ;
}
/*==============================================================================================================
ArrayAccess interface implementation.
==============================================================================================================*/
// offsetExists -
// Checks if the specified offset exists.
public function offsetExists ( $member )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
return ( isset ( $this -> DataKeys [ strtolower ( $member ) ] ) ) ;
}
// offsetGet -
// Return the value at the specified offset or key.
public function offsetGet ( $member )
{
if ( $this -> RebuildKeys )
$this -> __rebuild_keys ( ) ;
$lcmember = strtolower ( $member ) ;
if ( isset ( $this -> DataKeys [ $lcmember ] ) )
{
$index = $this -> DataKeys [ $lcmember ] ;
return ( $this -> Data [ $index ] ) ;
}
else
throw ( new \OutOfRangeException ( "Undefined offset $member." ) ) ;
}
// offsetSet -
// Sets a value at the specified offset.
public function offsetSet ( $member, $value )
{
$lcmember = strtolower ( $member ) ;
// $array [] = value construct : append the specified value to the existing array.
if ( $member === null )
{
$this -> Data [] = $value ;
$this -> RebuildKeys = true ;
}
// Assignment to an existing array key : preserve its original case
else if ( isset ( $this -> DataKeys [ $lcmember ] ) )
{
$index = $this -> DataKeys [ $lcmember ] ;
$this -> Data [ $index ] = $value ;
}
// Assignment to a new key
else
{
$this -> Data [ $member ] = $value ;
$this -> DataKeys [ strtolower ( $member ) ] = $member ;
}
}
// offsetUnset -
// Unsets the specified array key.
public function offsetUnset ( $member )
{
$lcmember = strtolower ( $member ) ;
if ( isset ( $this -> DataKeys [ $lcmember ] ) )
{
$index = $this -> DataKeys [ $lcmember ] ;
unset ( $this -> Data [ $index ] ) ;
unset ( $this -> DataKeys [ strtolower ( $index ) ] ) ;
}
}
}