PHP 关联数组:独特的函数值
PHP associative array: unique function to value
所以由于一些讨厌的框架问题,我有点卡在这里。基本上,我从用户输入中获取一个字符串,并将其替换为我的模型中仅在调用时已知的值。
我的结构是这样的:
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;
}
所以这会产生一些非常难看的代码。我的老板想编辑它,使一个函数成为数组的一部分,分配给每个条目,这将 运行 filter/edit 代码。像
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;
}
总而言之,这对我来说是一个非常有趣的问题,我正试图找到一种方法来进一步清理它。将您的答案标记为正确,因为它极大地帮助了我们。
如果我答对了问题,我会选择 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;
}
所以由于一些讨厌的框架问题,我有点卡在这里。基本上,我从用户输入中获取一个字符串,并将其替换为我的模型中仅在调用时已知的值。
我的结构是这样的:
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;
}
所以这会产生一些非常难看的代码。我的老板想编辑它,使一个函数成为数组的一部分,分配给每个条目,这将 运行 filter/edit 代码。像
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;
}
总而言之,这对我来说是一个非常有趣的问题,我正试图找到一种方法来进一步清理它。将您的答案标记为正确,因为它极大地帮助了我们。
如果我答对了问题,我会选择 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;
}