Created
June 6, 2017 11:08
-
-
Save devcem/710a7ca2ae009417db3c3bbe966cd96d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function applyUpdate($diff, $arr1, $arr2){ | |
$status = false; | |
if(count($diff['different']) > 0){ | |
foreach ($diff['different'] as $key => $value) { | |
$exec = '$arr1.'.$value.' = $arr2.'.$value.';'; | |
$exec = preg_replace("/\.(\d{1,3})/", "[$1]", $exec); | |
$exec = preg_replace('/\.(\w*)/', "['$1']", $exec); | |
eval($exec); | |
$status = true; | |
} | |
} | |
if(count($diff['missing_from_first']) > 0){ | |
foreach ($diff['missing_from_first'] as $key => $value) { | |
$exec = '$arr1.'.$value; | |
$exec = preg_replace('/\.(\d{1,3})/', '[$1]', $exec); | |
$split = explode('.', $exec); | |
$last = $split[count($split)-1]; | |
preg_match_all('/\[(\d*)\]/', $last, $index); | |
$join = str_replace($last, str_replace('['.$index[1][0].']', '', $last), $exec); | |
$exec = 'array_push('.$join.', $arr2.'.$value.');'; | |
$exec = preg_replace('/\.(\d{1,3})/', '[$1]', $exec); | |
$exec = preg_replace('/\.(\w*)/', "['$1']", $exec); | |
eval($exec); | |
$status = true; | |
} | |
} | |
if(count($diff['missing_from_second']) > 0){ | |
foreach ($diff['missing_from_second'] as $key => $value) { | |
$exec = '$arr1.'.$value; | |
$exec = preg_replace('/\.(\d{1,3})/', '[$1]', $exec); | |
$split = explode('.', $exec); | |
$last = $split[count($split)-1]; | |
preg_match_all('/\[(\d*)\]/', $last, $index); | |
$join = str_replace($last, str_replace('['.$index[1][0].']', '', $last), $exec); | |
$exec = 'array_splice('.$join.', '.$index[1][0].');'; | |
$exec = preg_replace('/\.(\w*)/', "['$1']", $exec); | |
eval($exec); | |
$status = true; | |
} | |
} | |
if($status){ | |
return $arr1; | |
}else{ | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var compare = function (a, b) { | |
var result = { | |
different: [], | |
missing_from_first: [], | |
missing_from_second: [] | |
}; | |
_.reduce(a, function (result, value, key) { | |
if (b.hasOwnProperty(key)) { | |
if (_.isEqual(value, b[key])) { | |
return result; | |
} else { | |
if (typeof (a[key]) != typeof ({}) || typeof (b[key]) != typeof ({})) { | |
//dead end. | |
result.different.push(key); | |
return result; | |
} else { | |
var deeper = compare(a[key], b[key]); | |
result.different = result.different.concat(_.map(deeper.different, (sub_path) => { | |
return key + "." + sub_path; | |
})); | |
result.missing_from_second = result.missing_from_second.concat(_.map(deeper.missing_from_second, (sub_path) => { | |
return key + "." + sub_path; | |
})); | |
result.missing_from_first = result.missing_from_first.concat(_.map(deeper.missing_from_first, (sub_path) => { | |
return key + "." + sub_path; | |
})); | |
return result; | |
} | |
} | |
} else { | |
result.missing_from_second.push(key); | |
return result; | |
} | |
}, result); | |
_.reduce(b, function (result, value, key) { | |
if (a.hasOwnProperty(key)) { | |
return result; | |
} else { | |
result.missing_from_first.push(key); | |
return result; | |
} | |
}, result); | |
return result; | |
} | |
function applyUpdate(arr1, arr2){ | |
diff = compare(arr1, arr2); | |
if(diff.different.length > 0){ | |
for(key in diff.different){ | |
value = diff.different[key]; | |
exec = "arr1." + value + " = arr2." + value + ";"; | |
exec = exec.replace(/\.(\d{1,3})/g,"[$1]"); | |
eval(exec); | |
} | |
} | |
if(diff.missing_from_first.length > 0){ | |
for(key in diff.missing_from_first){ | |
value = diff.missing_from_first[key]; | |
exec = "arr1." + value; | |
exec = exec.replace(/\.(\d{1,3})/g,"[$1]"); | |
split = exec.split('.'); | |
last = split[split.length-1]; | |
index = last.match(/[(\d{1,3})]/g); | |
join = exec.replace(last, last.replace("[" + index[0] + "]", "")); | |
exec = join + ".push(arr2." + value + ");"; | |
exec = exec.replace(/\.(\d{1,3})/g,"[$1]"); | |
eval(exec); | |
} | |
} | |
if(diff.missing_from_second.length > 0){ | |
for(key in diff.missing_from_second){ | |
value = diff.missing_from_second[key]; | |
exec = "arr1." + value; | |
exec = exec.replace(/\.(\d{1,3})/g,"[$1]"); | |
split = exec.split('.'); | |
last = split[split.length-1]; | |
index = last.match(/[(\d{1,3})]/g); | |
join = exec.replace(last, last.replace("[" + index[0] + "]", "")); | |
eval(join + '.splice(' + index + ', 1)'); | |
} | |
} | |
return arr1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment