Skip to content

Instantly share code, notes, and snippets.

@hugowetterberg
Created November 9, 2009 19:04
Show Gist options
  • Save hugowetterberg/230177 to your computer and use it in GitHub Desktop.
Save hugowetterberg/230177 to your computer and use it in GitHub Desktop.
<?php
$ops = array(
array('operation' => 'insert'),
array('operation' => 'update'),
array('operation' => 'update'),
array('operation' => 'update'),
);
$ops2 = array(
array('operation' => 'update'),
array('operation' => 'update'),
array('operation' => 'delete'),
);
var_dump(_conglomerateclient_compress_node_ops($ops));
var_dump(_conglomerateclient_compress_node_ops($ops2));
/**
* The task of this function is to remove unnecessary operations. Multiple
* consequetive updates can be reduced to just the last update. Updates
* preceding a delete can be skipped altogether.
*
* @param array $ops
* @return array
*/
function _conglomerateclient_compress_node_ops($ops) {
$last_op = FALSE;
for ($i=0; $i<count($ops); $i++) {
if ($ops[$i]['operation'] == 'delete' || $ops[$i]['operation'] == 'update') {
if ($last_op == 'update') {
$ops[$i-1] = NULL;
}
}
$last_op = $ops[$i]['operation'];
}
return $ops;
}
array(4) {
[0]=>
array(1) {
["operation"]=>
string(6) "insert"
}
[1]=>
NULL
[2]=>
NULL
[3]=>
array(1) {
["operation"]=>
string(6) "update"
}
}
array(3) {
[0]=>
NULL
[1]=>
NULL
[2]=>
array(1) {
["operation"]=>
string(6) "delete"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment