Created
February 6, 2009 20:26
-
-
Save sasezaki/59590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//http://d.hatena.ne.jp/technohippy/20081121#1227285803 | |
class Acme_ArrayOrdinalAccesser implements ArrayAccess { | |
public function __construct(array $array) { | |
foreach($array as $k => $v ) $this->offsetSet($k, $v); | |
} | |
public function __set($offset, $value) { | |
$this->offsetSet($offset, $value); | |
} | |
public function __get($offset) { | |
return $this->offsetGet($offset); | |
} | |
public function offsetExists( $offset ) { | |
return isset( $this->$offset ); | |
} | |
public function offsetSet( $offset, $value) | |
{ | |
if (!is_null($k = $this->map($offset))) { | |
$this[$k] = $value; | |
} else { | |
$this->$offset = $value; | |
} | |
} | |
public function offsetGet( $offset ) | |
{ | |
if (!is_null($k = $this->map($offset))) { | |
return $this[$k]; | |
} else { | |
return $this->$offset; | |
} | |
} | |
public function offsetUnset( $offset ) { | |
unset( $this->$offset ); | |
} | |
private function map($offset) { | |
$LESS_THAN_20 = array( | |
'zero', 'one', 'two', 'three', 'four', | |
'five', 'six', 'seven', 'eight', 'nine', | |
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', | |
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' | |
); | |
$DOUBLE_FIGURES = array( | |
null, 'ten', 'twenty', 'thirty', 'forty', | |
'fifty', 'sixty', 'seventy', 'eighty', 'ninety' | |
); | |
$key20 = array_flip($LESS_THAN_20); | |
if (array_key_exists($offset, $key20)) { | |
return $key20[$offset] - 1; | |
} else { | |
return null; | |
} | |
} | |
} | |
if (debug_backtrace()) {return;} | |
$array = new Acme_ArrayOrdinalAccesser(range(1,10)); | |
//print_r($array); | |
$array->nine = 'NINE'; | |
print_r((array)$array); | |
/** | |
Array | |
( | |
[0] => 1 | |
[1] => 2 | |
[2] => 3 | |
[3] => 4 | |
[4] => 5 | |
[5] => 6 | |
[6] => 7 | |
[7] => 8 | |
[8] => NINE | |
[9] => 10 | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment