Changeset 6

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

ValueConverterFactories? now only use the Definition interface, as this now provides a getChildDefinitions() method.
The ValueConverterFactoryChain? is now also used in the AttributeDefinition?.

Files:

Legend:

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

    r5 r6  
    44class FactoryMethodValueConverterFactory implements ValueConverterFactory { 
    55 
    6     public function isResponsible(TagDefinition $def) { 
    7         return ($def->getFactoryMethod() != null); 
     6    public function isResponsible(Definition $def) { 
     7        return ($this->getFactoryMethod($def) != null); 
    88    } 
    99 
    10     public function createValueConverter(TagDefinition $def) { 
    11         $converter = new FactoryMethodValueConverter($def->getType(), $def->getFactoryMethod()->getName()); 
     10    public function createValueConverter(Definition $def) { 
     11        $factoryMethod = $this->getFactoryMethod($def); 
     12        if ($factoryMethod == null) { 
     13            return null; 
     14        } 
     15        $converter = new FactoryMethodValueConverter($def->getType(), $factoryMethod->getName()); 
    1216        return $converter; 
     17    } 
     18 
     19    /** 
     20     * Get the fatcory method of the definition, if available 
     21     * 
     22     * @param Definition $def 
     23     * @return FactoryMethodDefinition 
     24     */ 
     25    protected function getFactoryMethod(Definition $def) { 
     26        foreach ($def->getChildDefinitions() as $child) { 
     27            if ($child instanceof FactoryMethodDefinition) { 
     28                return $child; 
     29            } 
     30        } 
     31        return null; 
    1332    } 
    1433} 
  • trunk/XJConf/converters/factories/ObjectValueConverterFactory.php

    r5 r6  
    44class ObjectValueConverterFactory implements ValueConverterFactory { 
    55 
    6     public function isResponsible(TagDefinition $def) { 
     6    public function isResponsible(Definition $def) { 
    77        return class_exists($def->getType()); 
    88    } 
    99 
    10     public function createValueConverter(TagDefinition $def) { 
     10    public function createValueConverter(Definition $def) { 
    1111        $converter = new ObjectValueConverter($def->getType()); 
    1212        return $converter; 
  • trunk/XJConf/converters/factories/PrimitiveValueConverterFactory.php

    r5 r6  
    88                          ); 
    99 
    10     public function isResponsible(TagDefinition $def) { 
     10    public function isResponsible(Definition $def) { 
    1111        return in_array($def->getType(), $this->primitives); 
    1212    } 
    1313 
    14     public function createValueConverter(TagDefinition $def) { 
     14    public function createValueConverter(Definition $def) { 
    1515        $converter = new PrimitiveValueConverter($def->getType()); 
    1616        return $converter; 
  • trunk/XJConf/converters/factories/ValueConverterFactory.php

    r5 r6  
    1919     * @return boolean 
    2020     */ 
    21     public function isResponsible(TagDefinition $def); 
     21    public function isResponsible(Definition $def); 
    2222 
    2323    /** 
     
    2727     * @return ValueConverter 
    2828     */ 
    29     public function createValueConverter(TagDefinition $def); 
     29    public function createValueConverter(Definition $def); 
    3030} 
    3131?> 
  • trunk/XJConf/converters/factories/ValueConverterFactoryChain.php

    r5 r6  
    1212    } 
    1313 
    14     public static function getFactory(TagDefinition $def) { 
     14    public static function getFactory(Definition $def) { 
    1515        foreach (self::$factories as $factory) { 
    1616            if ($factory->isResponsible($def)) { 
  • trunk/XJConf/definitions/AttributeDefinition.php

    r2 r6  
    11<?php 
    2 XJConfLoader::load('converters.ObjectValueConverter', 
    3                    'converters.PrimitiveValueConverter', 
     2XJConfLoader::load('converters.factories.ValueConverterFactoryChain', 
    43                   'definitions.Definition', 
    54                   'exceptions.MissingAttributeException', 
     
    87/** 
    98 * Definition container of an attribute. 
    10  *  
     9 * 
    1110 * This class is used to store information on how 
    1211 * an attribute of a specific tag should be handled. 
    13  *  
     12 * 
    1413 * Options include 
    1514 * - Type of the Attribute 
     
    1716 * - Setter method to set the attribute 
    1817 * - Whether the attribute is required, or not 
    19  *  
     18 * 
    2019 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    2120 */ 
    2221class AttributeDefinition implements Definition 
    2322{ 
    24          
     23 
    2524    /** 
    2625     * name of the attribute 
     
    2928     */ 
    3029        private $name         = null; 
    31      
     30 
    3231    /** 
    3332     * Type of the attribute 
    34      *  
     33     * 
    3534     * @var  string 
    3635     */ 
    3736        private $type         = null; 
    38      
     37 
    3938    /** 
    4039     * Name of the setter method 
    41      *  
     40     * 
    4241     * @var  string 
    4342     */ 
    4443        private $setter       = null; 
    45      
     44 
    4645    /** 
    4746     * Default value 
    48      *  
     47     * 
    4948     * @var  string 
    5049     */ 
    5150    private $defaultValue = null; 
    52          
     51 
    5352    /** 
    5453     * Whether the attribute is required 
    55      *  
     54     * 
    5655     * @var  boolean 
    5756     */ 
    5857    private $required     = false; 
    59      
     58 
    6059    /** 
    6160     * Converter used to convert the attribute 
    62      *  
     61     * 
    6362     * @param  ValueConverter 
    6463     */ 
    6564    private $valueConverter; 
    66      
     65 
    6766        /** 
    6867         * create a new attribute definition for a String attribute 
    69          *  
     68         * 
    7069         * @param   string  $name  name of the attribute 
    7170         * @param   string  $type  optional  type of the tag 
     
    7776                        throw new XJConfException('TagDefinition needs a name.'); 
    7877                } 
    79                  
     78 
    8079                $this->name = $name; 
    8180                if (null == $type || strlen($type) == 0) { 
     
    8483                    $this->type = $type; 
    8584                } 
    86                  
    87                 if (in_array($this->getType(), get_declared_classes()) == false) { 
    88                     $this->valueConverter = new PrimitiveValueConverter($this->type); 
    89                 } else { 
    90                     $this->valueConverter = new ObjectValueConverter($this->type); 
    91                 } 
    92         } 
    93      
     85        } 
     86 
    9487    /** 
    9588     * Set the default value for the attribute. 
    96      *  
     89     * 
    9790     * @param    defaultValue   default value that will be used, if a tag does not provide the attribute 
    9891     * @see      getDefault() 
     
    10598    /** 
    10699     * Get the default value of the attribute. 
    107      *  
     100     * 
    108101     * @return  string 
    109102     * @see     setDefault() 
     
    113106        return $this->defaultValue; 
    114107    } 
    115      
     108 
    116109    /** 
    117110     * returns whether the attribute is required or not 
    118      *  
     111     * 
    119112     * @return  boolean 
    120113     */ 
     
    125118    /** 
    126119     * set if attribute is required 
    127      *  
     120     * 
    128121     * @param  boolean  $required 
    129122     */ 
     
    135128        /** 
    136129         * Set the setter method 
    137      *  
     130     * 
    138131     * If no setter method is specified, the standard 
    139      * name "setAttributename()" will be used instead.  
    140          *  
     132     * name "setAttributename()" will be used instead. 
     133         * 
    141134         * @param  string  $setter  name of the setter method 
    142135         * @see    getSetterMethod() 
     
    146139                $this->setter = $setter; 
    147140        } 
    148          
    149         /**     
     141 
     142        /** 
    150143         * Get the name of the setter method that should be used 
    151144     * to set the attribute value in the parent container 
    152          *  
     145         * 
    153146         * @return  string 
    154147     * @see     setSetterMethod() 
     
    159152                        return 'set' . ucfirst($this->name); 
    160153                } 
    161                  
     154 
    162155                return $this->setter; 
    163156        } 
    164          
     157 
    165158        /** 
    166159         * Get the name of the attribute. 
    167          *  
     160         * 
    168161         * @return  string 
    169162         */ 
     
    172165                return $this->name; 
    173166        } 
    174          
     167 
    175168    /** 
    176169     * Get the type of the attribute 
    177      *  
     170     * 
    178171     * @param   Tag     $tag 
    179172     * @return  string 
     
    186179        /** 
    187180         * Get the type of the attribute 
    188          *  
     181         * 
    189182         * @return  string 
    190183         */ 
     
    193186                return $this->type; 
    194187        } 
    195          
     188 
    196189        /** 
    197190         * Convert a value to the defined type 
    198          *  
     191         * 
    199192         * The value you pass in will be cast to a 
    200193         * String before it is converted to the defined 
    201194         * type. 
    202      *  
     195     * 
    203196     * The type of the returned value can be specified in 
    204197     * the constructor using the type argument. 
     
    220213                throw new MissingAttributeException("The attribute '" + this.name + "' is required for the tag '" + tag.getName() + "'."); 
    221214            } 
    222              
     215 
    223216            // it's useless to create an instance passing null to the constructor. 
    224217            return null; 
    225218        } 
    226          
    227                 $instance = $this->valueConverter->convertValue(array($value)); 
     219 
     220                $instance = $this->getValueConverter()->convertValue(array($value)); 
    228221                return $instance; 
    229222        } 
    230223 
    231224    /** 
     225     * Get the value converter for this tag 
     226     * 
     227     * @return  ValueConverter 
     228     */ 
     229    private function getValueConverter() 
     230    { 
     231        if ($this->valueConverter == null) { 
     232            $this->valueConverter = ValueConverterFactoryChain::getFactory($this)->createValueConverter($this); 
     233        } 
     234        return $this->valueConverter; 
     235    } 
     236 
     237    /** 
    232238     * Add a child definition 
    233239     * 
    234240     * Attributes can not have any children. 
    235      *  
     241     * 
    236242     * @param  Definition  $def 
    237243     */ 
    238244    public function addChildDefinition(Definition $def) 
    239245    { 
    240         // attributes can not have any children.  
     246        // attributes can not have any children. 
     247    } 
     248 
     249    /** 
     250     * Return all child definitions. 
     251     * 
     252     * Currently, it is not possible to add any child 
     253     * definitions to an attribute 
     254     * 
     255     * @return array 
     256     */ 
     257    public function getChildDefinitions() { 
     258        return array(); 
    241259    } 
    242260} 
  • trunk/XJConf/definitions/CDataDefinition.php

    r5 r6  
    133133                return $this->type; 
    134134        } 
     135 
     136    /** 
     137     * Return all child definitions. 
     138     * 
     139     * Currently, it is not possible to add any child 
     140     * definitions to cdata 
     141     * 
     142     * @return array 
     143     */ 
     144    public function getChildDefinitions() { 
     145        return array(); 
     146    } 
    135147} 
    136148?> 
  • trunk/XJConf/definitions/ChildDefinition.php

    r5 r6  
    101101    } 
    102102 
     103    /** 
     104     * Return all child definitions. 
     105     * 
     106     * Currently, it is not possible to add any child 
     107     * definitions to a child 
     108     * 
     109     * @return array 
     110     */ 
     111    public function getChildDefinitions() { 
     112        return array(); 
     113    } 
     114 
    103115        /** 
    104116         * get the type of the child 
  • trunk/XJConf/definitions/ConstructorDefinition.php

    r5 r6  
    100100    } 
    101101 
     102    /** 
     103     * Return all child definitions. 
     104     * 
     105     * @return array 
     106     */ 
     107    public function getChildDefinitions() { 
     108        return $this->params; 
     109    } 
     110 
    102111        /** 
    103112         * get the type of the constructor 
  • trunk/XJConf/definitions/Definition.php

    r5 r6  
    5252     */ 
    5353        public function getType(); 
     54 
     55        /** 
     56         * Get all child definitions of the definition 
     57         * 
     58         * @return array 
     59         */ 
     60        public function getChildDefinitions(); 
    5461} 
    5562?> 
  • trunk/XJConf/definitions/FactoryMethodDefinition.php

    r5 r6  
    9898        } 
    9999 
     100    /** 
     101     * Return all child definitions. 
     102     * 
     103     * @return array 
     104     */ 
     105    public function getChildDefinitions() { 
     106        return $this->params; 
     107    } 
     108 
    100109        /** 
    101110         * get the type of the factory method 
  • trunk/XJConf/definitions/TagDefinition.php

    r5 r6  
    392392 
    393393    /** 
     394     * Return all child definitions. 
     395     * 
     396     * @return array 
     397     */ 
     398    public function getChildDefinitions() { 
     399        $children = $this->atts; 
     400        if ($this->factoryMethod instanceof Definition) { 
     401            $children[] = $this->factoryMethod; 
     402        } 
     403        if ($this->constructor instanceof Definition) { 
     404            $children[] = $this->constructor; 
     405        } 
     406        if ($this->cdata instanceof Definition) { 
     407            $children[] = $this->cdata; 
     408        } 
     409        return $children; 
     410    } 
     411 
     412    /** 
    394413     * Get the value converter for this tag 
    395414     * 
     
    398417    private function getValueConverter() 
    399418    { 
    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; 
     419        if ($this->valueConverter == null) { 
     420            $this->valueConverter = ValueConverterFactoryChain::getFactory($this)->createValueConverter($this); 
     421        } 
     422        return $this->valueConverter; 
    412423    } 
    413424}