Changeset 100

Show
Ignore:
Timestamp:
04/17/07 14:00:13 (1 year ago)
Author:
mikey
Message:

bugfix: Check if the constructor has arguments. If the first argument has a type hint and we have an empty string replace this with an appropriate value.
removed tabs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/XJConf/converters/ConstructorValueConverter.php

    r62 r100  
    3838    { 
    3939        if (class_exists($this->className) == false) { 
    40                throw new ValueConversionException('Class "' . $this->className . '" does not exist.'); 
    41            } 
     40            throw new ValueConversionException('Class "' . $this->className . '" does not exist.'); 
     41        } 
    4242 
    43            $constructor = $def->getChildDefinition('ConstructorDefinition'); 
    44            $tmpParams   = $constructor->getParams(); 
     43        $constructor = $def->getChildDefinition('ConstructorDefinition'); 
     44        $tmpParams   = $constructor->getParams(); 
    4545        $cParams     = array(); 
    4646        // get all values and their types 
     
    5151        // try to create a new instance 
    5252        try { 
    53                $refClass = new ReflectionClass($this->className); 
    54                if (count($cParams) > 1 && method_exists($refClass, 'newInstanceArgs') == true) { 
     53            $refClass = new ReflectionClass($this->className); 
     54            if (count($cParams) > 1 && method_exists($refClass, 'newInstanceArgs') == true) { 
    5555                $instance = $refClass->newInstanceArgs($cParams); 
    56                 } elseif (count($cParams) == 1) { 
    57                     try { 
    58                        $instance = $refClass->newInstance($cParams[0]); 
    59                     } catch (ReflectionException $re) { 
    60                         $instance = $refClass->newInstance(); 
    61                     } 
    62                 } elseif (count($cParams) == 0) { 
    63                     $instance = $refClass->newInstance(); 
    64                 } else { 
    65                     throw new ValueConversionException('Could not create instance of "' . $this->className . '" as Reflection does not support newInstanceArgs().'); 
    66                 } 
     56            } elseif (count($cParams) == 1) { 
     57                // check if the constructor has arguments 
     58                // if the first argument has a type hint and we have an empty  
     59                // string replace this with an appropriate value 
     60                $refMethod = $refClass->getConstructor(); 
     61                if (null != $refMethod) { 
     62                    $params = $refMethod->getParameters(); 
     63                    if (count($params) >= 1 && $params[0]->getClass() != null && empty($cParams[0]) == true) { 
     64                        $cParams[0] = null; 
     65                    } elseif (count($params) >= 1 && $params[0]->isArray() == true && empty($cParams[0]) == true) { 
     66                        if ($params[0]->allowsNull() == true) { 
     67                            $cParams[0] = null; 
     68                        } else { 
     69                            $cParams[0] = array(); 
     70                        } 
     71                    } 
     72                } 
     73                try { 
     74                    $instance = $refClass->newInstance($cParams[0]); 
     75                } catch (ReflectionException $re) { 
     76                    $instance = $refClass->newInstance(); 
     77                } 
     78            } elseif (count($cParams) == 0) { 
     79                $instance = $refClass->newInstance(); 
     80            } else { 
     81                throw new ValueConversionException('Could not create instance of "' . $this->className . '" as Reflection does not support newInstanceArgs().'); 
     82            } 
    6783        } catch (ReflectionException $re) { 
    6884            throw new ValueConversionException('Could not create instance of "' . $this->className . '": ' . $re->getMessage());