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