Changeset 1163

Show
Ignore:
Timestamp:
12/19/07 16:58:24 (1 year ago)
Author:
mikey
Message:

added some more tests
fixed type guessing
removed possibility to set a decode function

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/xml/unserializer/stubXMLUnserializer.php

    r1162 r1163  
    167167            $guessType = $this->options[stubXMLUnserializerOption::GUESS_TYPES]; 
    168168        } 
    169  
    170         if ($this->options[stubXMLUnserializerOption::DECODE_FUNC] !== null) { 
    171             $atts = array_map($this->options[stubXMLUnserializerOption::DECODE_FUNC], $atts); 
    172         } 
    173169         
    174170        if (is_array($this->options[stubXMLUnserializerOption::TAG_MAP]) && isset($this->options[stubXMLUnserializerOption::TAG_MAP][$sName])) { 
     
    189185 
    190186            if ($this->options[stubXMLUnserializerOption::GUESS_TYPES] === true) { 
    191                 $atts = $this->guessAndSetTypes($atts); 
     187                $atts = $this->guessAndSetType($atts); 
    192188            }             
    193189            if ($this->options[stubXMLUnserializerOption::ATTRIBUTES_ARRAYKEY] != false) { 
     
    331327            if (isset($parent['children']) === false || is_array($parent['children']) === false) { 
    332328                $parent['children'] = array(); 
    333                 if (in_array($parent['type'], array('array'/*, 'object'*/)) === false) { 
    334                     $parent['type'] = 'array';#$this->getComplexType($parent['name']); 
    335                     /*if ($parent['type'] == 'object') { 
    336                         $parent['class'] = $parent['name']; 
    337                     }*/ 
     329                if ('array' !== $parent['type']) { 
     330                    $parent['type'] = 'array'; 
    338331                } 
    339332            } 
     
    378371    protected function characters($buf) 
    379372    { 
    380         if (null !== $this->options[stubXMLUnserializerOption::DECODE_FUNC]) { 
    381             $buf = call_user_func($this->options[stubXMLUnserializerOption::DECODE_FUNC], $buf); 
    382         } 
    383          
    384373        $this->dataStack[$this->depth] .= $buf; 
    385374    } 
     
    393382    protected function guessAndSetType($value) 
    394383    { 
     384        if (is_array($value) === true) { 
     385            return array_map(array($this, 'guessAndSetType'), $value); 
     386        } 
     387         
    395388        if ('true' === $value) { 
    396389            return true; 
     
    405398        } 
    406399         
    407         if (preg_match('/^[-+]?[0-9]{1,}$/', $value) !== false) { 
     400        if (preg_match('/^[-+]?[0-9]{1,}$/', $value) != false) { 
    408401            return intval($value); 
    409402        } 
    410403         
    411         if (preg_match('/^[-+]?[0-9]{1,}\.[0-9]{1,}$/', $value) !== false) { 
     404        if (preg_match('/^[-+]?[0-9]{1,}\.[0-9]{1,}$/', $value) != false) { 
    412405            return doubleval($value); 
    413406        } 
  • trunk/src/main/php/net/stubbles/xml/unserializer/stubXMLUnserializerOption.php

    r1162 r1163  
    8282    const ENCODING_SOURCE     = 'encoding'; 
    8383    /** 
    84      * option: callback that will be applied to textual data 
    85      * 
    86      * Possible values: 
    87      * - any valid PHP callback 
    88      */ 
    89     const DECODE_FUNC         = 'decodeFunction'; 
    90     /** 
    9184     * option: list of tags, that will not be used as keys 
    9285     * 
     
    137130                                             self::FORCE_LIST          => array(),                // these tags will always be an indexed array 
    138131                                             self::ENCODING_SOURCE     => null,                   // specify the encoding character of the document to parse 
    139                                              self::DECODE_FUNC         => null,                   // function used to decode data 
    140132                                             self::WHITESPACE          => self::WHITESPACE_TRIM,  // remove whitespace around data 
    141                                              self::IGNORE_KEYS         => array(),                // List of tags that will automatically be added to the parent, instead of adding a new key 
     133                                             self::IGNORE_KEYS         => array(),                // list of tags that will automatically be added to the parent, instead of adding a new key 
    142134                                             self::GUESS_TYPES         => false                   // Whether to use type guessing 
    143135                                       ); 
  • trunk/src/test/php/net/stubbles/xml/unserializer/stubXMLUnserializerTestCase.php

    r1162 r1163  
    7272        $this->assertEqual($unserializer->unserialize($xml2), array('item' => array(array('name' => 'schst')))); 
    7373    } 
     74 
     75    /** 
     76     * test that whitespace handling works as expected 
     77     */ 
     78    public function testWhiteSpaceTrim() 
     79    { 
     80        $xml = '<xml> 
     81                  <string> 
     82                    
     83                    This XML 
     84                    document 
     85                    contains 
     86                    line breaks. 
     87                 
     88                  </string> 
     89                </xml>'; 
     90        $options      = array(stubXMLUnserializerOption::WHITESPACE => stubXMLUnserializerOption::WHITESPACE_TRIM); 
     91        $unserializer = new stubXMLUnserializer($options); 
     92        $this->assertEqual($unserializer->unserialize($xml), array('string' => 'This XML 
     93                    document 
     94                    contains 
     95                    line breaks.') 
     96        ); 
     97    } 
     98 
     99    /** 
     100     * test that whitespace handling works as expected 
     101     */ 
     102    public function testWhiteSpaceNormalize() 
     103    { 
     104        $xml = '<xml> 
     105                  <string> 
     106                    
     107                    This XML 
     108                    document 
     109                    contains 
     110                    line breaks. 
     111                 
     112                  </string> 
     113                </xml>'; 
     114        $options      = array(stubXMLUnserializerOption::WHITESPACE => stubXMLUnserializerOption::WHITESPACE_NORMALIZE); 
     115        $unserializer = new stubXMLUnserializer($options); 
     116        $this->assertEqual($unserializer->unserialize($xml), array('string' => 'This XML document contains line breaks.')); 
     117    } 
     118 
     119    /** 
     120     * test that whitespace handling works as expected 
     121     */ 
     122    public function testWhiteSpaceKeep() 
     123    { 
     124        $xml = '<xml> 
     125                  <string> 
     126                    
     127                    This XML 
     128                    document 
     129                    contains 
     130                    line breaks. 
     131                 
     132                  </string> 
     133                </xml>'; 
     134        $options      = array(stubXMLUnserializerOption::WHITESPACE => stubXMLUnserializerOption::WHITESPACE_KEEP); 
     135        $unserializer = new stubXMLUnserializer($options); 
     136        $this->assertEqual($unserializer->unserialize($xml), array('string' => ' 
     137                    
     138                    This XML 
     139                    document 
     140                    contains 
     141                    line breaks. 
     142                 
     143                  ')); 
     144    } 
     145 
     146    /** 
     147     * test unserializing a list of items 
     148     */ 
     149    public function testUnserializeWithAttributes() 
     150    { 
     151        $options      = array(stubXMLUnserializerOption::ATTRIBUTES_PARSE    => true, 
     152                              stubXMLUnserializerOption::ATTRIBUTES_ARRAYKEY => false 
     153                        ); 
     154        $unserializer = new stubXMLUnserializer($options); 
     155        $this->assertEqual($unserializer->unserializeFile(TEST_SRC_PATH . '/resources/unserializer.xml'), 
     156                           array('test' => array('foo'      => 'bar', 
     157                                                 'tag'      => 'test', 
     158                                                 '_content' => 'Test' 
     159                                           ) 
     160                           ) 
     161        ); 
     162    } 
     163 
     164    /** 
     165     * test unserializing a list of items 
     166     */ 
     167    public function testUnserializeWithTagMap() 
     168    { 
     169        $xml1         = '<root>' . 
     170                        '  <foo>FOO</foo>' . 
     171                        '  <bar>BAR</bar>' . 
     172                        '</root>'; 
     173        $xml2         = '<root>' . 
     174                        '  <foo>'. 
     175                        '    <tomato>45</tomato>'. 
     176                        '  </foo>'. 
     177                        '  <bar>'. 
     178                        '    <tomato>31</tomato>'. 
     179                        '  </bar>'. 
     180                        '</root>'; 
     181        $options      = array(stubXMLUnserializerOption::TAG_MAP => array('foo' => 'bar', 
     182                                                                          'bar' => 'foo' 
     183                                                                    ) 
     184                        ); 
     185        $unserializer = new stubXMLUnserializer($options); 
     186        $this->assertEqual($unserializer->unserialize($xml1), array('bar' => 'FOO', 
     187                                                                    'foo' => 'BAR' 
     188                                                              ) 
     189        ); 
     190        $this->assertEqual($unserializer->unserialize($xml2), array('bar' => array('tomato' => 45), 
     191                                                                    'foo' => array('tomato' => 31) 
     192                                                              ) 
     193        ); 
     194    } 
     195 
     196    /** 
     197     * test unserializing a list of items 
     198     */ 
     199    public function testUnserializeWithTypeGuessing() 
     200    { 
     201        $xml          = '<root>' . 
     202                        '  <string>Just a string...</string>' . 
     203                        '  <booleanValue>true</booleanValue>' . 
     204                        '  <foo>-563</foo>' . 
     205                        '  <bar>4.73736</bar>' . 
     206                        '  <array foo="false" bar="12">true</array>' . 
     207                        '</root>'; 
     208        $options      = array(stubXMLUnserializerOption::ATTRIBUTES_PARSE => true, 
     209                              stubXMLUnserializerOption::GUESS_TYPES      => true 
     210                        ); 
     211        $unserializer = new stubXMLUnserializer($options); 
     212        $result       = $unserializer->unserialize($xml); 
     213        $this->assertEqual($result, array('string'       => 'Just a string...', 
     214                                          'booleanValue' => true, 
     215                                          'foo'          => -563, 
     216                                          'bar'          => 4.73736, 
     217                                          'array'        => array('foo'      => false, 
     218                                                                  'bar'      => 12, 
     219                                                                  '_content' => true 
     220                                                             ) 
     221                                    ) 
     222        ); 
     223        $this->assertTrue($result['booleanValue']); 
     224        $this->assertTrue(is_int($result['foo'])); 
     225        $this->assertTrue(is_float($result['bar'])); 
     226        $this->assertFalse($result['array']['foo']); 
     227        $this->assertTrue(is_int($result['array']['bar'])); 
     228        $this->assertTrue($result['array']['_content']); 
     229    } 
    74230} 
    75231?>