AllowMultiple.php
4 years ago
HasDefaultValue.php
4 years ago
HasEmailTag.php
4 years ago
HasHelpText.php
4 years ago
HasLabel.php
4 years ago
HasMaxLength.php
4 years ago
HasMinLength.php
4 years ago
HasName.php
4 years ago
HasNodes.php
4 years ago
HasOptions.php
4 years ago
HasPlaceholder.php
4 years ago
HasType.php
4 years ago
HasVisibilityConditions.php
4 years ago
InsertNode.php
4 years ago
IsReadOnly.php
4 years ago
IsRequired.php
4 years ago
MoveNode.php
4 years ago
MoveNodeProxy.php
4 years ago
NameCollision.php
4 years ago
RemoveNode.php
4 years ago
SerializeAsJson.php
4 years ago
ShowInReceipt.php
4 years ago
StoreAsMeta.php
4 years ago
ValidationRules.php
4 years ago
WalkNodes.php
4 years ago
InsertNode.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI\Concerns; |
| 4 | |
| 5 | use Give\Framework\FieldsAPI\Contracts\Node; |
| 6 | use Give\Framework\FieldsAPI\Contracts\Collection; |
| 7 | use Give\Framework\FieldsAPI\Exceptions\ReferenceNodeNotFoundException; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.10.2 |
| 11 | */ |
| 12 | trait InsertNode { |
| 13 | |
| 14 | /** |
| 15 | * @since 2.10.2 |
| 16 | * |
| 17 | * @param string $siblingName |
| 18 | * @param Node $node |
| 19 | * |
| 20 | * @return $this |
| 21 | * @throws ReferenceNodeNotFoundException |
| 22 | */ |
| 23 | public function insertAfter( $siblingName, Node $node ) { |
| 24 | $this->checkNameCollisionDeep( $node ); |
| 25 | $this->insertAfterRecursive( $siblingName, $node ); |
| 26 | return $this; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @since 2.10.2 |
| 31 | * |
| 32 | * @param string $siblingName |
| 33 | * @param Node $node |
| 34 | * |
| 35 | * @throws ReferenceNodeNotFoundException |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | protected function insertAfterRecursive( $siblingName, Node $node ) { |
| 40 | $siblingIndex = $this->getNodeIndexByName( $siblingName ); |
| 41 | if ( false !== $siblingIndex ) { |
| 42 | $this->insertAtIndex( |
| 43 | $siblingIndex + 1, |
| 44 | $node |
| 45 | ); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if ( $this->nodes ) { |
| 50 | foreach ( $this->nodes as $childNode ) { |
| 51 | if ( $childNode instanceof Collection ) { |
| 52 | $childNode->insertAfter( $siblingName, $node ); |
| 53 | } |
| 54 | } |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | throw new ReferenceNodeNotFoundException( $siblingName ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @since 2.10.2 |
| 63 | * |
| 64 | * @param string $siblingName |
| 65 | * @param Node $node |
| 66 | * |
| 67 | * @throws ReferenceNodeNotFoundException |
| 68 | * |
| 69 | * @return $this |
| 70 | */ |
| 71 | public function insertBefore( $siblingName, Node $node ) { |
| 72 | $this->checkNameCollisionDeep( $node ); |
| 73 | $this->insertBeforeRecursive( $siblingName, $node ); |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @since 2.10.2 |
| 79 | * |
| 80 | * @param string $siblingName |
| 81 | * @param Node $node |
| 82 | * |
| 83 | * @throws ReferenceNodeNotFoundException |
| 84 | * |
| 85 | * @return void |
| 86 | */ |
| 87 | protected function insertBeforeRecursive( $siblingName, Node $node ) { |
| 88 | $siblingIndex = $this->getNodeIndexByName( $siblingName ); |
| 89 | if ( false !== $siblingIndex ) { |
| 90 | $this->insertAtIndex( |
| 91 | $siblingIndex - 1, |
| 92 | $node |
| 93 | ); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if ( $this->nodes ) { |
| 98 | foreach ( $this->nodes as $childNode ) { |
| 99 | if ( $childNode instanceof Collection ) { |
| 100 | $childNode->insertBefore( $siblingName, $node ); |
| 101 | } |
| 102 | } |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | throw new ReferenceNodeNotFoundException( $siblingName ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @since 2.10.2 |
| 111 | */ |
| 112 | protected function insertAtIndex( $index, $node ) { |
| 113 | $this->checkNameCollisionDeep( $node ); |
| 114 | array_splice( $this->nodes, $index, 0, [ $node ] ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritdoc} |
| 119 | */ |
| 120 | public function append( Node ...$nodes ) { |
| 121 | foreach ( $nodes as $node ) { |
| 122 | $this->insertAtIndex( $this->count(), $node ); |
| 123 | } |
| 124 | return $this; |
| 125 | } |
| 126 | } |
| 127 |