开发者

What is the best way to map arrays and not allow future developers to mess it up?

开发者 https://www.devze.com 2023-02-07 00:37 出处:网络
Lets say I have an array array(\'VI\', \'MC\', \'AE\') and an array array(\'V\', \'M\', \'E\') I want to map the arrays so that later on if I get the data of VI I can use that to get V. I used the arr

Lets say I have an array array('VI', 'MC', 'AE') and an array array('V', 'M', 'E') I want to map the arrays so that later on if I get the data of VI I can use that to get V. I used the array_map function to do this. But what if someone later on goes in and changes the order of my first array or adds one to it and doesn't change the second, it could mess up the mapping. What is the best way to handle this to make sure there isn't any mistake in the future.

Here is an example of my code. One thing to note, I have hard coded the arrays but these are really grabbed from methods that are grabbing from configuration values, so yes it is different then just having both arrays defined right in the method

 private function _getMethodFromMageType($mageType){
    $origCCTypes =  array('VI', 'MC', 'AE');
    $newCCTypes = array('V', 'M', 'E');
    $typesMap = array_map(array(&$this, 'mapCCTypes'), $origCCTypes, $newCCTypes);

    foreach ($typesMap as $key => $map){
        if($map[$origType]){
            return $map[$origType];
        }
    }
}

Here is my call back function for the array_map function

private function mapCCTypes($ccTypes1, $ccTypes2){
    return (array($ccTypes1 => $ccTypes2));
}
开发者_StackOverflow社区


Only have one array which directly stores the mappings in a clear way:

$CCTypes = array('VI' => 'V', 'MC' => 'M', 'AE' => 'E');


Well, it's a bit ugly, but if you're concerned about it, you can make the code more resistant to tinkering with the order of the config lists by hardcoding the current, known mappings and using those in preference to an order-based mapping where available:

private $ccStaticMap = array(
    'VI' => 'V',
    'MC' => 'M',
    'AE' => 'E',
);

private function _getMethodFromMageType($origType) {
    $origCCTypes = pullOrigTypesFromConfig();
    $newCCTypes = pullNewTypesFromConfig();
    foreach($this->ccStaticMap as $key => $val) {
        while(($ix = array_search($key, $origCCTypes)) !== false)
            unset($origCCTypes[$ix]);
        while(($ix = array_search($val, $newCCTypes)) !== false)
            unset($newCCTypes[$ix]);
    }
    $typesMap = $this->ccStaticMap;
    for($ix = 0; $ix < min(count($origCCTypes), count($newCCTypes)); $ix++)
        $typesMap[$origCCTypes[$ix]] = $newCCTypes[$ix];
    return isset($typesMap[$origType]) ? $typesMap[$origType] : null;
}


a) Why don't you array_combine? That's simpler than your array_map approach, if I correctly understood what it does.

b) Why don't you use an associative array right away?


I'm not sure if you can avoid that if your mappings come from configuration files. You can always mess up configuration files (and then you will have to save the original mappings somewhere to see if it has been messed up). Maybe you need to improve your configuration parsing or declaration process so that you always have to declare some kind of associative array e.g. in an INI file:

mappings.VI = V
mappings.MC = M
mappings.AE = E

$data = parse_ini_file("file.ini");
$mappings = $data['mappings'];
0

精彩评论

暂无评论...
验证码 换一张
取 消