Example #1
0
 /**
  * Convert to XML
  *
  * @access public
  * @param Mapper_Mapable $subject Object to map to XML
  * @return SimpleXMLElement
  */
 public function toXML(Mapper_Mapable $subject)
 {
     $xmlRoot = new SimpleXMLElement('<' . $this->mapObjectToElementName($subject) . ' />');
     if ($subject instanceof ArrayObject && !$subject instanceof SyncArray) {
         $xmlRoot->addAttribute('type', 'array');
     }
     if ($subject instanceof DeleteBySaving && $subject->isDeleted()) {
         $xmlRoot->addChild('_destroy', '1');
     }
     foreach ($subject->toArray() as $property => $value) {
         $key = $this->propertyToXmlkey($property);
         if (is_null($value)) {
             $xmlRoot->addChild($key)->addAttribute('nil', 'true');
         } elseif (is_bool($value)) {
             $xmlRoot->addChild($key, $value === true ? 'true' : 'false');
         } elseif (is_array($value)) {
             foreach ($value as $v) {
                 $xmlRoot->addChild($key, $v);
             }
         } elseif (!is_object($value) && !is_array($value)) {
             $xmlRoot->addChild($key, $value);
         } elseif (is_object($value) && $value instanceof Mapper_Mapable) {
             $xmlRoot->appendXML($this->toXml($value));
         } elseif (is_object($value) && $value instanceof \DateTime) {
             $xmlRoot->addChild($key, $value->format('c'));
         } else {
             throw new Mapper_Exception('Invalid value for key ' . $key);
         }
     }
     return $xmlRoot;
 }