Changeset 717
- Timestamp:
- 06/08/07 20:10:12 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/xml/serializer/annotations/stubXMLAttributeAnnotation.php
r432 r717 31 31 32 32 /** 33 * Whether an empty value should be skipped 34 * 35 * @var boolean 36 */ 37 protected $skipEmpty = true; 38 39 /** 33 40 * Set the attribute name 34 41 * … … 37 44 public function setAttributeName($attributeName) { 38 45 $this->attributeName = $attributeName; 46 } 47 48 /** 49 * Set the skipEmpty behaviour 50 * 51 * @param boolean $skipEmpty 52 */ 53 public function setSkipEmpty($skipEmpty) { 54 $this->skipEmpty = $skipEmpty; 39 55 } 40 56 … … 49 65 50 66 /** 67 * Check, whether empty values should be skipped 68 * 69 * @return boolean 70 */ 71 public function shouldSkipEmpty() { 72 return $this->skipEmpty; 73 } 74 75 /** 51 76 * Returns the target of the annotation as bitmap. 52 77 * trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php
r707 r717 150 150 if ($property->hasAnnotation('XMLAttribute')) { 151 151 $xmlAttribute = $property->getAnnotation('XMLAttribute'); 152 if ((string)$propValue === '' && $xmlAttribute->shouldSkipEmpty()) { 153 continue; 154 } 152 155 $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string)$propValue); 153 156 continue; … … 221 224 if ($method->hasAnnotation('XMLAttribute')) { 222 225 $xmlAttribute = $method->getAnnotation('XMLAttribute'); 226 if ((string)$returnValue === '' && $xmlAttribute->shouldSkipEmpty()) { 227 continue; 228 } 223 229 $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string)$returnValue); 224 230 continue; trunk/src/test/php/net/stubbles/xml/stubXMLSerializerTestCase.php
r708 r717 161 161 */ 162 162 public $xml2 = '<foo>bar</foo>'; 163 } 164 165 /** 166 * Class to test empty attributes 167 * 168 * @XMLTag(tagName='test') 169 */ 170 class XMLSerializerAttributeTest { 171 172 /** 173 * Empty property 174 * 175 * @var mixed 176 * @XMLAttribute(attributeName='emptyProp') 177 */ 178 public $emptyProp; 179 180 /** 181 * Empty property 182 * 183 * @var mixed 184 * @XMLAttribute(attributeName='emptyProp2', skipEmpty=false) 185 */ 186 public $emptyProp2; 187 188 /** 189 * Empty return value 190 * 191 * @return mixed 192 * @XMLAttribute(attributeName='emptyMethod') 193 */ 194 public function getEmpty() { 195 return null; 196 } 197 198 /** 199 * Empty return value 200 * 201 * @return mixed 202 * @XMLAttribute(attributeName='emptyMethod2', skipEmpty=false) 203 */ 204 public function getEmpty2() { 205 return null; 206 } 163 207 } 164 208 … … 342 386 343 387 /** 388 * Test serializing an object with empty attributes 389 */ 390 public function testObjectEmptyAttributes() { 391 $writer = new stubDomXMLStreamWriter(); 392 $obj = new XMLSerializerAttributeTest(); 393 $this->serializer->serialize($obj, $writer); 394 $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<test emptyProp2="" emptyMethod2=""/>', $writer->asXML()); 395 } 396 397 /** 344 398 * Test serializing an object with a method matcher 345 399 */
