如何以更简洁的方式格式化数组数据


How to format array data in more cleaner way?

我有这个方法:

private function formatCliendCardData($data) {
    $formatedData = array();
    $formatedData['first_name'] = trim($data['name']);
    $formatedData['last_name'] = trim($data['surname']);
    $formatedData['date_of_birth'] = $data['birth_year'] . '-' . sprintf("%02d", $data['birth_month_id']) . '-' . sprintf("%02d", $data['birth_day']); //[yyyy-mm-dd]
    $formatedData['sex'] = ($data['sex_id'] == 's1'? 'm' : 'f');
    $formatedData['county'] = 'Latvija'; //@TODO get real data
    $formatedData['post_index'] = (int) preg_replace( '/[^0-9]/', '', $data['post_index']);
    $formatedData['city'] = trim($data['city']);
    $formatedData['street'] = trim($data['street']);
    $formatedData['house_nr'] = trim($data['house_nr']);
    $formatedData['phone'] = trim($data['phone']);
    $formatedData['email'] = trim($data['email']);
    return $formatedData;
}

我经常遇到这个问题。我有一个大方法来重新格式化数据并返回。它看起来很丑。据我所知,还有一些其他的方法——只是进行foreach循环,但也有例外,所以我还是需要设置"if"。在你看来,什么是更好的方法来重新格式化这些数据?

我同意hello,

创建一个类来处理setter中所有的格式化和验证。

class Person
{
 public function setName($name)
 {
   $this->name = trim($name);
 }
 ...
}

,然后在函数中创建对象:

private function formatCliendCardData($data) {
    $person = new Person();
    $person->setName($data['name`])
    ...

如果你有高度自定义的条件来重新格式化一些数据,我认为你没有任何其他的解决方案,手动应用这个条件与一些if/else或其他自定义循环。

如果没有,你有一些统一的条件来重新格式化,你可以聚合这些并批量应用于你的数据。换句话说,在我看来,唯一的其他解决方案是:概括重新格式化操作的条件,并将其应用于您的数据。

这是面向对象的方法

恕我直言,在这种情况下,您必须减少并清理代码。我经常使用一个帮助函数,它允许我拆分数据和格式化规则。

// helper function (OUTSITE YOUR CLASS)
function waterfall($input=NULL,$fns=array()){
   $input = array_reduce($fns, function($result, $fn) {
     return $fn($result);
   }, $input);
   return $input;
}
// your formatting rules
$rules = array(
    'first_name' => array('trim'),
    'last_name' => array('trim'),
    'date_of_birth' => array(
        'f0' => function($x){
            return sprintf("%s-%02d-%02d",
                            $x['birth_year'],
                            $x['birth_month_id'],
                            $x['birth_day']
                   );
        },
        'trim'
    ),
    'sex' => array(
        'f0' => function($x){
            if($x['sex_id'] == 's1'){
                return 'm';
            }else{
                return 'f';
            }
        }
    ),
    'post_index' => array(
        'f0' => function($x){ 
                   return (int) preg_replace('/[^0-9]/', NULL, $x); 
                },
        'trim'
    ),
    'city' => array('trim'),
    'email' => array('strtolower','trim')
);
// your data
$data = array(
    'first_name' => '  Andrea',
    'last_name' => 'Ganduglia ',
    'date_of_birth' => array(
        'birth_year' => '1899',
        'birth_month_id' => 5,
        'birth_day' => 7
    ),
    'post_index' => 'IT12100',
    'city' => 'Rome',
    'email' => 'USER@DOMAIN.tld '
);
// put all together
$formatedData = array();
foreach($data as $k => $v){
    $formatedData[$k] = waterfall($v,$rules[$k]);
}
// results
print_r($formatedData);
/*
Array
(
    [first_name] => Andrea
    [last_name] => Ganduglia
    [date_of_birth] => 1899-05-07
    [post_index] => 12100
    [city] => Rome
    [email] => user@domain.tld
)
*/