Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
4
 
5
// LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{
6
 
7
/**
8
* +-----------------------------------------------------------------------------+
9
* | Copyright (c) 2004-2006 Sergio Goncalves Carvalho                                |
10
* +-----------------------------------------------------------------------------+
11
* | This file is part of XML_RPC2.                                              |
12
* |                                                                             |
13
* | XML_RPC2 is free software; you can redistribute it and/or modify            |
14
* | it under the terms of the GNU Lesser General Public License as published by |
15
* | the Free Software Foundation; either version 2.1 of the License, or         |
16
* | (at your option) any later version.                                         |
17
* |                                                                             |
18
* | XML_RPC2 is distributed in the hope that it will be useful,                 |
19
* | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
20
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
21
* | GNU Lesser General Public License for more details.                         |
22
* |                                                                             |
23
* | You should have received a copy of the GNU Lesser General Public License    |
24
* | along with XML_RPC2; if not, write to the Free Software                     |
25
* | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
26
* | 02111-1307 USA                                                              |
27
* +-----------------------------------------------------------------------------+
28
* | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com>                  |
29
* +-----------------------------------------------------------------------------+
30
*
31
* @category   XML
32
* @package    XML_RPC2
33
* @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>
34
* @copyright  2004-2006 Sergio Carvalho
35
* @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
36
* @version    CVS: $Id: Array.php 205683 2006-01-22 02:00:41Z fab $
37
* @link       http://pear.php.net/package/XML_RPC2
38
*/
39
 
40
// }}}
41
 
42
// dependencies {{{
43
require_once 'XML/RPC2/Exception.php';
44
require_once 'XML/RPC2/Backend/Php/Value.php';
45
// }}}
46
 
47
/**
48
 * XML_RPC array value class. Represents values of type array
49
 *
50
 * @author Sergio Carvalho
51
 * @package XML_RPC2
52
 */
53
class XML_RPC2_Backend_Php_Value_Array extends XML_RPC2_Backend_Php_Value
54
{
55
 
56
    // {{{ setNativeValue()
57
 
58
    /**
59
     * nativeValue property setter
60
     *
61
     * @param mixed value the new nativeValue
62
     */
63
    protected function setNativeValue($value)
64
    {
65
        if (!is_array($value)) {
66
            throw new XML_RPC2_InvalidTypeException(sprintf('Cannot create XML_RPC2_Value_Array from type \'%s\'.', gettype($nativeValue)));
67
        }
68
        parent::setNativeValue($value);
69
    }
70
 
71
    // }}}
72
    // {{{ constructor
73
 
74
    /**
75
     * Constructor. Will build a new XML_RPC2_Backend_Php_Value_Array with the given nativeValue
76
     *
77
     * @param mixed nativeValue
78
     */
79
    public function __construct($nativeValue)
80
    {
81
        $this->setNativeValue($nativeValue);
82
    }
83
 
84
    // }}}
85
    // {{{ encode()
86
 
87
    /**
88
     * Encode the instance into XML, for transport
89
     *
90
     * @return string The encoded XML-RPC value,
91
     */
92
    public function encode()
93
    {
94
        $result = '<array><data>';
95
        foreach($this->getNativeValue() as $element) {
96
            $result .= '<value>';
97
            $result .= ($element instanceof XML_RPC2_Backend_Php_Value) ?
98
                        $element->encode() :
99
                        XML_RPC2_Backend_Php_Value::createFromNative($element)->encode();
100
            $result .= '</value>';
101
        }
102
        $result .= '</data></array>';
103
        return $result;
104
    }
105
 
106
    // }}}
107
    // {{{ decode()
108
 
109
    /**
110
     * Decode transport XML and set the instance value accordingly
111
     *
112
     * @param mixed The encoded XML-RPC value,
113
     */
114
    public static function decode($xml)
115
    {
116
        // TODO Remove reparsing of XML fragment, when SimpleXML proves more solid. Currently it segfaults when
117
        // xpath is used both in an element and in one of its children
118
        $xml = simplexml_load_string($xml->asXML());
119
        $values = $xml->xpath('/value/array/data/value');
120
        $result = array();
121
        foreach (array_keys($values) as $i) {
122
            $result[] = XML_RPC2_Backend_Php_Value::createFromDecode($values[$i])->getNativeValue();
123
        }
124
        return $result;
125
    }
126
 
127
    // }}}
128
 
129
}
130
 
131
?>