Changeset 717

Show
Ignore:
Timestamp:
06/08/07 20:10:12 (1 year ago)
Author:
schst
Message:

Fixed bug #66: Do not serialize empty attributes, unless specified

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/xml/serializer/annotations/stubXMLAttributeAnnotation.php

    r432 r717  
    3131 
    3232    /** 
     33     * Whether an empty value should be skipped 
     34     * 
     35     * @var boolean 
     36     */ 
     37    protected $skipEmpty = true; 
     38 
     39    /** 
    3340     * Set the attribute name 
    3441     * 
     
    3744    public function setAttributeName($attributeName) { 
    3845        $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; 
    3955    } 
    4056 
     
    4965 
    5066    /** 
     67     * Check, whether empty values should be skipped 
     68     * 
     69     * @return boolean 
     70     */ 
     71    public function shouldSkipEmpty() { 
     72        return $this->skipEmpty; 
     73    } 
     74 
     75    /** 
    5176     * Returns the target of the annotation as bitmap. 
    5277     * 
  • trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php

    r707 r717  
    150150            if ($property->hasAnnotation('XMLAttribute')) { 
    151151                $xmlAttribute = $property->getAnnotation('XMLAttribute'); 
     152                if ((string)$propValue === '' && $xmlAttribute->shouldSkipEmpty()) { 
     153                    continue; 
     154                } 
    152155                $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string)$propValue); 
    153156                continue; 
     
    221224            if ($method->hasAnnotation('XMLAttribute')) { 
    222225                $xmlAttribute = $method->getAnnotation('XMLAttribute'); 
     226                if ((string)$returnValue === '' && $xmlAttribute->shouldSkipEmpty()) { 
     227                    continue; 
     228                } 
    223229                $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string)$returnValue); 
    224230                continue; 
  • trunk/src/test/php/net/stubbles/xml/stubXMLSerializerTestCase.php

    r708 r717  
    161161     */ 
    162162    public $xml2 = '<foo>bar</foo>'; 
     163} 
     164 
     165/** 
     166 * Class to test empty attributes 
     167 * 
     168 * @XMLTag(tagName='test') 
     169 */ 
     170class 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    } 
    163207} 
    164208 
     
    342386 
    343387    /** 
     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    /** 
    344398     * Test serializing an object with a method matcher 
    345399     */