ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Struct.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/Backend/Php/Value.php';
45 // }}}
46 
58 {
59 
60  // {{{ setNativeValue()
61 
67  protected function setNativeValue($value)
68  {
69  if (!is_array($value)) {
70  throw new XML_RPC2_InvalidTypeException(sprintf('Cannot create XML_RPC2_Backend_Php_Value_Struct from type \'%s\'.', gettype($nativeValue)));
71  }
72  parent::setNativeValue($value);
73  }
74 
75  // }}}
76  // {{{ constructor
77 
83  public function __construct($nativeValue)
84  {
85  $this->setNativeValue($nativeValue);
86  }
87 
88  // }}}
89  // {{{ encode()
90 
96  public function encode()
97  {
98  $result = '<struct>';
99  foreach($this->getNativeValue() as $name => $element) {
100  $result .= '<member>';
101  $result .= '<name>';
102  $result .= strtr($name, array('&' => '&amp;', '<' => '&lt;', '>' => '&gt;'));
103  $result .= '</name>';
104  $result .= '<value>';
105  $result .= ($element instanceof XML_RPC2_Backend_Php_Value) ?
106  $element->encode() :
108  $result .= '</value>';
109  $result .= '</member>';
110  }
111  $result .= '</struct>';
112  return $result;
113  }
114 
115  // }}}
116  // {{{ decode()
117 
123  public static function decode($xml)
124  {
125  // TODO Remove reparsing of XML fragment, when SimpleXML proves more solid. Currently it segfaults when
126  // xpath is used both in an element and in one of its children
127  $xml = simplexml_load_string($xml->asXML());
128  $values = $xml->xpath('/value/struct/member');
129  $result = array();
130  foreach (array_keys($values) as $i) {
131  $result[(string) $values[$i]->name] = XML_RPC2_Backend_Php_Value::createFromDecode($values[$i]->value)->getNativeValue();
132  }
133  return $result;
134  }
135 
136  // }}}
137 
138 }
139 
140 ?>