These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | use Traversable; |
||
21 | use TypeError; |
||
22 | use function gettype; |
||
23 | use function is_iterable; |
||
24 | use function sprintf; |
||
25 | |||
26 | /** |
||
27 | * Converts tabular data into a DOMDOcument object. |
||
28 | */ |
||
29 | class XMLConverter |
||
30 | { |
||
31 | /** |
||
32 | * XML Root name. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $root_name = 'csv'; |
||
37 | |||
38 | /** |
||
39 | * XML Node name. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $record_name = 'row'; |
||
44 | |||
45 | /** |
||
46 | * XML Item name. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $field_name = 'cell'; |
||
51 | |||
52 | /** |
||
53 | * XML column attribute name. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $column_attr = ''; |
||
58 | |||
59 | /** |
||
60 | * XML offset attribute name. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $offset_attr = ''; |
||
65 | |||
66 | /** |
||
67 | * Conversion method list. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $encoder = [ |
||
72 | 'field' => [ |
||
73 | true => 'fieldToElementWithAttribute', |
||
74 | false => 'fieldToElement', |
||
75 | ], |
||
76 | 'record' => [ |
||
77 | true => 'recordToElementWithAttribute', |
||
78 | false => 'recordToElement', |
||
79 | ], |
||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * Convert a Record collection into a DOMDocument. |
||
84 | * |
||
85 | * @param array|Traversable $records the CSV records collection |
||
86 | * |
||
87 | 6 | * @return DOMDocument |
|
88 | */ |
||
89 | 6 | public function convert($records): DOMDocument |
|
90 | 3 | { |
|
91 | if (!is_iterable($records)) { |
||
92 | throw new TypeError(sprintf('%s() expects argument passed to be iterable, %s given', __METHOD__, gettype($records))); |
||
93 | 3 | } |
|
94 | 3 | $doc = new DOMDocument('1.0'); |
|
95 | 3 | $doc->appendChild( |
|
96 | 3 | $this->import($records, $doc) |
|
0 ignored issues
–
show
|
|||
97 | 3 | ); |
|
98 | 3 | return $doc; |
|
99 | 3 | } |
|
100 | |||
101 | 3 | /** |
|
102 | * Create a new DOMElement related to the given DOMDocument. |
||
103 | 3 | * |
|
104 | * **DOES NOT** attach to the DOMDocument |
||
105 | * |
||
106 | * @param iterable $records |
||
107 | * |
||
108 | * @return DOMElement |
||
109 | */ |
||
110 | 3 | public function import($records, DOMDocument $doc): DOMElement |
|
111 | { |
||
112 | if (!is_iterable($records)) { |
||
113 | throw new TypeError(sprintf('%s() expects argument passed to be iterable, %s given', __METHOD__, gettype($records))); |
||
114 | } |
||
115 | |||
116 | 3 | $field_encoder = $this->encoder['field']['' !== $this->column_attr]; |
|
117 | 3 | $record_encoder = $this->encoder['record']['' !== $this->offset_attr]; |
|
118 | $root = $doc->createElement($this->root_name); |
||
119 | 3 | foreach ($records as $offset => $record) { |
|
120 | $node = $this->$record_encoder($doc, $record, $field_encoder, $offset); |
||
121 | $root->appendChild($node); |
||
122 | } |
||
123 | |||
124 | return $root; |
||
125 | 3 | } |
|
126 | |||
127 | 3 | /** |
|
128 | 3 | * Convert a CSV record into a DOMElement and |
|
129 | 3 | * adds its offset as DOMElement attribute. |
|
130 | 3 | */ |
|
131 | protected function recordToElementWithAttribute( |
||
132 | DOMDocument $doc, |
||
133 | 3 | array $record, |
|
134 | string $field_encoder, |
||
135 | int $offset |
||
136 | ): DOMElement { |
||
137 | $node = $this->recordToElement($doc, $record, $field_encoder); |
||
138 | $node->setAttribute($this->offset_attr, (string) $offset); |
||
139 | |||
140 | return $node; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | 3 | * Convert a CSV record into a DOMElement. |
|
145 | */ |
||
146 | 3 | protected function recordToElement(DOMDocument $doc, array $record, string $field_encoder): DOMElement |
|
147 | 3 | { |
|
148 | $node = $doc->createElement($this->record_name); |
||
149 | 3 | foreach ($record as $node_name => $value) { |
|
150 | $item = $this->$field_encoder($doc, (string) $value, $node_name); |
||
151 | $node->appendChild($item); |
||
152 | } |
||
153 | |||
154 | return $node; |
||
155 | } |
||
156 | |||
157 | 3 | /** |
|
158 | * Convert Cell to Item. |
||
159 | 3 | * |
|
160 | 3 | * Convert the CSV item into a DOMElement and adds the item offset |
|
161 | * as attribute to the returned DOMElement |
||
162 | 3 | * |
|
163 | * @param int|string $node_name |
||
164 | */ |
||
165 | protected function fieldToElementWithAttribute(DOMDocument $doc, string $value, $node_name): DOMElement |
||
166 | { |
||
167 | $item = $this->fieldToElement($doc, $value); |
||
168 | 6 | $item->setAttribute($this->column_attr, (string) $node_name); |
|
169 | |||
170 | 6 | return $item; |
|
171 | 6 | } |
|
172 | |||
173 | 3 | /** |
|
174 | * Convert Cell to Item. |
||
175 | * |
||
176 | * @param string $value Record item value |
||
177 | */ |
||
178 | protected function fieldToElement(DOMDocument $doc, string $value): DOMElement |
||
179 | { |
||
180 | $item = $doc->createElement($this->field_name); |
||
181 | 6 | $item->appendChild($doc->createTextNode($value)); |
|
182 | |||
183 | 6 | return $item; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * XML root element setter. |
||
188 | */ |
||
189 | 3 | public function rootElement(string $node_name): self |
|
190 | { |
||
191 | 3 | $clone = clone $this; |
|
192 | 3 | $clone->root_name = $this->filterElementName($node_name); |
|
193 | 3 | ||
194 | return $clone; |
||
195 | 3 | } |
|
196 | |||
197 | /** |
||
198 | * Filter XML element name. |
||
199 | * |
||
200 | * @throws DOMException If the Element name is invalid |
||
201 | */ |
||
202 | protected function filterElementName(string $value): string |
||
203 | { |
||
204 | return (new DOMElement($value))->tagName; |
||
205 | 6 | } |
|
206 | |||
207 | 6 | /** |
|
208 | 3 | * XML Record element setter. |
|
209 | */ |
||
210 | public function recordElement(string $node_name, string $record_offset_attribute_name = ''): self |
||
211 | 3 | { |
|
212 | $clone = clone $this; |
||
213 | $clone->record_name = $this->filterElementName($node_name); |
||
214 | $clone->offset_attr = $this->filterAttributeName($record_offset_attribute_name); |
||
215 | |||
216 | return $clone; |
||
217 | 3 | } |
|
218 | |||
219 | 3 | /** |
|
220 | 3 | * Filter XML attribute name. |
|
221 | 3 | * |
|
222 | * @param string $value Element name |
||
223 | 3 | * |
|
224 | * @throws DOMException If the Element attribute name is invalid |
||
225 | */ |
||
226 | protected function filterAttributeName(string $value): string |
||
227 | { |
||
228 | if ('' === $value) { |
||
229 | return $value; |
||
230 | } |
||
231 | |||
232 | return (new DOMAttr($value))->name; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * XML Field element setter. |
||
237 | */ |
||
238 | public function fieldElement(string $node_name, string $fieldname_attribute_name = ''): self |
||
239 | { |
||
240 | $clone = clone $this; |
||
241 | $clone->field_name = $this->filterElementName($node_name); |
||
242 | $clone->column_attr = $this->filterAttributeName($fieldname_attribute_name); |
||
243 | |||
244 | return $clone; |
||
245 | } |
||
246 | } |
||
247 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: