Changeset 210
- Timestamp:
- 02/05/07 19:46:49 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php
r209 r210 10 10 'net.stubbles.xml.serializer.annotations.XMLAttribute', 11 11 'net.stubbles.xml.serializer.annotations.XMLIgnore', 12 'net.stubbles.xml.serializer.annotations.XMLMatcher', 12 13 'net.stubbles.relection.stubReflection'); 13 14 … … 124 125 // export props 125 126 $properties = $clazz->getProperties(); 127 128 // get the property-matcher 129 if ($clazz->hasAnnotation('XMLProperties')) { 130 $matcher = $clazz->getAnnotation('XMLProperties'); 131 } else { 132 $matcher = null; 133 } 134 126 135 foreach ($properties as $property) { 127 136 if (!$property->isPublic()) { … … 131 140 continue; 132 141 } 142 if (null !== $matcher && !$matcher->matches($property->getName())) { 143 continue; 144 } 145 133 146 $propValue = $property->getValue($object); 134 147 if ($property->hasAnnotation('XMLAttribute')) { … … 139 152 if ($property->hasAnnotation('XMLTag')) { 140 153 $xmlTag = $property->getAnnotation('XMLTag'); 141 142 if (is_array($propValue)) { 143 $this->serializeArray($propValue, $xmlWriter, $xmlTag->getTagName(), $xmlTag->getElementTagName()); 144 } else { 145 $this->serializeDispatcher($propValue, $xmlWriter, $xmlTag->getTagName()); 146 } 147 continue; 148 } 154 $tagName = $xmlTag->getTagName(); 155 $elementName = $xmlTag->getElementTagName(); 156 } else { 157 $tagName = $property->getName(); 158 $elementName = null; 159 } 160 161 if (is_array($propValue)) { 162 $this->serializeArray($propValue, $xmlWriter, $tagName, $elementName); 163 } else { 164 $this->serializeDispatcher($propValue, $xmlWriter, $tagName); 165 } 149 166 } 150 167 151 168 // export methods 152 169 $methods = $clazz->getMethods(); 170 171 // get the method-matcher 172 if ($clazz->hasAnnotation('XMLMethods')) { 173 $matcher = $clazz->getAnnotation('XMLMethods'); 174 } else { 175 $matcher = null; 176 } 177 153 178 foreach ($methods as $method) { 154 179 if (!$method->isPublic()) { … … 161 186 continue; 162 187 } 188 if (0 != $method->getNumberOfParameters()) { 189 continue; 190 } 163 191 if ($method->hasAnnotation('XMLIgnore')) { 164 192 continue; 165 193 } 194 if (null !== $matcher && !$matcher->matches($method->getName())) { 195 continue; 196 } 197 198 166 199 $returnValue = $method->invoke($object); 167 200 if ($method->hasAnnotation('XMLAttribute')) { … … 172 205 if ($method->hasAnnotation('XMLTag')) { 173 206 $xmlTag = $method->getAnnotation('XMLTag'); 174 175 if (is_array($returnValue)) { 176 $this->serializeArray($returnValue, $xmlWriter, $xmlTag->getTagName(), $xmlTag->getElementTagName()); 177 } else { 178 $this->serializeDispatcher($returnValue, $xmlWriter, $xmlTag->getTagName()); 179 } 180 continue; 181 } 207 $tagName = $xmlTag->getTagName(); 208 $elementName = $xmlTag->getElementTagName(); 209 } else { 210 $tagName = $method->getName(); 211 $elementName = null; 212 } 213 214 if (is_array($propValue)) { 215 $this->serializeArray($returnValue, $xmlWriter, $tagName, $elementName); 216 } else { 217 $this->serializeDispatcher($returnValue, $xmlWriter, $tagName); 218 } 182 219 } 183 220 $xmlWriter->writeEndElement(); trunk/src/test/php/net/stubbles/xml/stubXMLSerializerTestCase.php
r209 r210 12 12 /** 13 13 * Simple Test class to test the XMLSerializer 14 */ 15 class XMLSerializerDefaultObj { 16 private $ignore = 'ignore'; 17 public $foo = 'foo'; 18 19 public function getBar() { 20 return 'bar'; 21 } 22 23 public function setBar($bar) { 24 throw new Exception('setBar has been called'); 25 } 26 } 27 28 /** 29 * Simple Test class to test the XMLSerializer 14 30 * 15 31 * @XMLTag(tagName=foo) … … 77 93 public function getValue() { 78 94 return "returned"; 95 } 96 } 97 98 /** 99 * Simple Test class to test the XMLSerializer 100 * 101 * @XMLTag(tagName=testObject) 102 * @XMLProperties[XMLMatcher](pattern=/^[a-zA-Z]{3}$/) 103 */ 104 class XMLSerializerPropertyMatcher { 105 public $one = 'one'; 106 public $two = 'two'; 107 public $three = 'three'; 108 } 109 110 111 /** 112 * Simple Test class to test the XMLSerializer 113 * 114 * @XMLTag(tagName=testObject) 115 * @XMLMethods[XMLMatcher](pattern=/^getF/) 116 */ 117 class XMLSerializerMethodMatcher { 118 public function getFoo() { 119 return "foo"; 120 } 121 public function getBar() { 122 return "bar"; 79 123 } 80 124 } … … 222 266 $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<class method="returned"/>', $writer->asXML()); 223 267 } 268 269 /** 270 * Test serializing an object with a property matcher 271 */ 272 public function testObjectPropertyMatcher() { 273 $writer = new stubDomXMLStreamWriter(); 274 $obj = new XMLSerializerPropertyMatcher(); 275 $this->serializer->serialize($obj, $writer); 276 $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<testObject><one>one</one><two>two</two></testObject>', $writer->asXML()); 277 } 278 279 /** 280 * Test serializing an object with a method matcher 281 */ 282 public function testObjectMethodMatcher() { 283 $writer = new stubDomXMLStreamWriter(); 284 $obj = new XMLSerializerMethodMatcher(); 285 $this->serializer->serialize($obj, $writer); 286 $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<testObject><getFoo>foo</getFoo></testObject>', $writer->asXML()); 287 } 288 289 /** 290 * Test serializing an object with a method matcher 291 */ 292 public function testObjectDefault() { 293 $writer = new stubDomXMLStreamWriter(); 294 $obj = new XMLSerializerDefaultObj(); 295 $this->serializer->serialize($obj, $writer); 296 $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<XMLSerializerDefaultObj><foo>foo</foo><getBar>bar</getBar></XMLSerializerDefaultObj>', $writer->asXML()); 297 } 224 298 } 225 299 ?>
