Sync: allow to access "deep" values from expressions

This commit is contained in:
Thomas Gelf 2015-12-02 15:59:31 +01:00
parent d37991627a
commit b190fd62e3
1 changed files with 21 additions and 20 deletions

View File

@ -72,32 +72,33 @@ class Sync
return $this->getDeepValue($val->$key, $keys); return $this->getDeepValue($val->$key, $keys);
} }
protected function getSpecificValue($row, $var)
{
if (strpos($var, '.') === false) {
if (! property_exists($row, $var)) {
return null;
}
return $row->$var;
} else {
$parts = explode('.', $var);
$main = array_shift($parts);
if (! is_object($row->$main)) {
die('Data is not nested, cannot access ...');
}
return $this->getDeepValue($row->$main, $parts);
}
}
protected function fillVariables($string, $row) protected function fillVariables($string, $row)
{ {
if (preg_match('/^\${([A-Za-z0-9\._-]+)}$/', $string, $m)) { if (preg_match('/^\${([A-Za-z0-9\._-]+)}$/', $string, $m)) {
$var = $m[1]; return $this->getSpecificValue($row, $m[1]);
if (strpos($var, '.') === false) {
if (! property_exists($row, $var)) {
return null;
}
$val = $row->$var;
} else {
$parts = explode('.', $var);
$main = array_shift($parts);
if (! is_object($row->$main)) {
die('Data is not nested, cannot access ...');
}
return $this->getDeepValue($row->$main, $parts);
}
return $val;
} }
$func = function ($match) use ($row) { $func = function ($match) use ($row) {
// TODO allow to access deep value also here return $this->getSpecificValue($row, $match[1]);
return $row->{$match[1]};
}; };
return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string); return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);