@@ -83,7 +83,7 @@ public function reporting()
8383 * Convert a array in node elements
8484 *
8585 * @param array|\Traversable $data
86- * @throws \Inphinit\Experimental\Exception
86+ * @throws \Inphinit\Experimental\Dom\DomException
8787 * @return void
8888 */
8989 public function fromArray (array $ data )
@@ -92,8 +92,12 @@ public function fromArray(array $data)
9292 throw new DomException ('Array is empty ' , 2 );
9393 } elseif (count ($ data ) > 1 ) {
9494 throw new DomException ('Root array accepts only a key ' , 2 );
95- } elseif (Helper::seq ($ data )) {
96- throw new DomException ('Document accpet only a node ' , 2 );
95+ }
96+
97+ $ root = key ($ data );
98+
99+ if (self ::validTag ($ root ) === false ) {
100+ throw new DomException ('Invalid root < ' . $ root . '> tag ' , 2 );
97101 }
98102
99103 if ($ this ->documentElement ) {
@@ -102,7 +106,7 @@ public function fromArray(array $data)
102106
103107 $ this ->enableRestoreInternal (true );
104108
105- $ this ->generate ($ this , $ data );
109+ $ this ->generate ($ this , $ data, 2 );
106110
107111 $ this ->raise ($ this ->exceptionlevel );
108112
@@ -132,7 +136,7 @@ public function toJson($format = Document::MINIMAL, $options = 0)
132136 * Convert DOM to Array
133137 *
134138 * @param int $type
135- * @throws \Inphinit\Experimental\Exception
139+ * @throws \Inphinit\Experimental\Dom\DomException
136140 * @return array
137141 */
138142 public function toArray ($ type = Document::SIMPLE )
@@ -186,7 +190,7 @@ public function __toString()
186190 *
187191 * @param string $path
188192 * @param int $format Support XML, HTML, and JSON
189- * @throws \Inphinit\Experimental\Exception
193+ * @throws \Inphinit\Experimental\Dom\DomException
190194 * @return void
191195 */
192196 public function save ($ path , $ format = Document::XML )
@@ -220,7 +224,6 @@ public function save($path, $format = Document::XML)
220224 * Get namespace attributes from root element or specific element
221225 *
222226 * @param \DOMElement $element
223- * @throws \Inphinit\Experimental\Exception
224227 * @return void
225228 */
226229 public function getNamespaces (\DOMElement $ element = null )
@@ -387,34 +390,43 @@ private function raise($debuglvl)
387390 \libxml_clear_errors ();
388391 }
389392
390- private function generate (\DOMNode $ node , $ data )
393+ private function generate (\DOMNode $ node , $ data, $ errorLevel )
391394 {
392395 if (is_array ($ data ) === false ) {
393396 $ node ->textContent = $ data ;
394397 return ;
395398 }
396399
400+ $ nextLevel = $ errorLevel + 1 ;
401+
397402 foreach ($ data as $ key => $ value ) {
398403 if ($ key === '@comments ' ) {
399404 continue ;
400405 } elseif ($ key === '@contents ' ) {
401- $ this ->generate ($ node , $ value );
406+ $ this ->generate ($ node , $ value, $ nextLevel );
402407 } elseif ($ key === '@attributes ' ) {
403408 $ this ->attrs ($ node , $ value );
404- } elseif (preg_match ( ' #^([a-z]|[a-z][\w:])+$#i ' , $ key )) {
409+ } elseif (self :: validTag ( $ key )) {
405410 if (Helper::seq ($ value )) {
406411 foreach ($ value as $ subvalue ) {
407- $ this ->generate ($ node , array ($ key => $ subvalue ));
412+ $ this ->generate ($ node , array ($ key => $ subvalue ), $ nextLevel );
408413 }
409414 } elseif (is_array ($ value )) {
410- $ this ->generate ($ this ->add ($ key , '' , $ node ), $ value );
415+ $ this ->generate ($ this ->add ($ key , '' , $ node ), $ value, $ nextLevel );
411416 } else {
412417 $ this ->add ($ key , $ value , $ node );
413418 }
419+ } else {
420+ throw new DomException ('Invalid root < ' . $ key . '> tag ' , $ nextLevel );
414421 }
415422 }
416423 }
417424
425+ private function validTag ($ tagName )
426+ {
427+ return preg_match ('#^([a-z_](\w+|)|[a-z_](\w+|):[a-z_](\w+|))$#i ' , $ tagName ) > 0 ;
428+ }
429+
418430 private function add ($ name , $ value , \DOMNode $ node )
419431 {
420432 $ newdom = $ this ->createElement ($ name , $ value );
0 commit comments