PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.17.0
GiveWP – Donation Plugin and Fundraising Platform v2.17.0
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 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Framework / FieldsAPI / Concerns / InsertNode.php
InsertNode.php
127 lines 2.5 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\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