/** * Transform an XML string into a XML-RPC native value * * @param string|SimpleXMLElement $xml A SimpleXMLElement object represent the XML string * It can be also a valid XML string for convertion * * @return Zend_XmlRpc_Value * @static */ protected static function _xmlStringToNativeXmlRpc($xml) { self::_createSimpleXMLElement($xml); self::_extractTypeAndValue($xml, $type, $value); switch ($type) { // All valid and known XML-RPC native values case self::XMLRPC_TYPE_I4: // Fall through to the next case // Fall through to the next case case self::XMLRPC_TYPE_INTEGER: #require_once 'Zend/XmlRpc/Value/Integer.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Integer($value); break; case self::XMLRPC_TYPE_APACHEI8: // Fall through to the next case // Fall through to the next case case self::XMLRPC_TYPE_I8: #require_once 'Zend/XmlRpc/Value/BigInteger.php'; $xmlrpcValue = new Zend_XmlRpc_Value_BigInteger($value); break; case self::XMLRPC_TYPE_DOUBLE: #require_once 'Zend/XmlRpc/Value/Double.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Double($value); break; case self::XMLRPC_TYPE_BOOLEAN: #require_once 'Zend/XmlRpc/Value/Boolean.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Boolean($value); break; case self::XMLRPC_TYPE_STRING: #require_once 'Zend/XmlRpc/Value/String.php'; $xmlrpcValue = new Zend_XmlRpc_Value_String($value); break; case self::XMLRPC_TYPE_DATETIME: // The value should already be in a iso8601 format #require_once 'Zend/XmlRpc/Value/DateTime.php'; $xmlrpcValue = new Zend_XmlRpc_Value_DateTime($value); break; case self::XMLRPC_TYPE_BASE64: // The value should already be base64 encoded #require_once 'Zend/XmlRpc/Value/Base64.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Base64($value, true); break; case self::XMLRPC_TYPE_NIL: // Fall through to the next case // Fall through to the next case case self::XMLRPC_TYPE_APACHENIL: // The value should always be NULL #require_once 'Zend/XmlRpc/Value/Nil.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Nil(); break; case self::XMLRPC_TYPE_ARRAY: // PHP 5.2.4 introduced a regression in how empty($xml->value) // returns; need to look for the item specifically $data = null; foreach ($value->children() as $key => $value) { if ('data' == $key) { $data = $value; break; } } if (null === $data) { #require_once 'Zend/XmlRpc/Value/Exception.php'; throw new Zend_XmlRpc_Value_Exception('Invalid XML for XML-RPC native ' . self::XMLRPC_TYPE_ARRAY . ' type: ARRAY tag must contain DATA tag'); } $values = array(); // Parse all the elements of the array from the XML string // (simple xml element) to Zend_XmlRpc_Value objects foreach ($data->value as $element) { $values[] = self::_xmlStringToNativeXmlRpc($element); } #require_once 'Zend/XmlRpc/Value/Array.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Array($values); break; case self::XMLRPC_TYPE_STRUCT: $values = array(); // Parse all the memebers of the struct from the XML string // (simple xml element) to Zend_XmlRpc_Value objects foreach ($value->member as $member) { // @todo? If a member doesn't have a <value> tag, we don't add it to the struct // Maybe we want to throw an exception here ? if (!isset($member->value) or !isset($member->name)) { continue; //throw new Zend_XmlRpc_Value_Exception('Member of the '. self::XMLRPC_TYPE_STRUCT .' XML-RPC native type must contain a VALUE tag'); } $values[(string) $member->name] = self::_xmlStringToNativeXmlRpc($member->value); } #require_once 'Zend/XmlRpc/Value/Struct.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Struct($values); break; default: #require_once 'Zend/XmlRpc/Value/Exception.php'; throw new Zend_XmlRpc_Value_Exception('Value type \'' . $type . '\' parsed from the XML string is not a known XML-RPC native type'); break; } $xmlrpcValue->_setXML($xml->asXML()); return $xmlrpcValue; }
/** * Transform an XML string into a XML-RPC native value * * @param string|SimpleXMLElement $simple_xml A SimpleXMLElement object represent the XML string * It can be also a valid XML string for convertion * * @return Zend_XmlRpc_Value * @static */ private static function _xmlStringToNativeXmlRpc($simple_xml) { if (!$simple_xml instanceof SimpleXMLElement) { try { $simple_xml = @new SimpleXMLElement($simple_xml); } catch (Exception $e) { // The given string is not a valid XML throw new Zend_XmlRpc_Value_Exception('Failed to create XML-RPC value from XML string: ' . $e->getMessage(), $e->getCode()); } } // Get the key (tag name) and value from the simple xml object and convert the value to an XML-RPC native value list($type, $value) = each($simple_xml); if (!$type) { // If no type was specified, the default is string $type = self::XMLRPC_TYPE_STRING; } switch ($type) { // All valid and known XML-RPC native values case self::XMLRPC_TYPE_I4: // Fall through to the next case // Fall through to the next case case self::XMLRPC_TYPE_INTEGER: $xmlrpc_val = new Zend_XmlRpc_Value_Integer($value); break; case self::XMLRPC_TYPE_DOUBLE: $xmlrpc_val = new Zend_XmlRpc_Value_Double($value); break; case self::XMLRPC_TYPE_BOOLEAN: $xmlrpc_val = new Zend_XmlRpc_Value_Boolean($value); break; case self::XMLRPC_TYPE_STRING: $xmlrpc_val = new Zend_XmlRpc_Value_String($value); break; case self::XMLRPC_TYPE_DATETIME: // The value should already be in a iso8601 format $xmlrpc_val = new Zend_XmlRpc_Value_DateTime($value); break; case self::XMLRPC_TYPE_BASE64: // The value should already be base64 encoded $xmlrpc_val = new Zend_XmlRpc_Value_Base64($value, true); break; case self::XMLRPC_TYPE_NIL: // The value should always be NULL $xmlrpc_val = new Zend_XmlRpc_Value_Nil(); break; case self::XMLRPC_TYPE_ARRAY: // If the XML is valid, $value must be an SimpleXML element and contain the <data> tag if (!$value instanceof SimpleXMLElement) { throw new Zend_XmlRpc_Value_Exception('XML string is invalid for XML-RPC native ' . self::XMLRPC_TYPE_ARRAY . ' type'); } // PHP 5.2.4 introduced a regression in how empty($xml->value) // returns; need to look for the item specifically $data = null; foreach ($value->children() as $key => $value) { if ('data' == $key) { $data = $value; break; } } if (null === $data) { throw new Zend_XmlRpc_Value_Exception('Invalid XML for XML-RPC native ' . self::XMLRPC_TYPE_ARRAY . ' type: ARRAY tag must contain DATA tag'); } $values = array(); // Parse all the elements of the array from the XML string // (simple xml element) to Zend_XmlRpc_Value objects foreach ($data->value as $element) { $values[] = self::_xmlStringToNativeXmlRpc($element); } $xmlrpc_val = new Zend_XmlRpc_Value_Array($values); break; case self::XMLRPC_TYPE_STRUCT: // If the XML is valid, $value must be an SimpleXML if (!$value instanceof SimpleXMLElement) { throw new Zend_XmlRpc_Value_Exception('XML string is invalid for XML-RPC native ' . self::XMLRPC_TYPE_STRUCT . ' type'); } $values = array(); // Parse all the memebers of the struct from the XML string // (simple xml element) to Zend_XmlRpc_Value objects foreach ($value->member as $member) { // @todo? If a member doesn't have a <value> tag, we don't add it to the struct // Maybe we want to throw an exception here ? if (!$member->value instanceof SimpleXMLElement || empty($member->value)) { continue; //throw new Zend_XmlRpc_Value_Exception('Member of the '. self::XMLRPC_TYPE_STRUCT .' XML-RPC native type must contain a VALUE tag'); } $values[(string) $member->name] = self::_xmlStringToNativeXmlRpc($member->value); } $xmlrpc_val = new Zend_XmlRpc_Value_Struct($values); break; default: throw new Zend_XmlRpc_Value_Exception('Value type \'' . $type . '\' parsed from the XML string is not a known XML-RPC native type'); break; } $xmlrpc_val->_setXML($simple_xml->asXML()); return $xmlrpc_val; }
/** * Transform an XML string into a XML-RPC native value * * @param string|SimpleXMLElement $xml A SimpleXMLElement object represent the XML string * It can be also a valid XML string for convertion * * @return Zend_XmlRpc_Value * @static */ private static function _xmlStringToNativeXmlRpc($xml) { if (!$xml instanceof SimpleXMLElement) { try { $xml = new SimpleXMLElement($xml); } catch (Exception $e) { // The given string is not a valid XML #require_once 'Zend/XmlRpc/Value/Exception.php'; throw new Zend_XmlRpc_Value_Exception('Failed to create XML-RPC value from XML string: ' . $e->getMessage(), $e->getCode()); } } $type = null; $value = null; list($type, $value) = each($xml); if (!$type and $value === null) { $namespaces = array('ex' => 'http://ws.apache.org/xmlrpc/namespaces/extensions'); foreach ($namespaces as $namespaceName => $namespaceUri) { $namespaceXml = $xml->children($namespaceUri); list($type, $value) = each($namespaceXml); if ($type !== null) { $type = $namespaceName . ':' . $type; break; } } } if (!$type) { // If no type was specified, the default is string $type = self::XMLRPC_TYPE_STRING; } switch ($type) { // All valid and known XML-RPC native values case self::XMLRPC_TYPE_I4: // Fall through to the next case // Fall through to the next case case self::XMLRPC_TYPE_INTEGER: #require_once 'Zend/XmlRpc/Value/Integer.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Integer($value); break; case self::XMLRPC_TYPE_APACHEI8: // Fall through to the next case // Fall through to the next case case self::XMLRPC_TYPE_I8: #require_once 'Zend/XmlRpc/Value/BigInteger.php'; $xmlrpcValue = new Zend_XmlRpc_Value_BigInteger($value); break; case self::XMLRPC_TYPE_DOUBLE: #require_once 'Zend/XmlRpc/Value/Double.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Double($value); break; case self::XMLRPC_TYPE_BOOLEAN: #require_once 'Zend/XmlRpc/Value/Boolean.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Boolean($value); break; case self::XMLRPC_TYPE_STRING: #require_once 'Zend/XmlRpc/Value/String.php'; $xmlrpcValue = new Zend_XmlRpc_Value_String($value); break; case self::XMLRPC_TYPE_DATETIME: // The value should already be in a iso8601 format #require_once 'Zend/XmlRpc/Value/DateTime.php'; $xmlrpcValue = new Zend_XmlRpc_Value_DateTime($value); break; case self::XMLRPC_TYPE_BASE64: // The value should already be base64 encoded #require_once 'Zend/XmlRpc/Value/Base64.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Base64($value, true); break; case self::XMLRPC_TYPE_NIL: // Fall through to the next case // Fall through to the next case case self::XMLRPC_TYPE_APACHENIL: // The value should always be NULL #require_once 'Zend/XmlRpc/Value/Nil.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Nil(); break; case self::XMLRPC_TYPE_ARRAY: // PHP 5.2.4 introduced a regression in how empty($xml->value) // returns; need to look for the item specifically $data = null; foreach ($value->children() as $key => $value) { if ('data' == $key) { $data = $value; break; } } if (null === $data) { #require_once 'Zend/XmlRpc/Value/Exception.php'; throw new Zend_XmlRpc_Value_Exception('Invalid XML for XML-RPC native ' . self::XMLRPC_TYPE_ARRAY . ' type: ARRAY tag must contain DATA tag'); } $values = array(); // Parse all the elements of the array from the XML string // (simple xml element) to Zend_XmlRpc_Value objects foreach ($data->value as $element) { $values[] = self::_xmlStringToNativeXmlRpc($element); } #require_once 'Zend/XmlRpc/Value/Array.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Array($values); break; case self::XMLRPC_TYPE_STRUCT: $values = array(); // Parse all the memebers of the struct from the XML string // (simple xml element) to Zend_XmlRpc_Value objects foreach ($value->member as $member) { // @todo? If a member doesn't have a <value> tag, we don't add it to the struct // Maybe we want to throw an exception here ? if (!isset($member->value) or !isset($member->name)) { continue; //throw new Zend_XmlRpc_Value_Exception('Member of the '. self::XMLRPC_TYPE_STRUCT .' XML-RPC native type must contain a VALUE tag'); } $values[(string) $member->name] = self::_xmlStringToNativeXmlRpc($member->value); } #require_once 'Zend/XmlRpc/Value/Struct.php'; $xmlrpcValue = new Zend_XmlRpc_Value_Struct($values); break; default: #require_once 'Zend/XmlRpc/Value/Exception.php'; throw new Zend_XmlRpc_Value_Exception('Value type \'' . $type . '\' parsed from the XML string is not a known XML-RPC native type'); break; } $xmlrpcValue->_setXML($xml->asXML()); return $xmlrpcValue; }