예제 #1
0
파일: Graph.php 프로젝트: qimus/graph
 /**
  * Поменять позиции местами
  *
  * @param int $parentId
  * @param int $nodeId1
  * @param int $nodeId2
  * @throws DatabaseException
  * @throws GraphException
  */
 public function swapPositions($parentId, $nodeId1, $nodeId2)
 {
     $directChilds = $this->edgesGateway->findDirectChildren($parentId);
     if (empty($directChilds)) {
         throw new GraphException('Child nodes not found.');
     }
     $childNodeIds = ArrayHelper::getColumn($directChilds, 'end');
     if (!in_array($nodeId1, $childNodeIds) || !in_array($nodeId2, $childNodeIds)) {
         throw new GraphException('Specified node is not a child.');
     }
     $sourcePosition = ArrayHelper::first(ArrayHelper::find($directChilds, ['end' => $nodeId1]));
     $targetPosition = ArrayHelper::first(ArrayHelper::find($directChilds, ['end' => $nodeId2]));
     $tmpPos = $sourcePosition['pos'];
     $sourcePosition['pos'] = $targetPosition['pos'];
     $targetPosition['pos'] = $tmpPos;
     $this->edgesGateway->updatePositions([$sourcePosition, $targetPosition]);
 }