1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
use LaLit\Array2XML; |
4
|
|
|
use LaLit\XML2Array; |
5
|
|
|
|
6
|
|
|
require_once __DIR__.'/../../vendor/autoload.php'; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @param string|string[] $tags |
10
|
|
|
* |
11
|
|
|
* @return array |
12
|
|
|
*/ |
13
|
|
|
function generateTags($tags) |
14
|
|
|
{ |
15
|
|
|
if (is_array($tags)) { |
16
|
|
|
$tag = array_shift($tags); |
17
|
|
|
} else { |
18
|
|
|
$tag = $tags; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$attributeSet = [ |
22
|
|
|
'No attributes' => '', |
23
|
|
|
'Empty attribute' => ' attribute1=""', |
24
|
|
|
'Encoded attribute' => ' attribute2="<important>"', |
25
|
|
|
'Simple attribute' => ' attribute3="1"', |
26
|
|
|
'Quoted and encoded attribute' => ' attribute4="\'<important>\'"', |
27
|
|
|
'Empty quoted attribute' => ' attribute5="\'\'"', |
28
|
|
|
'Null attribute' => ' attribute6="null"', |
29
|
|
|
'All attributes' => ' attribute1="" attribute2="<important>" attribute3="1" attribute4="\'<important>\'" attribute5="\'\'" attribute6="null"', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
$cDataSet = [ |
33
|
|
|
'Null value' => 'null', |
34
|
|
|
'Empty value' => '', |
35
|
|
|
'Simple value' => 'normal', |
36
|
|
|
'Encoded value' => '<escaped>', |
37
|
|
|
'Empty CDATA' => '<![CDATA[]]>', |
38
|
|
|
'CDATA with tagged value' => '<![CDATA[<very_important>]]>', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
if (is_array($tags) && count($tags) > 0) { |
42
|
|
|
$cDataSet = array_merge($cDataSet, generateTags($tags)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$results = []; |
46
|
|
|
|
47
|
|
|
foreach ($attributeSet as $attributeType => $attribute) { |
48
|
|
|
foreach ($cDataSet as $cdataType => $cdata) { |
49
|
|
|
if (!is_array($tag)) { |
50
|
|
|
$results[$attributeType.' - '.$cdataType] = "<{$tag}{$attribute}>{$cdata}</{$tag}>"; |
51
|
|
|
} else { |
52
|
|
|
$result = ''; |
53
|
|
|
foreach (range(1, $tag[1]) as $repeat) { |
54
|
|
|
$result .= "<{$tag[0]}{$attribute}>{$cdata}</{$tag[0]}>"; |
55
|
|
|
} |
56
|
|
|
$results[$attributeType.' - '.$cdataType.' with '.$tag[1].' nodes'] = $result; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $results; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$docs = []; |
65
|
|
|
|
66
|
|
|
$docs = array_merge($docs, generateTags(['root'])); |
67
|
|
|
$docs = array_merge($docs, generateTags(['root', 'node'])); |
68
|
|
|
$docs = array_merge($docs, generateTags(['root', 'collection', ['node', 2]])); |
|
|
|
|
69
|
|
|
$docs = array_unique($docs); |
70
|
|
|
|
71
|
|
|
$testData = []; |
72
|
|
|
foreach ($docs as $key => $value) { |
73
|
|
|
$php = XML2Array::createArray($value); |
74
|
|
|
|
75
|
|
|
$testData[$key] = [ |
76
|
|
|
unserialize(str_replace('s:4:"null";', 'N;', serialize($php))), |
77
|
|
|
str_replace('null', '', Array2XML::createXML('root', $php['root'])->saveXML()), |
78
|
|
|
$key, |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
file_put_contents(__DIR__.'/../files/testData.inc', '<?php return '.var_export(array_values($testData), true).';'); |
83
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.