@@ -15,12 +15,12 @@ class PhpArrayToXml
15
15
protected $ _encoding = 'UTF-8 ' ;
16
16
protected $ _default_root_name = 'root ' ;
17
17
protected $ _custom_root_name = null ;
18
- protected $ _default_node_name = 'node ' ;
19
- protected $ _custom_node_name = null ;
18
+ protected $ _default_tag_name = 'node ' ;
19
+ protected $ _custom_tag_name = null ;
20
20
protected $ _separator = '_ ' ;
21
- protected $ _transform_key_names = null ;
21
+ protected $ _transform_tags = null ;
22
22
protected $ _format_output = false ;
23
- protected $ _numeric_node_suffix = null ;
23
+ protected $ _numeric_tag_suffix = null ;
24
24
25
25
/**
26
26
* Set the version of the XML (Default = '1.0')
@@ -112,7 +112,7 @@ public function getFormatOutput()
112
112
*/
113
113
public function setCustomRootName ($ value )
114
114
{
115
- if (!$ this ->isValidNodeName ($ value )) {
115
+ if (!$ this ->isValidXmlTag ($ value )) {
116
116
throw new \Exception ('Not a valid root name: ' . $ value );
117
117
}
118
118
@@ -142,45 +142,45 @@ public function getDefaultRootName()
142
142
}
143
143
144
144
/**
145
- * Set the custom node name of the XML (only used for inner arrays)
145
+ * Set the custom tag name of the XML (only used for inner arrays)
146
146
*
147
147
* @param $value
148
148
* @return PhpArrayToXml
149
149
* @throws \Exception
150
150
*/
151
- public function setCustomNodeName ($ value )
151
+ public function setCustomTagName ($ value )
152
152
{
153
- if (!$ this ->isValidNodeName ($ value )) {
154
- throw new \Exception ('Not a valid node name: ' . $ value );
153
+ if (!$ this ->isValidXmlTag ($ value )) {
154
+ throw new \Exception ('Not a valid tag name: ' . $ value );
155
155
}
156
156
157
- $ this ->_custom_node_name = $ value ;
157
+ $ this ->_custom_tag_name = $ value ;
158
158
159
159
return $ this ;
160
160
}
161
161
162
162
/**
163
- * Get the custom node name of the XML (only used for inner arrays)
163
+ * Get the custom tag name of the XML (only used for inner arrays)
164
164
*
165
165
* @return string
166
166
*/
167
- public function getCustomNodeName ()
167
+ public function getCustomTagName ()
168
168
{
169
- return $ this ->_custom_node_name ;
169
+ return $ this ->_custom_tag_name ;
170
170
}
171
171
172
172
/**
173
- * Get the default node name of the XML (only used for inner arrays)
173
+ * Get the default tag name of the XML (only used for inner arrays)
174
174
*
175
175
* @return string
176
176
*/
177
- public function getDefaultNodeName ()
177
+ public function getDefaultTagName ()
178
178
{
179
- return $ this ->_default_node_name ;
179
+ return $ this ->_default_tag_name ;
180
180
}
181
181
182
182
/**
183
- * Set the value for the separator that will be used to replace special characters in node/ tag names
183
+ * Set the value for the separator that will be used to replace special characters in tag names
184
184
*
185
185
* @param $value
186
186
* @return PhpArrayToXml
@@ -193,7 +193,7 @@ public function setSeparator($value)
193
193
}
194
194
195
195
/**
196
- * Get the value for the separator that will be used to replace special characters in node/ tag names
196
+ * Get the value for the separator that will be used to replace special characters in tag names
197
197
*
198
198
* @return string
199
199
*/
@@ -203,7 +203,7 @@ public function getSeparator()
203
203
}
204
204
205
205
/**
206
- * Set the method for transforming keys
206
+ * Set the transformation method for tag names
207
207
* Possible values:
208
208
* - null
209
209
* - 'uppercase'
@@ -212,17 +212,17 @@ public function getSeparator()
212
212
* @param $value
213
213
* @return PhpArrayToXml
214
214
*/
215
- public function setMethodTransformKeys ($ value = null )
215
+ public function setTransformTags ($ value = null )
216
216
{
217
217
switch ($ value ) {
218
218
case self ::LOWERCASE :
219
219
case self ::UPPERCASE : {
220
- $ this ->_transform_key_names = $ value ;
220
+ $ this ->_transform_tags = $ value ;
221
221
break ;
222
222
}
223
223
default : {
224
224
if ($ value === null ) {
225
- $ this ->_transform_key_names = null ;
225
+ $ this ->_transform_tags = null ;
226
226
}
227
227
}
228
228
}
@@ -231,82 +231,82 @@ public function setMethodTransformKeys($value = null)
231
231
}
232
232
233
233
/**
234
- * Get the method for transforming keys
234
+ * Get the transformation method for tag names
235
235
*
236
236
* @return string
237
237
*/
238
- public function getMethodTransformKeys ()
238
+ public function getTransformTags ()
239
239
{
240
- return $ this ->_transform_key_names ;
240
+ return $ this ->_transform_tags ;
241
241
}
242
242
243
243
/**
244
- * Set the numeric node suffix
244
+ * Set the numeric tag suffix
245
245
*
246
246
* @param null $value
247
247
* @return PhpArrayToXml
248
248
*/
249
- public function setNumericNodeSuffix ($ value = null )
249
+ public function setNumericTagSuffix ($ value = null )
250
250
{
251
- $ this ->_numeric_node_suffix = $ value ;
251
+ $ this ->_numeric_tag_suffix = $ value ;
252
252
253
253
if ($ value === true || $ value === false ) {
254
- $ this ->_numeric_node_suffix = '' ;
254
+ $ this ->_numeric_tag_suffix = '' ;
255
255
}
256
256
return $ this ;
257
257
}
258
258
259
259
/**
260
- * Get the numeric node suffix
260
+ * Get the numeric tag suffix
261
261
*
262
262
* @return null
263
263
*/
264
- public function getNumericNodeSuffix ()
264
+ public function getNumericTagSuffix ()
265
265
{
266
- return $ this ->_numeric_node_suffix ;
266
+ return $ this ->_numeric_tag_suffix ;
267
267
}
268
268
269
269
/**
270
- * Validate if a given value has a proper node/ tag starting character to be used in XML
270
+ * Validate if a given value has a proper tag starting character to be used in XML
271
271
*
272
272
* @param null $value
273
273
* @return bool
274
274
*/
275
- public static function hasValidNodeStart ($ value = null )
275
+ public static function hasValidXmlTagStartingChar ($ value = null )
276
276
{
277
- if (preg_match (self ::getValidXmlNodeStartPattern (), $ value ) === 1 ) {
277
+ if (preg_match (self ::getValidXmlTagStartPattern (), $ value ) === 1 ) {
278
278
return true ;
279
279
}
280
280
return false ;
281
281
}
282
282
283
283
/**
284
- * Validate if a given value is a valid node/ tag character
284
+ * Validate if a given value is a valid tag character
285
285
*
286
286
* @param null $value
287
287
* @return bool
288
288
*/
289
- public static function isValidNodeNameChar ($ value = null )
289
+ public static function isValidXmlTagChar ($ value = null )
290
290
{
291
- if (preg_match (self ::getValidXmlNodeNameChar (), $ value ) === 1 ) {
291
+ if (preg_match (self ::getValidXmlTagNameChar (), $ value ) === 1 ) {
292
292
return true ;
293
293
}
294
294
return false ;
295
295
}
296
296
297
297
/**
298
- * Validate if a given value is a proper node/ tag name to be used in XML
298
+ * Validate if a given value is a proper tag name to be used in XML
299
299
*
300
300
* @param null $value
301
301
* @return bool
302
302
*/
303
- public static function isValidNodeName ($ value = null )
303
+ public static function isValidXmlTag ($ value = null )
304
304
{
305
305
if (empty ($ value ) || is_int ($ value )) {
306
306
return false ;
307
307
}
308
308
309
- if (preg_match (self ::getValidXmlNodeNamePattern (), $ value ) === 1 ) {
309
+ if (preg_match (self ::getValidXmlTagNamePattern (), $ value ) === 1 ) {
310
310
return true ;
311
311
}
312
312
return false ;
@@ -333,11 +333,11 @@ public function toXmlString($array = [])
333
333
}
334
334
335
335
/**
336
- * Get a regex pattern for valid node names
336
+ * Get a regex pattern for valid tag names
337
337
*
338
338
* @return string
339
339
*/
340
- protected static function getValidXmlNodeNamePattern ()
340
+ protected static function getValidXmlTagNamePattern ()
341
341
{
342
342
return '~
343
343
# XML 1.0 Name symbol PHP PCRE regex <http://www.w3.org/TR/REC-xml/#NT-Name>
@@ -351,11 +351,11 @@ protected static function getValidXmlNodeNamePattern()
351
351
}
352
352
353
353
/**
354
- * Get a regex pattern for valid node chars
354
+ * Get a regex pattern for valid tag chars
355
355
*
356
356
* @return string
357
357
*/
358
- protected static function getValidXmlNodeNameChar ()
358
+ protected static function getValidXmlTagNameChar ()
359
359
{
360
360
return '~
361
361
(?(DEFINE)
@@ -367,11 +367,11 @@ protected static function getValidXmlNodeNameChar()
367
367
}
368
368
369
369
/**
370
- * Get a regex pattern for valid node starting characters
370
+ * Get a regex pattern for valid tag starting characters
371
371
*
372
372
* @return string
373
373
*/
374
- protected static function getValidXmlNodeStartPattern ()
374
+ protected static function getValidXmlTagStartPattern ()
375
375
{
376
376
return '~^([:A-Z_a-z \\xC0- \\xD6 \\xD8- \\xF6 \\xF8- \\x{2FF} \\x{370}- \\x{37D} \\x{37F}- \\x{1FFF} \\x{200C}- \\x{200D} \\x{2070}- \\x{218F} \\x{2C00}- \\x{2FEF} \\x{3001}- \\x{D7FF} \\x{F900}- \\x{FDCF} \\x{FDF0}- \\x{FFFD} \\x{10000}- \\x{EFFFF}])~ux ' ;
377
377
}
@@ -451,7 +451,7 @@ protected function isNumericKey($value)
451
451
*/
452
452
protected function createElement ($ name , $ value = null , $ cdata = false , $ attributes = [])
453
453
{
454
- $ name = $ this ->createValidNodeName ($ name );
454
+ $ name = $ this ->createValidTagName ($ name );
455
455
456
456
if ($ cdata === true ) {
457
457
$ element = $ this ->_doc ->createElement ($ name );
@@ -474,60 +474,60 @@ protected function createElement($name, $value = null, $cdata = false, $attribut
474
474
}
475
475
476
476
/**
477
- * Creates a valid node name
477
+ * Creates a valid tag name
478
478
*
479
479
* @param null $name
480
480
* @return string
481
481
*/
482
- protected function createValidNodeName ($ name = null )
482
+ protected function createValidTagName ($ name = null )
483
483
{
484
484
if (empty ($ name ) || $ this ->isNumericKey ($ name )) {
485
485
$ key = $ name ;
486
486
487
- if ($ this ->isValidNodeName ($ this ->getCustomNodeName ())) {
488
- $ name = $ this ->getCustomNodeName ();
487
+ if ($ this ->isValidXmlTag ($ this ->getCustomTagName ())) {
488
+ $ name = $ this ->getCustomTagName ();
489
489
} else {
490
- $ name = $ this ->transformNodeName ($ this ->getDefaultNodeName ());
490
+ $ name = $ this ->transformTagName ($ this ->getDefaultTagName ());
491
491
}
492
492
493
- if ($ this ->getNumericNodeSuffix () !== null ) {
494
- $ name = $ name . (string )$ this ->getNumericNodeSuffix () . $ key ;
493
+ if ($ this ->getNumericTagSuffix () !== null ) {
494
+ $ name = $ name . (string )$ this ->getNumericTagSuffix () . $ key ;
495
495
}
496
496
return $ name ;
497
497
}
498
498
499
- if (!$ this ->isValidNodeName ($ name )) {
500
- $ name = $ this ->replaceInvalidNodeChars ($ name );
499
+ if (!$ this ->isValidXmlTag ($ name )) {
500
+ $ name = $ this ->replaceInvalidTagChars ($ name );
501
501
502
- if (!self ::hasValidNodeStart ($ name )) {
503
- $ name = $ this ->prefixInvalidNodeStartingChar ($ name );
502
+ if (!self ::hasValidXmlTagStartingChar ($ name )) {
503
+ $ name = $ this ->prefixInvalidTagStartingChar ($ name );
504
504
}
505
505
}
506
- return $ this ->transformNodeName ($ name );
506
+ return $ this ->transformTagName ($ name );
507
507
}
508
508
509
509
/**
510
- * If a node has an invalid starting character, use an underscore as prefix
510
+ * If a tag has an invalid starting character, use an underscore as prefix
511
511
*
512
512
* @param $value
513
513
* @return string
514
514
*/
515
- protected function prefixInvalidNodeStartingChar ($ value )
515
+ protected function prefixInvalidTagStartingChar ($ value )
516
516
{
517
517
return '_ ' . substr ($ value , 1 );
518
518
}
519
519
520
520
/**
521
- * Replace invalid node characters
521
+ * Replace invalid tag characters
522
522
*
523
523
* @param $value
524
524
* @return null|string|string[]
525
525
*/
526
- protected function replaceInvalidNodeChars ($ value )
526
+ protected function replaceInvalidTagChars ($ value )
527
527
{
528
528
$ pattern = '' ;
529
529
for ($ i =0 ; $ i < strlen ($ value ); $ i ++) {
530
- if (!self ::isValidNodeNameChar ($ value [$ i ])) {
530
+ if (!self ::isValidXmlTagChar ($ value [$ i ])) {
531
531
$ pattern .= "\\$ value [$ i ]" ;
532
532
}
533
533
}
@@ -549,21 +549,21 @@ protected function createValidRootName($name = null)
549
549
if (is_string ($ name )) {
550
550
$ name = preg_replace ("/[^_a-zA-Z0-9]/ " , $ this ->getSeparator (), $ name );
551
551
}
552
- if ($ this ->isValidNodeName ($ name )) {
552
+ if ($ this ->isValidXmlTag ($ name )) {
553
553
return $ name ;
554
554
}
555
- return $ this ->transformNodeName ($ this ->getDefaultRootName ());
555
+ return $ this ->transformTagName ($ this ->getDefaultRootName ());
556
556
}
557
557
558
558
/**
559
- * Transforms a node name (only when specified)
559
+ * Transforms a tag name (only when specified)
560
560
*
561
561
* @param null $name
562
562
* @return null|string
563
563
*/
564
- protected function transformNodeName ($ name = null )
564
+ protected function transformTagName ($ name = null )
565
565
{
566
- switch ($ this ->getMethodTransformKeys ()) {
566
+ switch ($ this ->getTransformTags ()) {
567
567
case self ::LOWERCASE : {
568
568
return strtolower ($ name );
569
569
break ;
0 commit comments