ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Value.php
Go to the documentation of this file.
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 
40 // }}}
41 
42 // dependencies {{{
43 require_once 'XML/RPC2/Exception.php';
44 require_once 'XML/RPC2/Value.php';
45 // }}}
46 
58 {
59  // {{{ properties
60 
66  private $_nativeValue = null;
67 
68  // }}}
69  // {{{ getNativeValue()
70 
76  public function getNativeValue()
77  {
78  return $this->_nativeValue;
79  }
80 
81  // }}}
82  // {{{ setNativeValue()
83 
89  protected function setNativeValue($value)
90  {
91  $this->_nativeValue = $value;
92  }
93 
94  // }}}
95  // {{{ createFromNative()
96 
123  public static function createFromNative($nativeValue, $explicitType = null)
124  {
125  if (is_null($explicitType)) {
126  switch (gettype($nativeValue)) {
127  case 'boolean':
128  $explicitType = 'boolean';
129  break;
130  case 'integer':
131  $explicitType = 'int';
132  break;
133  case 'double':
134  $explicitType = 'double';
135  break;
136  case 'string':
137  $explicitType = 'string';
138  break;
139  case 'array':
140  $explicitType = 'array';
141  $keys = array_keys($nativeValue);
142  if (count($keys) > 0) {
143  if ($keys[0] !== 0 && ($keys[0] !== 1)) $explicitType = 'struct';
144  $i=0;
145  do {
146  $previous = $keys[$i];
147  $i++;
148  if (array_key_exists($i, $keys) && ($keys[$i] !== $keys[$i - 1] + 1)) $explicitType = 'struct';
149  } while (array_key_exists($i, $keys) && $explicitType == 'array');
150  }
151  break;
152  case 'object':
153  if ((strtolower(get_class($nativeValue)) == 'stdclass') && (isset($nativeValue->xmlrpc_type))) {
154  // In this case, we have a "stdclass native value" (emulate xmlrpcext)
155  // the type 'base64' or 'datetime' is given by xmlrpc_type public property
156  $explicitType = $nativeValue->xmlrpc_type;
157  } else {
158  $nativeValue = serialize($nativeValue);
159  $explicitType = 'base64';
160  }
161  break;
162  case 'resource':
163  case 'NULL':
164  case 'unknown type':
165  throw new XML_RPC2_InvalidTypeEncodeException(sprintf('Impossible to encode value \'%s\' from type \'%s\'. No analogous type in XML_RPC.',
166  (string) $nativeValue,
167  gettype($nativeValue)));
168  default:
169  throw new XML_RPC2_InvalidTypeEncodeException(sprintf('Unexpected PHP native type returned by gettype: \'%s\', for value \'%s\'',
170  gettype($nativeValue),
171  (string) $nativeValue));
172  }
173  }
174  $explicitType = ucfirst(strtolower($explicitType));
175  switch ($explicitType) {
176  case 'I4':
177  case 'Int':
178  case 'Boolean':
179  case 'Double':
180  case 'String':
181  require_once 'XML/RPC2/Backend/Php/Value/Scalar.php';
183  break;
184  case 'Datetime.iso8601':
185  case 'Datetime':
186  require_once 'XML/RPC2/Backend/Php/Value/Datetime.php';
187  return new XML_RPC2_Backend_Php_Value_Datetime($nativeValue);
188  break;
189  case 'Base64':
190  require_once 'XML/RPC2/Backend/Php/Value/Base64.php';
191  return new XML_RPC2_Backend_Php_Value_Base64($nativeValue);
192  break;
193  case 'Array':
194  require_once 'XML/RPC2/Backend/Php/Value/Array.php';
195  return new XML_RPC2_Backend_Php_Value_Array($nativeValue);
196  break;
197  case 'Struct':
198  require_once 'XML/RPC2/Backend/Php/Value/Struct.php';
199  return new XML_RPC2_Backend_Php_Value_Struct($nativeValue);
200  break;
201  default:
202  throw new XML_RPC2_InvalidTypeEncodeException(sprintf('Unexpected explicit encoding type \'%s\'', $explicitType));
203  }
204  }
205 
206  // }}}
207  // {{{ createFromDecode()
208 
215  public static function createFromDecode($simpleXML)
216  {
217  // TODO Remove reparsing of XML fragment, when SimpleXML proves more solid. Currently it segfaults when
218  // xpath is used both in an element and in one of its children
219  $simpleXML = simplexml_load_string($simpleXML->asXML());
220 
221  $valueType = $simpleXML->xpath('./*');
222  if (count($valueType) == 1) { // Usually we must check the node name
223  $nodename = dom_import_simplexml($valueType[0])->nodeName;
224  switch ($nodename) {
225  case 'i4':
226  case 'int':
227  $nativeType = 'Integer';
228  break;
229  case 'boolean':
230  $nativeType = 'Boolean';
231  break;
232  case 'double':
233  $nativeType = 'Double';
234  break;
235  case 'string':
236  $nativeType = 'String';
237  break;
238  case 'dateTime.iso8601':
239  $nativeType = 'Datetime';
240  break;
241  case 'base64':
242  $nativeType = 'Base64';
243  break;
244  case 'array':
245  $nativeType = 'Array';
246  break;
247  case 'struct':
248  $nativeType = 'Struct';
249  break;
250  default:
251  throw new XML_RPC2_DecodeException(sprintf('Unable to decode XML-RPC value. Value type is not recognized \'%s\'', $nodename));
252  }
253  } elseif (count($valueType) == 0) { // Default type is string
254  $nodename = null;
255  $nativeType = 'String';
256  } else {
257  throw new XML_RPC2_DecodeException(sprintf('Unable to decode XML-RPC value. Value presented %s type nodes: %s.', count($valueType), $simpleXML->asXML()));
258  }
259  require_once(sprintf('XML/RPC2/Backend/Php/Value/%s.php', $nativeType));
260  $nativeType = 'XML_RPC2_Backend_Php_Value_' . $nativeType;
261  return self::createFromNative(@call_user_func(array($nativeType, 'decode'), $simpleXML), $nodename);
262  }
263 
264  // }}}
265  // {{{ encode()
266 
272  // Declaration commented because of: http://pear.php.net/bugs/bug.php?id=8499
273 // public abstract function encode();
274 
275  // }}}
276  // {{{ decode()
277 
283  // Declaration commented because of: http://pear.php.net/bugs/bug.php?id=8499
284 // public static abstract function decode($xml);
285 
286  // }}}
287 }
288 
289 ?>