PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.2
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.2
2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 All 67 releases
better-payment / vendor / league / csv / src / XMLConverter.php

XMLConverter.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.2, at vendor/league/csv/src/XMLConverter.php

194 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * League.Csv (https://csv.thephpleague.com)
5 *
6 * (c) Ignace Nyamagana Butera <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 declare(strict_types=1);
13
14 namespace League\Csv;
15
16 use DOMAttr;
17 use DOMDocument;
18 use DOMElement;
19 use DOMException;
20
21 /**
22 * Converts tabular data into a DOMDocument object.
23 */
24 class XMLConverter
25 {
26 /**
27 * XML Root name.
28 */
29 protected string $root_name = 'csv';
30
31 /**
32 * XML Node name.
33 */
34 protected string $record_name = 'row';
35
36 /**
37 * XML Item name.
38 */
39 protected string $field_name = 'cell';
40
41 /**
42 * XML column attribute name.
43 */
44 protected string $column_attr = '';
45
46 /**
47 * XML offset attribute name.
48 */
49 protected string $offset_attr = '';
50
51 public static function create(): self
52 {
53 return new self();
54 }
55
56 /**
57 * DEPRECATION WARNING! This method will be removed in the next major point release.
58 *
59 * @deprecated since version 9.7.0
60 * @see XMLConverter::create()
61 */
62 public function __construct()
63 {
64 }
65
66 /**
67 * Convert a Record collection into a DOMDocument.
68 */
69 public function convert(iterable $records): DOMDocument
70 {
71 $doc = new DOMDocument('1.0');
72 $node = $this->import($records, $doc);
73 $doc->appendChild($node);
74
75 return $doc;
76 }
77
78 /**
79 * Create a new DOMElement related to the given DOMDocument.
80 *
81 * **DOES NOT** attach to the DOMDocument
82 */
83 public function import(iterable $records, DOMDocument $doc): DOMElement
84 {
85 $root = $doc->createElement($this->root_name);
86 foreach ($records as $offset => $record) {
87 $node = $this->recordToElement($doc, $record, $offset);
88 $root->appendChild($node);
89 }
90
91 return $root;
92 }
93
94 /**
95 * Convert a CSV record into a DOMElement and
96 * adds its offset as DOMElement attribute.
97 */
98 protected function recordToElement(DOMDocument $doc, array $record, int $offset): DOMElement
99 {
100 $node = $doc->createElement($this->record_name);
101 foreach ($record as $node_name => $value) {
102 $item = $this->fieldToElement($doc, (string) $value, $node_name);
103 $node->appendChild($item);
104 }
105
106 if ('' !== $this->offset_attr) {
107 $node->setAttribute($this->offset_attr, (string) $offset);
108 }
109
110 return $node;
111 }
112
113 /**
114 * Convert Cell to Item.
115 *
116 * Convert the CSV item into a DOMElement and adds the item offset
117 * as attribute to the returned DOMElement
118 *
119 * @param int|string $node_name
120 */
121 protected function fieldToElement(DOMDocument $doc, string $value, $node_name): DOMElement
122 {
123 $item = $doc->createElement($this->field_name);
124 $item->appendChild($doc->createTextNode($value));
125
126 if ('' !== $this->column_attr) {
127 $item->setAttribute($this->column_attr, (string) $node_name);
128 }
129
130 return $item;
131 }
132
133 /**
134 * XML root element setter.
135 */
136 public function rootElement(string $node_name): self
137 {
138 $clone = clone $this;
139 $clone->root_name = $this->filterElementName($node_name);
140
141 return $clone;
142 }
143
144 /**
145 * Filter XML element name.
146 *
147 * @throws DOMException If the Element name is invalid
148 */
149 protected function filterElementName(string $value): string
150 {
151 return (new DOMElement($value))->tagName;
152 }
153
154 /**
155 * XML Record element setter.
156 */
157 public function recordElement(string $node_name, string $record_offset_attribute_name = ''): self
158 {
159 $clone = clone $this;
160 $clone->record_name = $this->filterElementName($node_name);
161 $clone->offset_attr = $this->filterAttributeName($record_offset_attribute_name);
162
163 return $clone;
164 }
165
166 /**
167 * Filter XML attribute name.
168 *
169 * @param string $value Element name
170 *
171 * @throws DOMException If the Element attribute name is invalid
172 */
173 protected function filterAttributeName(string $value): string
174 {
175 if ('' === $value) {
176 return $value;
177 }
178
179 return (new DOMAttr($value))->name;
180 }
181
182 /**
183 * XML Field element setter.
184 */
185 public function fieldElement(string $node_name, string $fieldname_attribute_name = ''): self
186 {
187 $clone = clone $this;
188 $clone->field_name = $this->filterElementName($node_name);
189 $clone->column_attr = $this->filterAttributeName($fieldname_attribute_name);
190
191 return $clone;
192 }
193 }
194