Changeset 5

Show
Ignore:
Timestamp:
08/26/06 10:36:54 (2 years ago)
Author:
schst
Message:

Added ValueConverterFactories? and put them into a chain of responsibility.
This enables us to remove the concrete classes from the TagDefinition?. But it still needs some work to make it really extensible.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/XJConf/definitions/CDataDefinition.php

    r2 r5  
    66/** 
    77 * Definition for the character data inside a tag. 
    8  *  
     8 * 
    99 * This is used to pass the character data to the constructor 
    1010 * of the tag, while casting it to the desired class. 
    11  *  
     11 * 
    1212 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    1313 */ 
     
    2828    /** 
    2929     * Converter used to convert the character data 
    30      *  
     30     * 
    3131     * @var  ValueConverter 
    3232     */ 
    3333    private $valueConverter; 
    34      
     34 
    3535    /** 
    3636     * Create a new CDataDefinition for any other type 
    37      *  
    38      * @param  string  $type  optional  type of the content   
     37     * 
     38     * @param  string  $type  optional  type of the content 
    3939     */ 
    4040    public function __construct($type = null) 
     
    5656    /** 
    5757     * Set the setter method 
    58      *  
     58     * 
    5959     * If no setter method is specified, the standard 
    60      * name "setAttributename()" will be used instead.  
    61      *  
     60     * name "setAttributename()" will be used instead. 
     61     * 
    6262     * @param  string  $setter  name of the setter method 
    6363     * @see    getSetterMethod() 
     
    6767                $this->setter = $setter; 
    6868        } 
    69          
     69 
    7070        /** 
    7171     * Get the setter method, which is setData() by default 
    72      *  
     72     * 
    7373     * @return  string 
    7474     * @see     setSetterMethod() 
     
    7878        return $this->setter; 
    7979    } 
    80      
     80 
    8181    /** 
    8282     * Character data cannot have any child definitions 
     
    8484    public function addChildDefinition(Definition $def) 
    8585    { 
    86         // Character data can not have any children.  
     86        // Character data can not have any children. 
    8787    } 
    8888 
    8989    /** 
    9090     * Convert the character data to any type 
    91      *  
     91     * 
    9292     * @param   Tag    $tag 
    9393         * @return  mixed  concerted value 
     
    106106    /** 
    107107     * get the name under which the data will be stored 
    108      *  
     108     * 
    109109     * @return  string 
    110110     */ 
     
    116116    /** 
    117117     * Get the type of the cdata 
    118      *  
     118     * 
    119119     * @param   Tag     $tag 
    120120     * @return  string 
     
    123123        return $this->valueConverter->getType(); 
    124124    } 
    125      
    126      
     125 
     126        /** 
     127         * get the type of the data 
     128         * 
     129         * @return  string 
     130         */ 
     131        public function getType() 
     132        { 
     133                return $this->type; 
     134        } 
    127135} 
    128136?> 
  • trunk/XJConf/definitions/ChildDefinition.php

    r2 r5  
    88/** 
    99 * Definition to access the child of the tag. 
    10  *  
     10 * 
    1111 * This can be used to pass the child to the constructor. 
    12  *  
     12 * 
    1313 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    1414 */ 
     
    1717    /** 
    1818     * Name of the child to access 
    19      *  
     19     * 
    2020     * @var  string 
    2121     */ 
    2222        private $name = null; 
    23      
     23 
    2424    /** 
    2525     * Create a new child definition 
    26      *  
     26     * 
    2727     * @param   string  $name  name of child 
    2828     * @throws  XJConfException 
     
    3333                        throw new XJConfException('ChildDefinition needs a name.'); 
    3434                } 
    35                  
     35 
    3636                $this->name = $name; 
    3737    } 
    38      
     38 
    3939    /** 
    4040         * Get the name of the child. 
    41          *  
     41         * 
    4242         * @return  string 
    4343         */ 
     
    4949    /** 
    5050     * Convert the value 
    51      *  
     51     * 
    5252     * @param   Tag    $tag 
    5353         * @return  mixed  concerted value 
     
    6060            throw new ValueConversionException('Child element "' . $this->getName() . '" does not exist.'); 
    6161        } 
    62          
     62 
    6363        return $child->getConvertedValue(); 
    6464    } 
     
    6666    /** 
    6767     * Get the type of the child 
    68      *  
     68     * 
    6969     * @param   Tag     $tag 
    7070     * @return  string 
     
    7777            throw new ValueConversionException('Child element "' . $this->getName() . '" does not exist.'); 
    7878        } 
    79          
     79 
    8080        return $child->getValueType($tag); 
    8181    } 
    82      
     82 
    8383    /** 
    8484     * This does not provide a setter method. 
    85      *  
     85     * 
    8686     * @return  null 
    8787     */ 
     
    9393    /** 
    9494     * It's not possible to add a new child. 
    95      *  
     95     * 
    9696     * @param  Definition  $def 
    9797     */ 
     
    100100        // Character data can not have any children. 
    101101    } 
     102 
     103        /** 
     104         * get the type of the child 
     105         * 
     106         * @return  string 
     107         */ 
     108        public function getType() 
     109        { 
     110                return null; 
     111        } 
    102112} 
    103113?> 
  • trunk/XJConf/definitions/ConstructorDefinition.php

    r2 r5  
    33/** 
    44 * Definition for the constructor of a class 
    5  *  
     5 * 
    66 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    77 */ 
     
    1010    /** 
    1111     * Parameters of the constructor 
    12      *  
     12     * 
    1313     * @var  array<Definition> 
    1414     */ 
     
    1717    /** 
    1818     * Add a new child definition (equals a parameter of the constructor) 
    19      *  
     19     * 
    2020     * @param  Definition  $def 
    2121     */ 
     
    2727    /** 
    2828     * Get the parameters of the constructor 
    29      *  
     29     * 
    3030     * @return  array 
    3131     */ 
     
    3737    /** 
    3838     * Convert the constructor. 
    39      *  
     39     * 
    4040     * This does not do anything! 
    41      *  
     41     * 
    4242     * @param  Tag  $tag 
    4343     * @return  null 
     
    4747        return null; 
    4848    } 
    49      
    50    /**     
     49 
     50   /** 
    5151    * Get the name under which it will be stored 
    52     *  
     52    * 
    5353    * @return  string 
    5454    */ 
     
    6060    /** 
    6161     * Get the type of the constructor 
    62      *  
     62     * 
    6363     * @param   Tag  $tag 
    6464     * @return  null 
     
    6868        return null; 
    6969    } 
    70      
     70 
    7171    /** 
    7272     * Get the setter method 
    73      *  
     73     * 
    7474     * @return  null 
    7575     */ 
     
    8282     * Get the names of all child elements that are used in 
    8383     * the constructor. 
    84      *  
     84     * 
    8585     * These children are not used, when adding them using 
    8686     * setter-methods. 
    87      *  
     87     * 
    8888     * @return  array 
    8989     */ 
     
    9696            } 
    9797        } 
    98          
     98 
    9999        return $childrenNames; 
    100100    } 
     101 
     102        /** 
     103         * get the type of the constructor 
     104         * 
     105         * @return  string 
     106         */ 
     107        public function getType() 
     108        { 
     109                return null; 
     110        } 
    101111} 
    102112?> 
  • trunk/XJConf/definitions/Definition.php

    r2 r5  
    22/** 
    33 * Interface for tag and attribute definitions 
    4  *   
     4 * 
    55 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    66 */ 
     
    1010     * Get the name under which the information 
    1111     * will be stored. 
    12      *  
     12     * 
    1313     * @return  string  name of the value 
    1414     */ 
     
    1717    /** 
    1818     * Get the converted value. 
    19      *  
     19     * 
    2020     * XJConf will pass the complete tag to this method 
    21      *  
     21     * 
    2222     * @param   Tag  $tag  value 
    2323     * @return  mixed 
    2424     */ 
    2525    public function convertValue(Tag $tag); 
    26      
     26 
    2727    /** 
    2828     * Get the type of the converted value 
     
    3131     */ 
    3232    public function getValueType(Tag $tag); 
    33      
     33 
    3434    /** 
    3535     * Get the name of the setter method 
    36      *  
     36     * 
    3737     * @return  string 
    3838     */ 
    3939    public function getSetterMethod(); 
    40      
     40 
    4141    /** 
    4242     * Add a child definition of any type 
    43      *  
     43     * 
    4444     * @param  Definition  def 
    4545     */ 
    4646    public function addChildDefinition(Definition $def); 
     47 
     48    /** 
     49     * Get the type of the definition 
     50     * 
     51     * @return string 
     52     */ 
     53        public function getType(); 
    4754} 
    4855?> 
  • trunk/XJConf/definitions/FactoryMethodDefinition.php

    r2 r5  
    1515    /** 
    1616     * Parameters of the factory method 
    17      *  
     17     * 
    1818     * @var  array 
    1919     */ 
     
    2121        /** 
    2222         * name of factory method 
    23          *  
     23         * 
    2424         * @var  string 
    2525         */ 
    2626        private $name; 
    27      
     27 
    2828        /** 
    2929         * construcor 
     
    3838    /** 
    3939     * Get the parameters of the factory method 
    40      *  
     40     * 
    4141     * @return  array 
    4242     */ 
     
    4848    /** 
    4949     * Add a new child definition (equals a parameter of the factory method) 
    50      *  
     50     * 
    5151     *  @param  Definition  $def 
    5252     */ 
     
    5555        array_push($this->params, $def); 
    5656    } 
    57      
    58     /**     
     57 
     58    /** 
    5959     * Get the name under which it will be stored 
    60      *  
     60     * 
    6161     * @return  string 
    6262     */ 
     
    6565                return $this->name; 
    6666        } 
    67      
     67 
    6868        /** 
    6969     * Convert the factory method. 
    70      *  
     70     * 
    7171     * @param   Tag  $tag 
    7272     * @throws  UnsupportedOperationException 
     
    7676                throw new UnsupportedOperationException(); 
    7777        } 
    78      
     78 
    7979        /** 
    8080     * Get the type of the factory method. 
    81      *  
     81     * 
    8282     * @param   Tag  $tag 
    8383     * @throws  UnsupportedOperationException 
     
    8787                throw new UnsupportedOperationException(); 
    8888        } 
    89          
     89 
    9090    /** 
    9191     * Get the setter method 
    92      *  
     92     * 
    9393     * @throws  UnsupportedOperationException 
    9494     */ 
     
    9797                throw new UnsupportedOperationException(); 
    9898        } 
     99 
     100        /** 
     101         * get the type of the factory method 
     102         * 
     103         * @return  string 
     104         */ 
     105        public function getType() 
     106        { 
     107                return null; 
     108        } 
    99109} 
    100110?> 
  • trunk/XJConf/definitions/TagDefinition.php

    r2 r5  
    11<?php 
    2 XJConfLoader::load('converters.FactoryMethodValueConverter', 
    3                    'converters.ObjectValueConverter', 
    4                    'converters.PrimitiveValueConverter', 
     2XJConfLoader::load('converters.factories.ValueConverterFactoryChain', 
    53                   'definitions.AttributeDefinition', 
    64                   'definitions.CDataDefinition', 
     
    1311/** 
    1412 * Defintion of an XML tag 
    15  *  
     13 * 
    1614 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    1715 */ 
     
    5048        /** 
    5149         * name of attribute that contains the name 
    52          *  
     50         * 
    5351         * @var  string 
    5452         */ 
     
    5654        /** 
    5755         * definition of how to construct the object 
    58          *  
     56         * 
    5957         * @var  ConstructorDefinition 
    6058         */ 
     
    6260    /** 
    6361     * definition of factory that is able to construct the object 
    64      *  
     62     * 
    6563     * @var  FactoryMethodDefinition 
    6664     */ 
     
    6866    /** 
    6967     * converts the value 
    70      *  
     68     * 
    7169     * @var  ValueConverter 
    7270     */ 
     
    7472    /** 
    7573     * definition of tag content 
    76      *  
     74     * 
    7775     * @var   CDataDefinition 
    7876     * @todo  Eventually call the setter method for the cdata 
     
    8280    /** 
    8381     * Create a new tag definition 
    84      *  
     82     * 
    8583     * @param   string  $name  name of the tag 
    8684     * @param   string  $type  type of the tag 
     
    103101        /** 
    104102         * Set the type of the tag 
    105          *  
     103         * 
    106104         * @param  string  $type 
    107105         */ 
     
    110108                $this->type = $type; 
    111109        } 
    112          
     110 
    113111    /** 
    114112     * Add a new child definition 
    115      *  
     113     * 
    116114     * Possible definitions are: 
    117115     * - AttributeDefinition 
    118116     * - ConstrcutorDefinition 
    119117     * - CDataDefinition 
    120      *  
     118     * 
    121119     * @param  Definition  $def 
    122120     */ 
     
    127125            return; 
    128126        } 
    129          
     127 
    130128        if ($def instanceof FactoryMethodDefinition) { 
    131129            $this->factoryMethod = $def; 
    132130            return; 
    133131        } 
    134          
     132 
    135133        if ($def instanceof ConstructorDefinition) { 
    136134            $this->constructor = $def; 
    137135            return; 
    138136        } 
    139          
     137 
    140138        if ($def instanceof CDataDefinition) { 
    141139            $this->cdata = $def; 
     
    146144        /** 
    147145         * Add an attribute to the tag 
    148          *  
     146         * 
    149147         * @param  AttributeDefinition  $att 
    150148         */ 
     
    153151                array_push($this->atts, $att); 
    154152        } 
    155          
     153 
    156154        /** 
    157155         * set the name of the value 
    158          *  
     156         * 
    159157         * @param  string  $name 
    160158         */ 
     
    166164        /** 
    167165         * Set the name of the tag 
    168          *  
     166         * 
    169167         * @param name 
    170168         */ 
     
    173171                $this->tagName = $name; 
    174172        } 
    175          
     173 
    176174        /** 
    177175         * Set the attribute that will be used as key. 
    178      *  
     176     * 
    179177         * @return   name of the value 
    180178         */ 
     
    184182                $this->nameAttribute = $att; 
    185183        } 
    186          
     184 
    187185        /** 
    188186         * get the name of the value 
    189          *  
     187         * 
    190188         * @return  string 
    191189         */ 
     
    194192                return $this->name; 
    195193        } 
    196          
     194 
    197195        /** 
    198196         * get the name of the tag 
    199          *  
     197         * 
    200198         * @return  string 
    201199         */ 
     
    204202                return $this->tagName; 
    205203        } 
    206          
     204 
    207205        /** 
    208206         * get the name of the tag 
    209          *  
     207         * 
    210208         * @return  string 
    211209         */ 
     
    215213                return $tag->getAttribute($this->nameAttribute); 
    216214        } 
    217          
     215 
    218216        return $this->name; 
    219217        } 
     
    221219        /** 
    222220         * get the type of the tag 
    223          *  
     221         * 
    224222         * @return  string 
    225223         */ 
     
    231229    /** 
    232230     * Get the type of the tag 
    233      *  
     231     * 
    234232     * @return   string 
    235233     */ 
     
    238236        return $this->getValueConverter()->getType(); 
    239237    } 
    240      
     238 
    241239        /** 
    242240         * Set the setter method 
    243          *  
     241         * 
    244242         * @param  string  $setter  name of the setter method 
    245243         */ 
     
    248246                $this->setter = $setter; 
    249247        } 
    250          
     248 
    251249        /** 
    252250         * Get the name of the setter method that should be used 
    253          *  
     251         * 
    254252         * @return  string 
    255253         */ 
     
    259257                        return $this->setter; 
    260258                } 
    261                  
     259 
    262260                // no name, the parent should be a collection 
    263261                if ('__none' == $this->name) { 
    264262                        return null; 
    265263                } 
    266                  
     264 
    267265                return 'set' . ucfirst($this->name); 
    268266        } 
     
    270268        /** 
    271269         * Convert the value of the tag. 
    272          *  
     270         * 
    273271         * @param   Tag    $tag  tag that will be converted 
    274272         * @return  mixed  converted value 
     
    293291            } 
    294292        } 
    295          
     293 
    296294        $conParams = array(); 
    297295        if (null != $this->factoryMethod) { 
     
    300298                $conParams = $this->constructor->getParams(); 
    301299        } 
    302          
     300 
    303301        $cParams     = array(); 
    304302        // get all values and their types 
     
    307305        } 
    308306        $instance = $this->getValueConverter()->convertValue($cParams); 
    309          
     307 
    310308        // could not call setter methods on a non-object 
    311309        if (is_object($instance) == false) { 
    312310            return $instance; 
    313311        } 
    314          
     312 
    315313        // add attributes and child elements 
    316314        $this->addAttributesToValue($instance, $tag); 
     
    321319    /** 
    322320     * Add all attributes using the appropriate setter methods 
    323      *   
     321     * 
    324322     * @param   mixed  instance 
    325323     * @param   Tag    $tag 
     
    349347    /** 
    350348     * Add all children to the created instance 
    351      *  
     349     * 
    352350     * @param   mixed  instance 
    353351     * @param   Tag    $tag 
     
    357355    { 
    358356        // traverse all children 
    359         $children = $tag->getChildren();            
     357        $children = $tag->getChildren(); 
    360358        if (count($children) == 0) { 
    361359            return; 
    362360        } 
    363          
     361 
    364362        $ignore = $this->constructor->getUsedChildrenNames(); 
    365363        $class  = new ReflectionClass(get_class($instance)); 
    366          
     364 
    367365        foreach ($children as $child) { 
    368366            if (in_array($child->getName(), $ignore) == true) { 
    369367                continue; 
    370368            } 
    371              
     369 
    372370            try { 
    373371                $class->getMethod($child->getSetterMethod())->invoke($instance, array($child->getConvertedValue())); 
     
    380378    /** 
    381379     * Check, whether the value supports indexed children 
    382      *   
     380     * 
    383381     * @return  boolean 
    384382     */ 
     
    389387                        return true; 
    390388                } 
    391                  
     389 
    392390                return false; 
    393391        } 
     
    395393    /** 
    396394     * Get the value converter for this tag 
    397      *  
     395     * 
    398396     * @return  ValueConverter 
    399397     */ 
    400398    private function getValueConverter() 
    401399    { 
    402         if (null == $this->valueConverter) { 
    403                 if (in_array($this->getType(), get_declared_classes()) == false) { 
    404                         $this->valueConverter = new PrimitiveValueConverter($this->getType()); 
    405                 } else { 
    406                         if (null != $this->factoryMethod) { 
    407                                 $this->valueConverter = new FactoryMethodValueConverter($this->getType(), $this->factoryMethod->getName()); 
    408                         } else { 
    409                                 $this->valueConverter = new ObjectValueConverter($this->getType()); 
    410                         } 
    411                 } 
    412         } 
    413          
    414         return $this->valueConverter; 
     400        return ValueConverterFactoryChain::getFactory($this)->createValueConverter($this); 
     401 
     402    } 
     403 
     404    /** 
     405     * Get the factory method definition 
     406     * 
     407     * @return FactoryMethodDefinition 
     408     * @todo   Find a solution to make this more flexible so the Definition interface provides a way to get all child definitions 
     409     */ 
     410    public function getFactoryMethod() { 
     411        return $this->factoryMethod; 
    415412    } 
    416413}