37 if (
null !== $elements) {
51 $this->_elements = $elements;
52 foreach ($elements as $elementId =>
$element) {
53 if (is_numeric($elementId)) {
55 new \
Magento\Framework\
Phrase(
"Element ID must not be numeric: '%1'.", [$elementId])
61 $this->_assertArray($groups);
62 foreach ($groups as $groupName =>
$group) {
63 $this->_assertArray(
$group);
67 '"%2" is an invalid format of "%1" group. Verify the format and try again.',
68 [$groupName, var_export(
$group, 1)]
72 foreach (
$group as $groupElementId) {
73 $this->_assertElementExists($groupElementId);
89 $element = $this->_elements[$elementId];
94 $this->_assertElementExists($parentId);
95 if (empty($this->_elements[$parentId][self::CHILDREN][$elementId])) {
98 'The "%1" is not in the nested set of "%2", causing the parent-child relation to break. ' 99 .
'Verify and try again.',
100 [$elementId, $parentId]
107 if (isset(
$element[self::CHILDREN])) {
113 'The "%1" format of children is invalid. Verify and try again.',
118 foreach (array_keys(
$children) as $childId) {
119 $this->_assertElementExists($childId);
121 $this->_elements[$childId][self::PARENT]
122 ) || $elementId !== $this->_elements[$childId][self::PARENT]
126 'The "%1" doesn\'t have "%2" as parent, causing the parent-child relation to break. ' 127 .
'Verify and try again.',
128 [$childId, $elementId]
156 if (isset($this->_elements[$elementId])) {
158 new \
Magento\Framework\
Phrase(
'An element with a "%1" ID already exists.', [$elementId])
161 $this->_elements[$elementId] = [];
175 return $this->_elements[$elementId] ??
false;
186 return isset($this->_elements[$elementId]);
201 if (isset($this->_elements[$elementId][self::CHILDREN])) {
202 foreach (array_keys($this->_elements[$elementId][self::CHILDREN]) as $childId) {
203 $this->_assertElementExists($childId);
207 unset($this->_elements[$childId][self::PARENT]);
212 $wasFound = isset($this->_elements[$elementId]);
213 unset($this->_elements[$elementId]);
228 $this->_assertElementExists($elementId);
234 throw new \InvalidArgumentException(
"The '{$attribute}' attribute is reserved and can't be set.");
250 $this->_assertElementExists($elementId);
251 if (isset($this->_elements[$elementId][
$attribute])) {
252 return $this->_elements[$elementId][
$attribute];
267 $this->_assertElementExists($oldId);
268 if (!$newId || isset($this->_elements[$newId])) {
270 new \
Magento\Framework\
Phrase(
'An element with a "%1" ID is already defined.', [$newId])
275 $this->_elements[$newId] = $this->_elements[$oldId];
278 if (isset($this->_elements[$oldId][self::CHILDREN])) {
279 foreach (array_keys($this->_elements[$oldId][self::CHILDREN]) as $childId) {
280 $this->_assertElementExists($childId);
286 if (isset($this->_elements[$oldId][self::PARENT]) && ($parentId = $this->_elements[$oldId][self::PARENT])) {
289 unset($this->_elements[$parentId][self::CHILDREN][$oldId]);
293 unset($this->_elements[$oldId]);
310 if ($elementId == $parentId) {
313 'The "%1" was incorrectly set as a child to itself. Resolve the issue and try again.',
318 if ($this->_isParentRecursively($elementId, $parentId)) {
321 'The "%3" cannot be set as child to "%1" because "%1" is a parent of "%2" recursively. ' 322 .
'Resolve the issue and try again.',
323 [$elementId, $parentId, $elementId]
328 unset($this->_elements[$parentId][self::CHILDREN][$elementId]);
347 $childId = $elementId;
352 unset($this->_elements[$childId][self::PARENT]);
354 unset($this->_elements[$parentId][self::CHILDREN][$childId]);
355 if (empty($this->_elements[$parentId][self::CHILDREN])) {
356 unset($this->_elements[$parentId][self::CHILDREN]);
379 if ($position >= $currentOffset + 1) {
382 }
elseif ($position < 0) {
383 if ($position < $currentOffset + 1 - count($this->_elements[$parentId][self::CHILDREN])) {
384 if ($position === -1) {
391 $this->
unsetChild($childId)->_insertChild($parentId, $childId, $offset,
$alias);
414 if ($childId === $siblingId) {
415 $newOffset = $this->_getRelativeOffset($parentId, $siblingId, $offset);
416 return $this->
reorderChild($parentId, $childId, $newOffset);
419 $newOffset = $this->
unsetChild($childId)->_getRelativeOffset($parentId, $siblingId, $offset);
432 private function _getRelativeOffset($parentId, $siblingId, $delta)
438 if ($newOffset < 0) {
453 if (isset($this->_elements[$parentId][self::CHILDREN])) {
454 return array_search(
$alias, $this->_elements[$parentId][self::CHILDREN]);
480 return $this->_elements[$childId][
self::PARENT] ??
false;
492 if (isset($this->_elements[$parentId][self::CHILDREN][$childId])) {
509 $this->_assertElementExists($parentId);
510 $this->_elements[$parentId][
self::GROUPS][$groupName][$childId] = $childId;
530 if (isset($this->_elements[$parentId][self::GROUPS][$groupName])) {
531 foreach ($this->_elements[$parentId][self::GROUPS][$groupName] as $childId) {
532 if (isset($this->_elements[$parentId][self::CHILDREN][$childId])) {
554 'The "%1" is not a child of "%2". Resolve the issue and try again.',
555 [$childId, $parentId]
569 private function _isParentRecursively($childId, $potentialParentId)
571 $parentId = $this->
getParentId($potentialParentId);
575 if ($parentId === $childId) {
578 return $this->_isParentRecursively($childId, $parentId);
605 $this->_assertElementExists($elementId);
606 if (!empty($this->_elements[$elementId][self::PARENT])) {
609 'The element "%1" can\'t have a parent because "%2" is already the parent of "%1".',
610 [$elementId, $this->_elements[$elementId][self::PARENT]]
614 $this->_assertElementExists($targetParentId);
619 'The element "%1" is already a child of "%2".',
620 [$elementId, $targetParentId]
627 'The element "%1" can\'t have a child because "%1" already has a child with alias "%2".',
634 if (
null === $offset) {
642 $this->_elements[$elementId][
self::PARENT] = $targetParentId;
652 private function _assertElementExists($elementId)
654 if (!isset($this->_elements[$elementId])) {
655 throw new \OutOfBoundsException(
656 'The element with the "' . $elementId .
'" ID wasn\'t found. Verify the ID and try again.' 668 private function _assertArray(
$value)
671 throw new LocalizedException(
672 new \
Magento\Framework\Phrase(
"An array expected: %1", [var_export(
$value, 1)])
getGroupChildNames($parentId, $groupName)
getChildAlias($parentId, $childId)
setAttribute($elementId, $attribute, $value)
elseif(isset( $params[ 'redirect_parent']))
reorderToSibling($parentId, $childId, $siblingId, $offset)
__construct(array $elements=null)
getChildId($parentId, $alias)
unsetChild($elementId, $alias=null)
getAttribute($elementId, $attribute)
addToParentGroup($childId, $groupName)
reorderChild($parentId, $childId, $position)
_assertParentRelation($elementId)
unsetElement($elementId, $recursive=true)
setAsChild($elementId, $parentId, $alias='', $position=null)
_getChildOffset($parentId, $childId)
createElement($elementId, array $data)
importElements(array $elements)
renameElement($oldId, $newId)
_insertChild($targetParentId, $elementId, $offset, $alias)