opensrs\Ops::XML2PHP PHP Метод

XML2PHP() публичный Метод

XML Parser that converts an OPS protocol message into a PHP array.
public XML2PHP ( $msg ) : mixed
Результат mixed PHP array, or error
    public function XML2PHP($msg)
    {
        $this->_data = null;
        $xp = xml_parser_create();
        xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, false);
        xml_parser_set_option($xp, XML_OPTION_SKIP_WHITE, true);
        xml_parser_set_option($xp, XML_OPTION_TARGET_ENCODING, 'ISO-8859-1');
        if (!xml_parse_into_struct($xp, $msg, $vals, $index)) {
            $error = sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($xp)), xml_get_current_line_number($xp));
            xml_parser_free($xp);
            throw new Exception('oSRS Error - ' . $error);
        }
        xml_parser_free($xp);
        $temp = $depth = array();
        foreach ($vals as $value) {
            switch ($value['tag']) {
                case 'OPS_envelope':
                case 'header':
                case 'body':
                case 'data_block':
                    break;
                case 'version':
                case 'msg_id':
                case 'msg_type':
                    $key = '_OPS_' . $value['tag'];
                    $temp[$key] = $value['value'];
                    break;
                case 'item':
                    // Not every Item has attributes
                    if (isset($value['attributes'])) {
                        $key = $value['attributes']['key'];
                    } else {
                        $key = '';
                    }
                    switch ($value['type']) {
                        case 'open':
                            array_push($depth, $key);
                            break;
                        case 'complete':
                            array_push($depth, $key);
                            $p = implode('::', $depth);
                            // enn_change - make sure that   $value['value']   is defined
                            if (isset($value['value'])) {
                                $temp[$p] = $value['value'];
                            } else {
                                $temp[$p] = '';
                            }
                            array_pop($depth);
                            break;
                        case 'close':
                            array_pop($depth);
                            break;
                    }
                    break;
                case 'dt_assoc':
                case 'dt_array':
                    break;
            }
        }
        foreach ($temp as $key => $value) {
            $levels = explode('::', $key);
            $num_levels = count($levels);
            if ($num_levels == 1) {
                $this->_data[$levels[0]] = $value;
            } else {
                $pointer =& $this->_data;
                for ($i = 0; $i < $num_levels; ++$i) {
                    if (!isset($pointer[$levels[$i]])) {
                        $pointer[$levels[$i]] = array();
                    }
                    $pointer =& $pointer[$levels[$i]];
                }
                $pointer = $value;
            }
        }
        return $this->_data;
    }

Usage Example

Пример #1
0
 /**
  * should convert xml to php array.
  */
 public function testXml2Php()
 {
     $ops = new Ops();
     $data = array('convert' => 'this');
     // use our built in php to xml to test
     $result = $ops->PHP2XML($data);
     // converting back, they should be the same
     $this->assertSame($data, $ops->XML2PHP($result));
 }