/** * Adds a new item to the layout * * @param string $id The layout item id * @param string $parentId The id or alias of parent item. Set null to add the root item * @param mixed $blockType The block type associated with the layout item * @param array $options The layout item options * @param string|null $siblingId The id or alias of an item which should be nearest neighbor * @param bool $prepend Determines whether the moving item should be located before or after * the specified sibling item * * @throws Exception\InvalidArgumentException if the id or parent id are empty or invalid * @throws Exception\ItemAlreadyExistsException if the layout item with the same id already exists * @throws Exception\ItemNotFoundException if the parent layout item does not exist * @throws Exception\LogicException if the layout item cannot be added by other reasons * * @SuppressWarnings(PHPMD.NPathComplexity) */ public function add($id, $parentId, $blockType, array $options = [], $siblingId = null, $prepend = false) { $this->validateId($id, true); if (isset($this->items[$id])) { throw new Exception\ItemAlreadyExistsException(sprintf('The "%s" item already exists.' . ' Remove existing item before add the new item with the same id.', $id)); } if (!$parentId && !$this->hierarchy->isEmpty()) { throw new Exception\LogicException(sprintf('The "%s" item cannot be the root item' . ' because another root item ("%s") already exists.', $id, $this->hierarchy->getRootId())); } if ($parentId) { $parentId = $this->validateAndResolveId($parentId); } if ($siblingId) { $siblingId = $this->resolveId($siblingId); if (!isset($this->items[$siblingId])) { throw new Exception\ItemNotFoundException(sprintf('The "%s" sibling item does not exist.', $siblingId)); } if ($parentId && $siblingId === $parentId) { throw new Exception\LogicException('The sibling item cannot be the same as the parent item.'); } } $path = $parentId ? $this->getProperty($parentId, self::PATH, true) : []; $this->hierarchy->add($path, $id, $siblingId, $prepend); $path[] = $id; $this->items[$id] = [self::PATH => $path, self::BLOCK_TYPE => $blockType, self::OPTIONS => $options]; }