Created
October 14, 2009 07:53
-
-
Save hatyuki/209876 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 // vim: ts=4 sts=4 sw=4 | |
class HashRename | |
{ | |
/*** | |
* hash_rename | |
* | |
* @params Hash $hash | |
* @params Hash $rule | |
*/ | |
function hash_rename (&$hash, $args) | |
{ | |
if ( !is_array($hash) && !is_array($args) ) { | |
trigger_error('argument must be hash(array)', E_USER_ERROR); | |
} | |
$new_hash = array( ); | |
foreach ($hash as $key => $val) { | |
$orig_key = $key; | |
if ( array_key_exists($key, $args) ) { | |
$key = $args[$key]; | |
} | |
if ( array_key_exists('__prepend', $args) ) { | |
$key = $args['__prepend'].$key; | |
} | |
if ( array_key_exists('__append', $args) ) { | |
$key = $key.$args['__append']; | |
} | |
if ( array_key_exists('__callback', $args) ) { | |
$key = call_user_func($args['__callback'], $key); | |
} | |
if ( array_key_exists('__strict', $args) && array_key_exists($key, $new_hash) ) { | |
trigger_error( | |
"duplicate result key [$key] from original key [$orig_key]", | |
E_USER_ERROR | |
); | |
} | |
$new_hash[$key] = $hash[$orig_key]; | |
} | |
$hash = $new_hash; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment