Changeset 25

Show
Ignore:
Timestamp:
08/30/06 15:38:42 (2 years ago)
Author:
mikey
Message:

made ArrayValueConverter? working

Files:

Legend:

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

    r14 r25  
    55 * @author  Frank Kleine <frank.kleine@schlund.de> 
    66 */ 
    7 XJConfLoader::load('converters.ValueConverter'); 
     7XJConfLoader::load('converters.ValueConverter', 
     8                   'definitions.Definition', 
     9                   'definitions.TagDefinition' 
     10); 
    811/** 
    912 * Converter to convert a value to an array. 
     
    2225    public function convertValue(Tag $tag, Definition $def) 
    2326    { 
    24         return array(); 
     27        $return = array(); 
     28        $return = $this->addAttributesToValue($tag, $def, $return); 
     29        $return = $this->addChildrenToValue($tag, $def, $return); 
     30        return $return; 
    2531    } 
    2632 
     
    3440        return 'array'; 
    3541    } 
     42     
     43    /** 
     44     * Add all attributes using the appropriate setter methods 
     45     * 
     46     * @param   Tag         $tag 
     47     * @param   Definition  $def 
     48     * @param   array       $array 
     49     */ 
     50    protected function addAttributesToValue(Tag $tag, TagDefinition $def, $array) 
     51    { 
     52        // set all attributes 
     53        foreach ($def->getAttributes() as $att) { 
     54            $val = $att->convertValue($tag); 
     55            // attribute has not been set and there is no 
     56            // default value, skip the method call 
     57            if (null == $val) { 
     58                continue; 
     59            } 
     60 
     61            $array[$att->getName()] = $val; 
     62        } 
     63         
     64        return $array; 
     65    } 
     66     
     67    /** 
     68     * Add all children to the created instance 
     69     * 
     70     * @param   Tag         $tag 
     71     * @param   Definition  $def 
     72     * @param   array       $array 
     73     */ 
     74    protected function addChildrenToValue(Tag $tag, Definition $def, $array) 
     75    { 
     76        // traverse all children 
     77        $children = $tag->getChildren(); 
     78        if (count($children) == 0) { 
     79            return $array; 
     80        } 
     81         
     82        foreach ($children as $child) { 
     83            $val = $child->getConvertedValue(); 
     84            // attribute has not been set and there is no 
     85            // default value, skip the method call 
     86            if (null == $val) { 
     87                continue; 
     88            } 
     89             
     90            $childDef = $child->getDefinition(); 
     91            if ($childDef->getName() == '__none') { 
     92                $array[] = $val; 
     93            } else { 
     94                $array[$child->getName()] = $val; 
     95            } 
     96        } 
     97         
     98        return $array; 
     99    } 
    36100} 
    37101?>