/** * TODO: Move this logic to the DocumentManager * {@inheritDoc} */ public function orderAt($uuid, $position, $userId, $webspaceKey, $locale) { $document = $this->documentManager->find($uuid, $locale); $parentDocument = $this->inspector->getParent($document); $siblingDocuments = $this->inspector->getChildren($parentDocument); $siblings = array_values($siblingDocuments->toArray()); // get indexed array $countSiblings = count($siblings); $currentPosition = array_search($document, $siblings) + 1; if ($countSiblings < $position || $position <= 0) { throw new InvalidOrderPositionException(sprintf('Cannot order node "%s" at out-of-range position "%s", must be >= 0 && < %d"', $this->inspector->getPath($document), $position, $countSiblings)); } if ($position === $countSiblings) { // move to the end $this->documentManager->reorder($document, null); } else { if ($currentPosition < $position) { $targetSibling = $siblings[$position]; } elseif ($currentPosition > $position) { $targetSibling = $siblings[$position - 1]; } $this->documentManager->reorder($document, $targetSibling->getPath()); } $this->documentManager->persist($document, $locale); $this->documentManager->flush(); return $this->documentToStructure($document); }