1 | <?php |
||
13 | class XmlConstruct extends XMLWriter |
||
14 | { |
||
15 | protected $separator; |
||
16 | |||
17 | /** |
||
18 | * Constructor. |
||
19 | * |
||
20 | * @param string $rootElementName |
||
21 | * @param string $separator |
||
22 | */ |
||
23 | public function __construct(string $rootElementName, string $separator = '|') |
||
24 | { |
||
25 | $this->openMemory(); |
||
26 | $this->setIndent(true); |
||
27 | $this->setIndentString(' '); |
||
28 | $this->startDocument('1.0', 'UTF-8'); |
||
29 | |||
30 | $this->startElement($rootElementName); |
||
31 | $this->separator = $separator; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Construct elements and texts from an array. The array should contain an |
||
36 | * attribute's name in index part and a attribute's text in value part. |
||
37 | * |
||
38 | * @param array $prmArray Contains values |
||
39 | * @return XmlConstruct |
||
40 | */ |
||
41 | public function fromArray(array $prmArray): XmlConstruct |
||
61 | |||
62 | /** |
||
63 | * Construct elements and texts from a json string. |
||
64 | * |
||
65 | * @param string $jsonString |
||
66 | * @return XmlConstruct |
||
67 | */ |
||
68 | public function fromJson(string $jsonString): XmlConstruct |
||
76 | |||
77 | /** |
||
78 | * Construct elements and texts from a json string. |
||
79 | * |
||
80 | * @param JsonSerializable $jsonObject |
||
81 | * @return XmlConstruct |
||
82 | */ |
||
83 | public function fromJsonSerializable(JsonSerializable $jsonObject): XmlConstruct |
||
87 | |||
88 | /** |
||
89 | * Return the content of a current xml document. |
||
90 | * |
||
91 | * @return string XML document |
||
92 | */ |
||
93 | public function getDocument(): string |
||
100 | |||
101 | /** |
||
102 | * Write a key. |
||
103 | * |
||
104 | * @param string $key |
||
105 | * @return void|BadFunctionCallException |
||
106 | */ |
||
107 | protected function writeKey(string $key) |
||
128 | |||
129 | /** |
||
130 | * Set an element with a text to a current xml document. |
||
131 | * |
||
132 | * @param string $name An element's name |
||
133 | * @param string $text An element's text |
||
134 | * @return void|BadFunctionCallException |
||
135 | */ |
||
136 | protected function setElement(string $name, string $text) |
||
144 | |||
145 | /** |
||
146 | * Check if provided string is Json string. |
||
147 | * |
||
148 | * @param string $string |
||
149 | * @return bool |
||
150 | */ |
||
151 | protected function isJson(string $string): bool |
||
157 | } |
||
158 |