/** * Recursive function to create payload * @param DomDocument $payload_dom * @param array $sig_model as a DomNode * example of sig.. * * <params wrapper-element="myDemo" * wrapper-element-ns="http://wso2.org/dyn/codegen/demo" * simple="no" contentModel="all"> * <param token="#in" * targetNamespace="http://wso2.org/dyn/codegen/demo" * minOccurs="1" maxOccurs="1" name="demo1" type="int" * type-namespace="http://www.w3.org/2001/XMLSchema" * type-prefix="xs" simple="yes"/> * <param token="#in" * targetNamespace="http://wso2.org/dyn/codegen/demo" * minOccurs="1" maxOccurs="1" name="demo2" type="string" * type-namespace="http://www.w3.org/2001/XMLSchema" * type-prx="xs" simple="yes"/> * </params> * * * @param DomNode $parent_node - The parent node to add the content * @param DomNode $root_node - The top most of parent * @param mixed $user_obj - class object to pass * @param array $classmap, the classmap user entered * @param $prefix_i - next available prefix index * @param $namespace_map - Just make sure the unique namespace is used. Newly added (passed by reference) */ function wsf_create_payload_for_class_map(DomDocument $payload_dom, DomNode $sig_node, DomNode $parent_node, DomNode $root_node, $user_obj, $classmap, &$prefix_i, array &$namespace_map, $mtom_on, &$attachment_map) { ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "Loading in to creating payload from classmap"); if (wsf_set_nil_element($user_obj, $parent_node, $root_node, $prefix_i, $namespace_map)) { return TRUE; } // This is not expected in the class map mode to have params with no childs // So mark it as an unknown schema // here we always expect structures with childs unless it is simple content model if (!($sig_node->attributes->getNamedItem(WSF_CONTENT_MODEL) && $sig_node->attributes->getNamedItem(WSF_CONTENT_MODEL)->value == WSF_SIMPLE_CONTENT) && !$sig_node->hasChildNodes()) { // Just take the first namespace in the map to create the xml elements in // the unknown structure.. $values = array_values($namespace_map); $prefix = $values[0]; wsf_create_payload_for_unknown_class_map($payload_dom, $parent_node, $user_obj, $prefix); return; } if ($sig_node->hasAttributes()) { // filling user class information to the array.. $changed_sig_node = wsf_infer_sig_node_from_user_obj($sig_node, $parent_node, $root_node, $user_obj, $classmap, $prefix_i, $namespace_map); $user_arguments = wsf_convert_classobj_to_array($changed_sig_node, $user_obj); wsf_build_content_model($changed_sig_node, $user_arguments, $parent_node, $payload_dom, $root_node, $classmap, $prefix_i, $namespace_map, $mtom_on, $attachment_map); } else { // this situation meets only for non-wrapped mode as doclit-bare wsdls // and for the simpleContent extension in the classmap $the_only_node = $sig_node->firstChild; // handle simple content extension seperatly if ($the_only_node->attributes->getNamedItem(WSF_CONTENT_MODEL) && $the_only_node->attributes->getNamedItem(WSF_CONTENT_MODEL)->value == WSF_SIMPLE_CONTENT) { // filling user class information to the array.. $user_arguments = wsf_convert_classobj_to_array($the_only_node, $user_obj); wsf_build_content_model($the_only_node, $user_arguments, $parent_node, $payload_dom, $root_node, $classmap, $prefix_i, $namespace_map, $mtom_on, $attachement_map); } } }
/** * Converts DomDocument to array with all attributes in it * * @param DOMDocument|DOMElement|DomNode $root * @return array */ function convertDomToArray($root) { $result = []; if ($root->hasAttributes()) { $attrs = $root->attributes; foreach ($attrs as $attr) { $result['@attributes'][$attr->name] = $attr->value; } } if ($root->hasChildNodes()) { $children = $root->childNodes; if ($children->length == 1) { $child = $children->item(0); if ($child->nodeType == XML_TEXT_NODE) { $result['_value'] = $child->nodeValue; return count($result) == 1 ? $result['_value'] : $result; } } $groups = []; foreach ($children as $child) { if (!isset($result[$child->nodeName])) { $result[$child->nodeName] = $this->convertDomToArray($child); } else { if (!isset($groups[$child->nodeName])) { $result[$child->nodeName] = array($result[$child->nodeName]); $groups[$child->nodeName] = 1; } $result[$child->nodeName][] = $this->convertDomToArray($child); } } } return $result; }
/** * parse payload and return an object hierarchy * @param $payload * @param $sig_node * example of sig.. * * <params wrapper-element="myDemo" * wrapper-element-ns="http://wso2.org/dyn/codegen/demo" * simple="no" contentModel="all"> * <param token="#in" * targetNamespace="http://wso2.org/dyn/codegen/demo" * minOccurs="1" maxOccurs="1" name="demo1" type="int" * type-namespace="http://www.w3.org/2001/XMLSchema" * type-prefix="xs" simple="yes"/> * <param token="#in" * targetNamespace="http://wso2.org/dyn/codegen/demo" * minOccurs="1" maxOccurs="1" name="demo2" type="string" * type-namespace="http://www.w3.org/2001/XMLSchema" * type-prx="xs" simple="yes"/> * </params> * * * @param $element_type element name under which the parsing happen * @return the parsed result objects */ function wsf_parse_payload_for_class_map(DomNode $payload, DomNode $sig_node, $element_type, $classmap, $cid2cont_type, $cid2attachments) { ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "Loading in to parsing classmap"); while ($payload != NULL && $payload->nodeType != XML_ELEMENT_NODE) { $payload = $payload->nextSibling; } // valid for headers $is_simple_header = FALSE; if ($sig_node->attributes->getNamedItem(WSF_WSDL_SIMPLE) && $sig_node->attributes->getNamedItem(WSF_WSDL_SIMPLE)->value == "yes") { $is_simple_header = TRUE; } // this situation meets only for non-wrapped mode as doclit-bare wsdls if ($is_simple_header) { $the_only_node = $sig_node; } else { $the_only_node = $sig_node->firstChild; } if ($sig_node->hasAttributes() || $the_only_node && $the_only_node->attributes->getNamedItem(WSF_CONTENT_MODEL) && $the_only_node->attributes->getNamedItem(WSF_CONTENT_MODEL)->value == WSF_SIMPLE_CONTENT) { //this will be assigned a value inside wsf_infer_sig_node_from_xml passing by references.. $object = NULL; $changed_sig_node = wsf_infer_sig_node_from_xml($sig_node, $payload, $object, $element_type, $classmap); // here on we consider our sig node is the changed one $sig_node = $changed_sig_node; if ($object == NULL) { return NULL; } // for a NULL payload return the empty object if ($payload == NULL) { return $object; } } if ($sig_node->hasAttributes() && !$is_simple_header) { //wrapped situations.. //initializing parse tree handling the nil situation $is_nil = $payload->getAttributeNS(WSF_XSI_NAMESPACE, "nil"); ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "xsi:nil = " . $is_nil); if ($is_nil && ($is_nil == "1" || $is_nil == "true" || $is_nil == "TRUE")) { $parse_tree = NULL; } else { $parse_tree = ""; } // this situation gotta use the object.. //again simple content should parsed differently if ($sig_node && $sig_node->attributes->getNamedItem(WSF_CONTENT_MODEL) && $sig_node->attributes->getNamedItem(WSF_CONTENT_MODEL)->value == WSF_SIMPLE_CONTENT) { $parse_tree = wsf_infer_content_model($payload, $sig_node, $classmap, $cid2cont_type, $cid2attachments); $attr_parse_tree = wsf_infer_attributes($payload, $sig_node); $parse_tree = array_merge($parse_tree, $attr_parse_tree); } else { // first parse the attributes ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "payload: " . wsf_test_serialize_node($payload)); $parse_tree = wsf_infer_attributes($payload, $sig_node); $current_child = $payload->firstChild; while ($current_child != NULL && $current_child->nodeType != XML_ELEMENT_NODE) { $current_child = $current_child->nextSibling; } if ($current_child != NULL) { $content_parse_tree = wsf_infer_content_model($current_child, $sig_node, $classmap, $cid2cont_type, $cid2attachments); $parse_tree = array_merge($content_parse_tree, $parse_tree); } } ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "parsed tree: " . print_r($parse_tree, TRUE)); ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "parsed tree: " . "\$parse_tree === NULL" . ($parse_tree === NULL)); // all the above part of the story we were handling non-wrap types like in doclit-bare if ($parse_tree === NULL) { return NULL; } foreach ($parse_tree as $parsed_key => $parsed_value) { $object->{$parsed_key} = $parsed_value; } ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "parsed object: " . print_r($object, TRUE)); return $object; } else { // this situation meets only for non-wrapped mode as doclit-bare wsdls // handle simple content extension seperatly if ($the_only_node && $the_only_node->attributes->getNamedItem(WSF_CONTENT_MODEL) && $the_only_node->attributes->getNamedItem(WSF_CONTENT_MODEL)->value == WSF_SIMPLE_CONTENT) { $parse_tree = wsf_infer_content_model($payload, $the_only_node, $classmap, $cid2cont_type, $cid2attachments); $attr_parse_tree = wsf_infer_attributes($payload, $the_only_node); $parse_tree = array_merge($parse_tree, $attr_parse_tree); foreach ($parse_tree as $parsed_key => $parsed_value) { $object->{$parsed_key} = $parsed_value; } return $object; } else { $is_simple = FALSE; if ($the_only_node->attributes->getNamedItem(WSF_WSDL_SIMPLE) && $the_only_node->attributes->getNamedItem(WSF_WSDL_SIMPLE)->value == "yes") { $is_simple = TRUE; } $param_type = NULL; if ($the_only_node->attributes->getNamedItem(WSF_TYPE)) { $param_type = $the_only_node->attributes->getNamedItem(WSF_TYPE)->value; } if ($is_simple) { if ($payload !== NULL) { if ($payload->firstChild) { $original_value = $payload->firstChild->nodeValue; } else { $original_value = ""; } $is_list = FALSE; if ($the_only_node->attributes->getNamedItem(WSF_LIST) && $the_only_node->attributes->getNamedItem(WSF_LIST)->value == "yes") { $is_list = TRUE; } $converted_value = wsf_wsdl_deserialize_string_value($param_type, $original_value, $the_only_node, $payload); return $converted_value; } } } } return NULL; }
static function convertDomNodeToDataArray(DomNode $node) { $data = array(); $values = array(); $has_value = false; if ($node->hasChildNodes()) { foreach ($node->childNodes as $child_node) { /* * A html dom node always contains one dom document type child * node with no content (DOMDocumentType#internalSubset is for * example <!DOCTYPE html>). Ignore it! */ if ($child_node instanceof DOMDocumentType) { continue; } if ($child_node->nodeType === XML_TEXT_NODE) { $has_value = true; $values[] = $child_node->nodeValue; } else { $key = $child_node->nodeName; if (isset($data[$key])) { if (!is_array($data[$key]) || !isset($data[$key][0])) { $data[$key] = array($data[$key]); } $data[$key][] = self::convertDomNodeToDataArray($child_node); } else { $data[$key] = self::convertDomNodeToDataArray($child_node); } } } } if ($node->hasAttributes()) { foreach ($node->attributes as $attribute_node) { $key = '@' . $attribute_node->nodeName; $data[$key] = $attribute_node->nodeValue; } } if ($has_value) { $value = implode('', $values); if (trim($value) != '') { if (empty($data)) { $data = $value; } else { $data['@'] = $value; } } } return $data; }