PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Framework / FieldsAPI / Concerns / InsertNode.php

InsertNode.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Framework/FieldsAPI/Concerns/InsertNode.php

153 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\NameCollisionException;
8 use Give\Framework\FieldsAPI\Exceptions\ReferenceNodeNotFoundException;
9
10 /**
11 * @since 2.10.2
12 */
13 trait InsertNode
14 {
15 /**
16 * @inheritDoc
17 *
18 * @since 2.10.2
19 *
20 * @throws ReferenceNodeNotFoundException|NameCollisionException
21 */
22 public function insertAfter(string $siblingName, Node $node): self
23 {
24 $this->checkNameCollisionDeep($node);
25 $this->insertAfterRecursive($siblingName, $node);
26
27 return $this;
28 }
29
30 /**
31 * @since 4.8.0 Updated to only fall through if no node was found
32 * @since 2.10.2
33 *
34 * @throws ReferenceNodeNotFoundException|NameCollisionException
35 */
36 protected function insertAfterRecursive(string $siblingName, Node $node): bool
37 {
38 $siblingIndex = $this->getNodeIndexByName($siblingName);
39 if (null !== $siblingIndex) {
40 $this->insert(
41 $node,
42 $siblingIndex + 1
43 );
44
45 return true;
46 }
47
48 if ($this->nodes) {
49 foreach ($this->nodes as $childNode) {
50 if ($childNode instanceof Collection) {
51 try {
52 $childNode->insertAfter($siblingName, $node);
53 return true; // Stop searching after successful insertion
54 } catch (ReferenceNodeNotFoundException $e) {
55 // Continue searching in other child nodes
56 continue;
57 }
58 }
59 }
60 }
61
62 // Only throw exception if this is the root call and no node was found
63 throw new ReferenceNodeNotFoundException($siblingName);
64 }
65
66 /**
67 * @inheritDoc
68 *
69 * @since 2.10.2
70 *
71 * @throws ReferenceNodeNotFoundException|NameCollisionException
72 */
73 public function insertBefore(string $siblingName, Node $node): self
74 {
75 $this->checkNameCollisionDeep($node);
76 $this->insertBeforeRecursive($siblingName, $node);
77
78 return $this;
79 }
80
81 /**
82 * @since 4.8.0 Updated to only fall through if no node was found
83 * @since 2.10.2
84 *
85 * @throws ReferenceNodeNotFoundException|NameCollisionException
86 */
87 protected function insertBeforeRecursive(string $siblingName, Node $node): bool
88 {
89 $siblingIndex = $this->getNodeIndexByName($siblingName);
90 if (null !== $siblingIndex) {
91 $this->insert(
92 $node,
93 $siblingIndex
94 );
95
96 return true;
97 }
98
99 if ($this->nodes) {
100 foreach ($this->nodes as $childNode) {
101 if ($childNode instanceof Collection) {
102 try {
103 $childNode->insertBefore($siblingName, $node);
104 return true; // Stop searching after successful insertion
105 } catch (ReferenceNodeNotFoundException $e) {
106 // Continue searching in other child nodes
107 continue;
108 }
109 }
110 }
111 }
112
113 // Only throw exception if this is the root call and no node was found
114 throw new ReferenceNodeNotFoundException($siblingName);
115 }
116
117 /**
118 * @since 4.17.0 Declare the nullable parameter explicitly.
119 * @since 2.24.0 Make index optional to avoid rebuilding array when appending
120 * @since 2.10.2
121 *
122 * @param int|null $index appends to end if null
123 *
124 * @throws NameCollisionException
125 */
126 protected function insert(Node $node, ?int $index = null)
127 {
128 $this->checkNameCollisionDeep($node);
129
130 if ($index === null) {
131 $this->nodes[] = $node;
132 } else {
133 array_splice($this->nodes, $index, 0, [$node]);
134 }
135 }
136
137 /**
138 * @inheritdoc
139 *
140 * @since 2.10.2
141 *
142 * @throws NameCollisionException
143 */
144 public function append(Node ...$nodes): self
145 {
146 foreach ($nodes as $node) {
147 $this->insert($node);
148 }
149
150 return $this;
151 }
152 }
153