Changeset 491

Show
Ignore:
Timestamp:
04/13/07 13:54:07 (2 years ago)
Author:
mikey
Message:

improved type detection: doubles are now recognized as well
unrecognized values will be checked if they are constant names, if yes value of constant with this name will be returned
values that can not be converted into a type will be treated as string

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/stubAnnotationStateParser.php

    r484 r491  
    141141     * @param   string  $value     the value of the param 
    142142     * @param   bool    $asString  whether the value is a string or not 
    143      * @throws  ReflectionException 
    144143     */ 
    145144    public function registerSingleAnnotationParam($value, $asString = false) 
     
    158157     * @param   string  $value     the value of the param 
    159158     * @param   bool    $asString  whether the value is a string or not 
    160      * @throws  ReflectionException 
    161159     */ 
    162160    public function setAnnotationParamValue($value, $asString = false) 
     
    181179     * @param   boolean  $asString  whether value should be treated as string or not 
    182180     * @return  mixed 
    183      * @throws  ReflectionException 
    184      * @todo    improve type detection and error handling 
    185181     */ 
    186182    protected function convertAnnotationValue($value, $asString) 
     
    198194        } 
    199195         
    200         if ('null' === $value || 'NULL' == $value) { 
     196        if ('null' === strtolower($value)) { 
    201197            return null; 
    202198        } 
     
    204200        if (preg_match('/^[+-]?[0-9]+$/', $value) != false) {            
    205201            return (integer) $value; 
     202        } 
     203         
     204        if (preg_match('/^[+-]?[0-9]+\.[0-9]+$/', $value) != false) {            
     205            return (double) $value; 
    206206        } 
    207207         
     
    211211        } 
    212212         
    213         throw new ReflectionException('Could not determine type of value ' . $value); 
     213        if (defined($value) == true) { 
     214            return constant($value); 
     215        } 
     216         
     217        return (string) $value; 
    214218    } 
    215219} 
  • trunk/src/test/php/net/stubbles/reflection/annotations/parser/stubAnnotationStateParserTestCase.php

    r483 r491  
    99 */ 
    1010stubClassLoader::load('net.stubbles.reflection.annotations.parser.stubAnnotationStateParser'); 
     11define('ANNOTATION_TEST_CONSTANT', 'baz'); 
    1112/** 
    1213 * This is a test class that has many annotations. 
     
    2021 * @InvalidChars(foo='ba@r=,') 
    2122 * @SingleValue(42) 
     23 * @Constant(foo=ANNOTATION_TEST_CONSTANT) 
    2224 * @SingleStringValue('This is a string with chars like = or ,') 
    2325 * @WithEscaped(foo='This string contains \' and \\, which is possible using escaping...') 
     
    2729 *            null=null, 
    2830 *            negInt=-13, 
     31 *            double=2.34, 
     32 *            negDouble=-5.67, 
    2933 *            string1='true', 
    3034 *            string2='null', 
     
    7276        $this->assertEqual($annotations['SingleValue']['type'], 'SingleValue'); 
    7377        $this->assertEqual($annotations['SingleValue']['params'], array('value' => 42)); 
     78        $this->assertTrue(isset($annotations['Constant'])); 
     79        $this->assertEqual($annotations['Constant']['type'], 'Constant'); 
     80        $this->assertEqual($annotations['Constant']['params'], array('foo' => ANNOTATION_TEST_CONSTANT)); 
    7481        $this->assertTrue(isset($annotations['SingleStringValue'])); 
    7582        $this->assertEqual($annotations['SingleStringValue']['type'], 'SingleStringValue'); 
     
    8693        $this->assertFalse($annotations['WithTypes']['params']['false']); 
    8794        $this->assertEqual($annotations['WithTypes']['params']['integer'], 4562); 
     95        $this->assertEqual(gettype($annotations['WithTypes']['params']['integer']), 'integer'); 
    8896        $this->assertNull($annotations['WithTypes']['params']['null']); 
    8997        $this->assertEqual($annotations['WithTypes']['params']['negInt'], -13); 
     98        $this->assertEqual(gettype($annotations['WithTypes']['params']['negInt']), 'integer'); 
     99        $this->assertEqual($annotations['WithTypes']['params']['double'], 2.34); 
     100        $this->assertEqual(gettype($annotations['WithTypes']['params']['double']), 'double'); 
     101        $this->assertEqual($annotations['WithTypes']['params']['negDouble'], -5.67); 
     102        $this->assertEqual(gettype($annotations['WithTypes']['params']['negDouble']), 'double'); 
    90103        $this->assertEqual($annotations['WithTypes']['params']['string1'], 'true'); 
    91104        $this->assertEqual($annotations['WithTypes']['params']['string2'], 'null');