Contract
5 years ago
Exception
5 years ago
InsertNode.php
5 years ago
MoveNode.php
5 years ago
MoveNodeProxy.php
5 years ago
NameCollision.php
5 years ago
RemoveNode.php
5 years ago
WalkNodes.php
5 years ago
InsertNode.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI\FieldCollection; |
| 4 | |
| 5 | use Give\Framework\FieldsAPI\FieldCollection\Contract\Node; |
| 6 | use Give\Framework\FieldsAPI\FieldCollection\Contract\GroupNode; |
| 7 | use Give\Framework\FieldsAPI\FieldCollection\Exception\ReferenceNodeNotFoundException; |
| 8 | |
| 9 | /** |
| 10 | * @unreleased |
| 11 | */ |
| 12 | trait InsertNode { |
| 13 | |
| 14 | /** |
| 15 | * @unreleased |
| 16 | * |
| 17 | * @param string $siblingName |
| 18 | * @param Node $node |
| 19 | * |
| 20 | * @return $this |
| 21 | */ |
| 22 | public function insertAfter( $siblingName, Node $node ) { |
| 23 | $this->checkNameCollisionDeep( $node ); |
| 24 | $this->insertAfterRecursive( $siblingName, $node ); |
| 25 | return $this; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @unreleased |
| 30 | * |
| 31 | * @param string $siblingName |
| 32 | * @param Node $node |
| 33 | * |
| 34 | * @throws ReferenceNodeNotFoundException |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | protected function insertAfterRecursive( $siblingName, Node $node ) { |
| 39 | $siblingIndex = $this->getNodeIndexByName( $siblingName ); |
| 40 | if ( false !== $siblingIndex ) { |
| 41 | $this->insertAtIndex( |
| 42 | $siblingIndex + 1, |
| 43 | $node |
| 44 | ); |
| 45 | return; |
| 46 | } elseif ( $this->nodes ) { |
| 47 | foreach ( $this->nodes as $childNode ) { |
| 48 | if ( $childNode instanceof GroupNode ) { |
| 49 | $childNode->insertAfterRecursive( $siblingName, $node ); |
| 50 | } |
| 51 | } |
| 52 | return; |
| 53 | } |
| 54 | throw new ReferenceNodeNotFoundException( $siblingName ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @unreleased |
| 59 | * |
| 60 | * @param string $siblingName |
| 61 | * @param Node $node |
| 62 | * |
| 63 | * @return $this |
| 64 | */ |
| 65 | public function insertBefore( $siblingName, Node $node ) { |
| 66 | $this->checkNameCollisionDeep( $node ); |
| 67 | $this->insertBeforeRecursive( $siblingName, $node ); |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @unreleased |
| 73 | * |
| 74 | * @param string $siblingName |
| 75 | * @param Node $node |
| 76 | * |
| 77 | * @throws ReferenceNodeNotFoundException |
| 78 | * |
| 79 | * @return void |
| 80 | */ |
| 81 | protected function insertBeforeRecursive( $siblingName, Node $node ) { |
| 82 | $siblingIndex = $this->getNodeIndexByName( $siblingName ); |
| 83 | if ( false !== $siblingIndex ) { |
| 84 | $this->insertAtIndex( |
| 85 | $siblingIndex - 1, |
| 86 | $node |
| 87 | ); |
| 88 | return; |
| 89 | } elseif ( $this->nodes ) { |
| 90 | foreach ( $this->nodes as $childNode ) { |
| 91 | if ( $childNode instanceof GroupNode ) { |
| 92 | $childNode->insertBeforeRecursive( $siblingName, $node ); |
| 93 | } |
| 94 | } |
| 95 | return; |
| 96 | } |
| 97 | throw new ReferenceNodeNotFoundException( $siblingName ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @unreleased |
| 102 | */ |
| 103 | protected function insertAtIndex( $index, $node ) { |
| 104 | array_splice( $this->nodes, $index, 0, [ $node ] ); |
| 105 | } |
| 106 | } |
| 107 |