Changeset 393

Show
Ignore:
Timestamp:
03/16/07 21:51:56 (1 year ago)
Author:
schst
Message:

Fixed bug #21

Files:

Legend:

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

    r223 r393  
    4040 
    4141    /** 
    42      * Check, whether the method or property name matches 
     42     * Get the name of the tag to use for a property 
    4343     * 
    44      * @return string 
     44     * @param stubReflectionProperty $property 
     45     * @param mixed $propertyValue 
     46     * @return string|false 
    4547     */ 
    46     public function matches($string) { 
    47         return preg_match($this->pattern, $string); 
     48    public function getTagnameForProperty(stubReflectionProperty $property, $propertyValue) { 
     49        $matches = array(); 
     50        if (!preg_match($this->pattern, $property->getName(), $matches)) { 
     51            return false; 
     52        } 
     53        if (isset($matches[1])) { 
     54            return $matches[1]; 
     55        } 
     56        return $matches[0]; 
     57    } 
     58 
     59    /** 
     60     * Get the name of the tag to use for a method 
     61     * 
     62     * @param stubReflectionMethod $method 
     63     * @param mixed $returnValue 
     64     */ 
     65    public function getTagnameForMethod(stubReflectionMethod $method, $returnValue) { 
     66        $matches = array(); 
     67        if (!preg_match($this->pattern, $method->getName(), $matches)) { 
     68            return false; 
     69        } 
     70        if (isset($matches[1])) { 
     71            $name = $matches[1]; 
     72        } else { 
     73            $name = $matches[0]; 
     74        } 
     75        return strtolower($name{0}) . substr($name, 1); 
    4876    } 
    4977 
  • trunk/src/main/php/net/stubbles/xml/serializer/annotations/stubXMLMethodsAnnotation.php

    r223 r393  
    1717 * @subpackage  xml_serializer 
    1818 */ 
    19 interface stubXMLMethodsAnnotation extends stubXMLPropertiesAnnotation { 
     19interface stubXMLMethodsAnnotation { 
     20 
     21    /** 
     22     * Get the name of the tag to use for a method 
     23     * 
     24     * @param stubReflectionMethod $method 
     25     * @param mixed $returnValue 
     26     * @return string|false 
     27     */ 
     28    public function getTagnameForMethod(stubReflectionMethod $method, $returnValue); 
    2029} 
    2130?> 
  • trunk/src/main/php/net/stubbles/xml/serializer/annotations/stubXMLPropertiesAnnotation.php

    r223 r393  
    1919 */ 
    2020interface stubXMLPropertiesAnnotation { 
    21     public function matches($string); 
     21 
     22    /** 
     23     * Get the name of the tag to use for a property 
     24     * 
     25     * @param stubReflectionProperty $property 
     26     * @param mixed $propertyValue 
     27     * @return string|false 
     28     */ 
     29    public function getTagnameForProperty(stubReflectionProperty $property, $propertyValue); 
    2230} 
    2331?> 
  • trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php

    r251 r393  
    142142                continue; 
    143143            } 
    144             if (null !== $matcher && !$matcher->matches($property->getName())) { 
    145                 continue; 
    146             } 
    147  
    148144            $propValue = $property->getValue($object); 
     145 
    149146            if ($property->hasAnnotation('XMLAttribute')) { 
    150147                $xmlAttribute = $property->getAnnotation('XMLAttribute'); 
     
    157154                $elementName = $xmlTag->getElementTagName(); 
    158155            } else { 
    159                 $tagName = $property->getName(); 
     156                if (null !== $matcher) { 
     157                    $tagName = $matcher->getTagnameForProperty($property, $propValue); 
     158                    if ($tagName === false) { 
     159                        continue; 
     160                    } 
     161                } else { 
     162                    $tagName = $property->getName(); 
     163                } 
    160164                $elementName = null; 
    161165            } 
     
    177181            $matcher = null; 
    178182        } 
    179  
    180183        foreach ($methods as $method) { 
    181184            if (!$method->isPublic()) { 
     
    192195            } 
    193196            if ($method->hasAnnotation('XMLIgnore')) { 
    194                 continue; 
    195             } 
    196             if (null !== $matcher && !$matcher->matches($method->getName())) { 
    197197                continue; 
    198198            } 
     
    210210                $elementName = $xmlTag->getElementTagName(); 
    211211            } else { 
    212                 $tagName = $method->getName(); 
     212                if (null !== $matcher) { 
     213                    $tagName = $matcher->getTagnameForMethod($method, $returnValue); 
     214                    if ($tagName === false) { 
     215                        continue; 
     216                    } 
     217                } else { 
     218                    $tagName = $method->getName(); 
     219                } 
    213220                $elementName = null; 
    214221            } 
  • trunk/src/test/php/net/stubbles/xml/stubXMLSerializerTestCase.php

    r210 r393  
    100100 * 
    101101 * @XMLTag(tagName=testObject) 
    102  * @XMLProperties[XMLMatcher](pattern=/^[a-zA-Z]{3}$/) 
     102 * @XMLProperties[XMLMatcher](pattern=/^([a-zA-Z]{3})$/) 
    103103 */ 
    104104class XMLSerializerPropertyMatcher { 
     
    113113 * 
    114114 * @XMLTag(tagName=testObject) 
    115  * @XMLMethods[XMLMatcher](pattern=/^getF/) 
     115 * @XMLMethods[XMLMatcher](pattern=/^get(F.+)/) 
    116116 */ 
    117117class XMLSerializerMethodMatcher { 
     
    284284        $obj = new XMLSerializerMethodMatcher(); 
    285285        $this->serializer->serialize($obj, $writer); 
    286         $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<testObject><getFoo>foo</getFoo></testObject>', $writer->asXML()); 
     286        $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<testObject><foo>foo</foo></testObject>', $writer->asXML()); 
    287287    } 
    288288