Skip to content
This repository has been archived by the owner on Jan 1, 2020. It is now read-only.

Commit

Permalink
minor code move and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
uwej711 committed Jan 20, 2014
1 parent ab99bb7 commit 0fd8d86
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
43 changes: 43 additions & 0 deletions src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php
Expand Up @@ -154,4 +154,47 @@ public function canonicalName($className)
{
return $className;
}

/**
* This sort method is used for sorting elements in the given array according the reference array
* which contains some of the array keys of the first array.
*
* The method makes sure that array elements without a key in the reference stay in the array with
* a nearly stable order (i.e. what was before the elements in reference stays before, what was after
* stays after, what is in is ordered as in reference.
*/
public function sort($array, $reference)
{
$headIdx = 0;
$tailIdx = 0;
$i = 0;
foreach($array as $element) {
$i++;
if (false === array_search($element, $reference)) {
if (0 == $tailIdx) {
$headIdx = $i;
}
} else {
$tailIdx = $i;
}
}

$toSort = array_splice($array, $headIdx);
$tail = array_splice($toSort, $tailIdx - $headIdx);

for ($i=1; $i < count($toSort); $i++) {
$tempIdx = (int)array_search($toSort[$i], $reference);
$temp = $toSort[$i];
$j = $i - 1;

while ($j >= 0 && (int)array_search($toSort[$j], $reference) > $tempIdx){
$toSort[$j + 1] = $toSort[$j];
$j--;
}

$toSort[$j+1] = $temp;
}

return array_merge($array, $toSort, $tail);
}
}
43 changes: 0 additions & 43 deletions src/Midgard/CreatePHP/Mapper/DoctrinePhpcrOdmMapper.php
Expand Up @@ -206,47 +206,4 @@ public function getNodeName(&$item, $key)
$item = PathHelper::getNodeName($item);
}

/**
* This sort method is used for sorting elements in the given array according the reference array
* which contains some of the array keys of the first array.
*
* The method makes sure that array elements without a key in the reference stay in the array with
* a nearly stable order (i.e. what was before the elements in reference stays before, what was after
* stays after, what is in is ordered as in reference.
*/
public function sort($array, $reference)
{
$headIdx = 0;
$tailIdx = 0;
$i = 0;
foreach($array as $element) {
$i++;
if (false === array_search($element, $reference)) {
if (0 == $tailIdx) {
$headIdx = $i;
}
} else {
$tailIdx = $i;
}
}

$toSort = array_splice($array, $headIdx);
$tail = array_splice($toSort, $tailIdx - $headIdx);

for ($i=1; $i < count($toSort); $i++) {
$tempIdx = (int)array_search($toSort[$i], $reference);
$temp = $toSort[$i];
$j = $i - 1;

while ($j >= 0 && (int)array_search($toSort[$j], $reference) > $tempIdx){
$toSort[$j + 1] = $toSort[$j];
$j--;
}

$toSort[$j+1] = $temp;
}

return array_merge($array, $toSort, $tail);
}

}
4 changes: 2 additions & 2 deletions src/Midgard/CreatePHP/RestService.php
Expand Up @@ -206,7 +206,7 @@ private function _storeData($new_values, EntityInterface $entity)
$expanded_name = $this->_expandPropertyName($rel, $entity);
if (array_key_exists($expanded_name, $new_values)) {
$expectedOrder = $new_values[$expanded_name];
array_walk($expectedOrder, array($this, 'walkChildrenNames'));
array_walk($expectedOrder, array($this, 'walkJsonLdDecode'));
$this->_mapper->orderChildren($entity, $node, $expectedOrder);
}
} elseif ($node instanceof PropertyInterface) {
Expand All @@ -229,7 +229,7 @@ private function _storeData($new_values, EntityInterface $entity)
return null;
}

public function walkChildrenNames(&$item, $key)
public function walkJsonLdDecode(&$item, $key)
{
$item = $this->jsonldDecode($item);
}
Expand Down

0 comments on commit 0fd8d86

Please sign in to comment.