public function swapData($node_name1, $node_name2) { if (!$this->hasIdentifier($node_name1)) { throw new e\NodeException(S_SPAN . "The node {$node_name1} does not exists." . E_SPAN); } if (!$this->hasIdentifier($node_name2)) { throw new e\NodeException(S_SPAN . "The node {$node_name2} does not exists." . E_SPAN); } // must be siblings if (StructuredDataNode::removeLastIndex($node_name1) != StructuredDataNode::removeLastIndex($node_name2)) { throw new e\NodeException(S_SPAN . "The nodes {$node_name1} and {$node_name2} are not siblings." . E_SPAN); } $par_id = $this->node_map[$node_name1]->getParentId(); // get the data $node1_data = $this->node_map[$node_name1]->toStdClass(); $node2_data = $this->node_map[$node_name2]->toStdClass(); if (self::DEBUG) { u\DebugUtility::out("Parent ID: {$par_id}" . BR . "Node 1: {$node_name1}" . BR . "Node 2: {$node_name2}"); } if ($par_id != '') { $siblings = $this->node_map[$par_id]->getChildren(); } else { $siblings = $this->children; } $sibling_count = count($siblings); if (self::DEBUG) { u\DebugUtility::out("Sibling count: {$sibling_count}"); } for ($i = 0; $i < $sibling_count; $i++) { if (self::DEBUG) { u\DebugUtility::out($siblings[$i]->getIdentifier()); } // find the two positions if ($siblings[$i]->getIdentifier() == $node_name1) { $node_pos1 = $i; if (self::DEBUG) { u\DebugUtility::out("Node 1 position: {$node_pos1}"); } } if ($siblings[$i]->getIdentifier() == $node_name2) { $node_pos2 = $i; if (self::DEBUG) { u\DebugUtility::out("Node 2 position: {$node_pos2}" . BR); } } } // create new nodes $new_node1 = new StructuredDataNode($node2_data, NULL, $this->data_definition, $node_pos1, $par_id . structuredDataNode::DELIMITER); $new_node2 = new StructuredDataNode($node1_data, NULL, $this->data_definition, $node_pos2, $par_id . structuredDataNode::DELIMITER); // insert new nodes // must assign new nodes to the original arrays, not $siblings if ($par_id != '') { $this->node_map[$par_id]->swapChildren($node_pos1, $new_node1, $node_pos2, $new_node2); } else { $this->children[$node_pos1] = $new_node1; $this->children[$node_pos2] = $new_node2; } $this->node_map[$node_name1] = $new_node1; $this->node_map[$node_name2] = $new_node2; return $this; }