Sulu\Component\Content\Mapper\ContentMapper::orderAt PHP Method

orderAt() public method

TODO: Move this logic to the DocumentManager {@inheritdoc}
public orderAt ( $uuid, $position, $userId, $webspaceKey, $locale )
    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->getUuid());
        }
        $this->documentManager->flush();
        return $this->documentToStructure($document);
    }

Usage Example

Exemplo n.º 1
0
 public function testOrderAtToLast()
 {
     $data = $this->prepareOrderAtData();
     $result = $this->mapper->orderAt($data[2]->getUuid(), 4, 1, 'sulu_io', 'en');
     $this->assertEquals($data[2]->getUuid(), $result->getUuid());
     $this->assertEquals('/page-1/page-1-2', $result->getPath());
     $this->assertEquals(1, $result->getChanger());
     $result = $this->mapper->loadByParent($data[0]->getUuid(), 'sulu_io', 'en');
     $this->assertEquals('/page-1/page-1-1', $result[0]->getPath());
     $this->assertEquals('/page-1/page-1-3', $result[1]->getPath());
     $this->assertEquals('/page-1/page-1-4', $result[2]->getPath());
     $this->assertEquals('/page-1/page-1-2', $result[3]->getPath());
 }