Пример #1
0
 /**
  * Builds an XML document to represent the model. Some configuration is
  * available through <code>options</code>. However more complicated cases should
  * override <code>Mad_Model_Base#toXml</code>.
  *
  * By default the generated XML document will include the processing
  * instruction and all the object's attributes. For example:
  *
  *   <?xml version="1.0" encoding="UTF-8"?>
  *   <topic>
  *     <title>The First Topic</title>
  *     <author-name>David</author-name>
  *     <id type="integer">1</id>
  *     <approved type="boolean">false</approved>
  *     <replies-count type="integer">0</replies-count>
  *     <bonus-time type="datetime">2000-01-01T08:28:00+12:00</bonus-time>
  *     <written-on type="datetime">2003-07-16T09:28:00+1200</written-on>
  *     <content>Have a nice day</content>
  *     <author-email-address>david@loudthinking.com</author-email-address>
  *     <parent-id></parent-id>
  *     <last-read type="date">2004-04-15</last-read>
  *   </topic>
  *
  * This behavior can be controlled with <code>only</code>, <code>except</code>,
  * <code>skip_instruct</code>, <code>skip_types</code> and <code>dasherize</code>.
  * The <code>only</code> and <code>except</code> options are the same as for the
  * <code>attributes</code> method. The default is to dasherize all column names, but you
  * can disable this setting <code>dasherize</code> to <code>false</code>. To not have the
  * column type included in the XML output set <code>:skip_types</code> to <code>true</code>.
  *
  * For instance:
  *
  *   $topic->toXml(array('skip_instruct' => true, 
  *                       'except' => array('id', 'bonus_time', 'written_on', 'replies_count'));
  *
  *   <topic>
  *     <title>The First Topic</title>
  *     <author-name>David</author-name>
  *     <approved type="boolean">false</approved>
  *     <content>Have a nice day</content>
  *     <author-email-address>david@loudthinking.com</author-email-address>
  *     <parent-id></parent-id>
  *     <last-read type="date">2004-04-15</last-read>
  *   </topic>
  *
  * To include first level associations use <code>include</code>:
  *
  *   $firm->toXml(array('include' => array('Account', 'Clients')));
  *
  *   <?xml version="1.0" encoding="UTF-8"?>
  *   <firm>
  *     <id type="integer">1</id>
  *     <rating type="integer">1</rating>
  *     <name>37signals</name>
  *     <clients type="array">
  *       <client>
  *         <rating type="integer">1</rating>
  *         <name>Summit</name>
  *       </client>
  *       <client>
  *         <rating type="integer">1</rating>
  *         <name>Microsoft</name>
  *       </client>
  *     </clients>
  *     <account>
  *       <id type="integer">1</id>
  *       <credit-limit type="integer">50</credit-limit>
  *     </account>
  *   </firm>
  *
  * To include deeper levels of associations pass a hash like this:
  *
  *   $firm->toXml(array('include' => array('Account' => array(), 
  *                                         'Clients' => array('include' => 'Address'))));
  *
  *   <?xml version="1.0" encoding="UTF-8"?>
  *   <firm>
  *     <id type="integer">1</id>
  *     <rating type="integer">1</rating>
  *     <name>37signals</name>
  *     <clients type="array">
  *       <client>
  *         <rating type="integer">1</rating>
  *         <name>Summit</name>
  *         <address>
  *           ...
  *         </address>
  *       </client>
  *       <client>
  *         <rating type="integer">1</rating>
  *         <name>Microsoft</name>
  *         <address>
  *           ...
  *         </address>
  *       </client>
  *     </clients>
  *     <account>
  *       <id type="integer">1</id>
  *       <credit-limit type="integer">50</credit-limit>
  *     </account>
  *   </firm>
  *
  * To include any methods on the model being called use <code>methods</code>:
  *
  *   $firm->toXml(array('methods' => array('calculated_earnings', 'real_earnings')));
  *
  *   <firm>
  *     # ... normal attributes as shown above ...
  *     <calculated-earnings>100000000000000000</calculated-earnings>
  *     <real-earnings>5</real-earnings>
  *   </firm>
  *
  * As noted above, you may override <code>toXml()</code> in your <code>Mad_Model_Base</code>
  * subclasses to have complete control about what's generated. The general
  * form of doing this is:
  *
  *   class IHaveMyOwnXML extends Mad_Model_Base
  *   {
  *      public function toXml($options = array)
  *      {
  *          // ...
  *      }
  *   }
  */
 public function toXml($options = array())
 {
     $serializer = new Mad_Model_Serializer_Xml($this, $options);
     return $serializer->serialize();
 }
Пример #2
0
 public function testSerializeWithProperties()
 {
     $record = $this->articles('xml_rpc');
     $record->validity = array('is' => 'great');
     $record->is_good = true;
     $options = array('properties' => array('validity', 'is_good'), 'indent' => 0);
     $serializer = new Mad_Model_Serializer_Xml($record, $options);
     $xml = $serializer->serialize($record, $options);
     $this->assertContains('<is-good type="boolean">true</is-good>', $xml);
     $this->assertContains('<validity><is>great</is></validity>', $xml);
 }