PHP关联数组:值

时间:2015-12-15 20:22:00

标签: php associative-array

因此,由于一些令人讨厌的框架问题,我有点卡在这里。基本上,我从用户输入中获取一个字符串,并将其替换为我的模型中仅在调用时知道的值。

我的结构是这样的:

static public $placeholders = array("[REPLACE_STRING_1]"=>'firstName',
                                    "[REPLACE_STRING_2]"=>'DateTime->format(\'aa,bb\')');
//Problem line above.  Very messy, format is an arm long and painful

public function myfunction(ModelClass $master) {
    $body = $this->sampleService->get('samplebody'); // populate body from DB
    foreach(static::$placeholders as $key => $value){
        $body = str_replace($key, $master->value, $body);
    }
    return $body;
}

所以这会产生一些非常难看的代码。我的老板想要编辑它以使函数成为数组的一部分,分配给每个条目,运行以过滤/编辑代码。像

这样的东西
function dateFormat($prop){
    return $prop->format('aa,bb');
}
function asIs($prop){
    return $prop;
}

static public $placeholders = array("[REPLACE_STRING_1]"=>['firstName', asIs]
                                    "[REPLACE_STRING_2]"=>['DateTime', dateFormat]);

PHP中是否有任何现有的结构或功能可以实现这一点,或者他的代码只是一个白痴?

编辑:我找到了一个非常类似于下面发布的答案的解决方案,但需要进行一些修改以传递变量。

function dateFormat ($prop, $master) {
    return $master->$prop->format('aabb');
}
function asIs ($prop, $master) {
    return $appointment->$prop;
}
static public $placeholders = array("[REPLACE_STRING_1]"=>['firstname','asIs'], 
                                    "[REPLACE_STRING_2]"=>['DateTime', dateFormat];

//instatiate service to get sampleService values

//main difference here
public function buildBody(ModelClass $master) {
    $body = $this->sampleService->get('samplebody');
    foreach(static::$placeholders as $key => $value){
        $body = preg_replace_callback('/'.preg_quote($key).'/',
        //Use closure here to pass in $master, otherwise laravel gets angry about strings instead of objects
        function ($matches) use ($master) {
            $placeholder = static::placeholders[$matches[0]];
            $func = $placeholder[1];
            $prop = $placeholder[0];
            return call_user_func(array($this, $func), $prop, $appointment);
        }, $body);
    }
    return $body;
}

总而言之,这对我来说是一个非常有趣的问题,而我正试图找到一种方法来进一步清理它。要将你的答案标记为正确,因为它非常有助于到达这里。

1 个答案:

答案 0 :(得分:0)

如果我的问题是正确的,我会选择preg_replace_callback,并且有点像这样:

function dateFormat($prop){
    return $prop;
}

function asIs($prop){
    return $prop;
}

static public $placeholders = array(
    "[REPLACE_STRING_1]"=>['firstName', 'asIs'],
    "[REPLACE_STRING_2]"=>['DateTime', 'dateFormat']
);

private function replace($matches) {
    $placeholder = static::$placeholders[$matches[0]];
    $func = $placeholder[1];
    $prop = $placeholder[0];

    // this calls your class methods ('asIs', 'dateFormat')
    // with $prop = 'firstName' etc.
    return call_user_func(array($this, $func), $prop);
}

public function myfunction(ModelClass $master) {
    $body = $this->sampleService->get('samplebody');

    foreach(static::$placeholders as $key => $value) {
        // use a callback replace method instead of str_replace
        $body = preg_replace_callback('/' . preg_quote($key) . '/', array($this, 'replace'), $body);
    }

    return $body;
}