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: Scalar.php 308633 2011-02-24 18:54:23Z sergiosgc $
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 scalar value abstract class. All XML_RPC value classes representing scalar types inherit from XML_RPC2_Value_Scalar
49
 *
50
 * @category   XML
51
 * @package    XML_RPC2
52
 * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>
53
 * @copyright  2004-2006 Sergio Carvalho
54
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
55
 * @link       http://pear.php.net/package/XML_RPC2
56
 */
57
abstract class XML_RPC2_Backend_Php_Value_Scalar extends XML_RPC2_Backend_Php_Value
58
{
59
 
60
    // {{{ properties
61
 
62
    /**
63
     * scalar type
64
     *
65
     * @var string
66
     */
67
    private $_scalarType = null;
68
 
69
    // }}}
70
    // {{{ setScalarType()
71
 
72
    /**
73
     * scalarType property setter
74
     *
75
     * @param mixed value The new scalarType
76
     */
77
    protected function setScalarType($value)
78
    {
79
        switch ($value) {
80
            case 'nil':
81
            case 'int':
82
            case 'i8':
83
            case 'i4':
84
            case 'boolean':
85
            case 'string':
86
            case 'double':
87
            case 'dateTime.iso8601':
88
            case 'base64':
89
                $this->_scalarType = $value;
90
                break;
91
            default:
92
                throw new XML_RPC2_InvalidTypeException(sprintf('Type \'%s\' is not an XML-RPC scalar type', $value));
93
        }
94
    }
95
 
96
    // }}}
97
    // {{{ getScalarType()
98
 
99
    /**
100
     * scalarType property getter
101
     *
102
     * @return mixed The current scalarType
103
     */
104
    public function getScalarType()
105
    {
106
        return $this->_scalarType;
107
    }
108
 
109
    // }}}
110
    // {{{ constructor
111
 
112
    /**
113
     * Constructor. Will build a new XML_RPC2_Value_Scalar with the given nativeValue
114
     *
115
     * @param mixed nativeValue
116
     */
117
    public function __construct($scalarType, $nativeValue)
118
    {
119
        $this->setScalarType($scalarType);
120
        $this->setNativeValue($nativeValue);
121
    }
122
 
123
    // }}}
124
    // {{{ createFromNative()
125
 
126
    /**
127
     * Choose a XML_RPC2_Value subclass appropriate for the
128
     * given value and create it.
129
     *
130
     * @param string The native value
131
     * @param string Optinally, the scalar type to use
132
     * @throws XML_RPC2_InvalidTypeEncodeException When native value's type is not a native type
133
     * @return XML_RPC2_Value The newly created value
134
     */
135
    public static function createFromNative($nativeValue, $explicitType = null)
136
    {
137
        if (is_null($explicitType)) {
138
            switch (gettype($nativeValue)) {
139
                case 'integer':
140
                    $explicitType = $nativeValue <= 2147483647 /* PHP_INT_MAX on 32 bit systems */ ? gettype($nativeValue) : 'Integer64';
141
                    break;
142
                case 'NULL':
143
                    $explicitType = 'Nil';
144
                    break;
145
                case 'boolean':
146
                case 'double':
147
                case 'string':
148
                    $explicitType = gettype($nativeValue);
149
                    break;
150
                default:
151
                    throw new XML_RPC2_InvalidTypeEncodeException(sprintf('Impossible to encode scalar value \'%s\' from type \'%s\'. Native type is not a scalar XML_RPC type (boolean, integer, double, string)',
152
                        (string) $nativeValue,
153
                        gettype($nativeValue)));
154
            }
155
        }
156
        $explicitType = ucfirst(strtolower($explicitType));
157
        require_once(sprintf('XML/RPC2/Backend/Php/Value/%s.php', $explicitType));
158
        $explicitType = sprintf('XML_RPC2_Backend_Php_Value_%s', $explicitType);
159
        return new $explicitType($nativeValue);
160
    }
161
 
162
    // }}}
163
    // {{{ encode()
164
 
165
    /**
166
     * Encode the instance into XML, for transport
167
     *
168
     * @return string The encoded XML-RPC value,
169
     */
170
    public function encode()
171
    {
172
        return '<' . $this->getScalarType() . '>' . $this->getNativeValue() . '</' . $this->getScalarType() . '>';
173
    }
174
 
175
    // }}}
176
 
177
}
178
 
179
?>