| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ByJG\AnyDataset\Core\Formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ByJG\AnyDataset\Core\GenericIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ByJG\Util\XmlUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class XmlFormatter extends BaseFormatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected function anydatasetXml($collection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         $anyDataSet = XmlUtil::createXmlDocumentFromStr("<anydataset></anydataset>"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         $nodeRoot = $anyDataSet->getElementsByTagName("anydataset")->item(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         foreach ($collection as $sr) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             $row = $this->rowXml($sr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             $nodeRow = $row->getElementsByTagName("row")->item(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             $newRow = XmlUtil::createChild($nodeRoot, "row"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             XmlUtil::addNodeFromNode($newRow, $nodeRow); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         return $anyDataSet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected function rowXml($row) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $node = XmlUtil::createXmlDocumentFromStr("<row></row>"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $root = $node->getElementsByTagName("row")->item(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         foreach ($row as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             if (!is_array($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 $field = XmlUtil::createChild($root, "field", $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 XmlUtil::addAttribute($field, "name", $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 foreach ($value as $valueItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                     $field = XmlUtil::createChild($root, "field", $valueItem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                     XmlUtil::addAttribute($field, "name", $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Returns the AnyDataset XmlDocument representive object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return \DOMDocument XmlDocument object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @throws \ByJG\Util\Exception\XmlUtilException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     public function raw() | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         if ($this->object instanceof GenericIterator) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             return $this->anydatasetXml($this->object->toArray()); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         return $this->rowXml($this->object->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function toText() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return $this->raw()->saveXML(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  | } |