Subversion Repositories Web Services

Rev

Rev 102682 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
82190 hhl 1
<?php
2
/**
3
 *
4
 * This file is part of Open Library System.
5
 * Copyright © 2009, Dansk Bibliotekscenter a/s,
6
 * Tempovej 7-11, DK-2750 Ballerup, Denmark. CVR: 15149043
7
 *
8
 * Open Library System is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Open Library System is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with Open Library System.  If not, see <http://www.gnu.org/licenses/>.
118297 fvs 20
 */
82190 hhl 21
 
22
 
23
/**
24
 * \brief
25
 *
26
 * @author Finn Stausgaard - DBC
27
 */
28
class xmlconvert {
29
 
118297 fvs 30
  /**
31
   * xmlconvert constructor.
32
   */
82190 hhl 33
  public function __construct() {
34
  }
35
 
36
  /** \brief Create an ols--object out of SOAP xml
37
   *
118297 fvs 38
   * @param $request
39
   * @return bool|mixed
82190 hhl 40
   */
41
  public function soap2obj(&$request) {
118297 fvs 42
    if ($request) {
43
      $dom = new DomDocument();
44
      $dom->preserveWhiteSpace = FALSE;
45
      if (@ $dom->loadXML($request))
46
        return $this->xml2obj($dom);
47
    }
48
    return FALSE;
82190 hhl 49
  }
50
 
118297 fvs 51
 
82190 hhl 52
  /** \brief Converts domdocument object to object.
118297 fvs 53
   *
54
   * @param $domobj
55
   * @param string $force_NS
56
   * @return mixed
57
   */
58
  public function xml2obj($domobj, $force_NS = '') {
82190 hhl 59
    foreach ($domobj->childNodes as $node) {
94543 fvs 60
      $subnode = new stdClass();
82190 hhl 61
      if ($node->nodeName == '#comment') {
62
        continue;
63
      }
64
      if ($force_NS) {
65
        $subnode->_namespace = htmlspecialchars($force_NS);
66
      }
67
      elseif ($node->namespaceURI) {
68
        $subnode->_namespace = htmlspecialchars($node->namespaceURI);
69
      }
70
      if ($node->nodeName == '#text' || $node->nodeName == '#cdata-section') {
71
        if (trim($node->nodeValue) == '') {
72
          continue;
73
        }
74
        $subnode->_value = trim($node->nodeValue);
75
        $localName = '#text';
76
      }
77
      else {
78
        $localName = $node->localName;
79
        $subnode->_value = $this->xml2obj($node, $force_NS);
80
        if ($node->hasAttributes()) {
102682 fvs 81
          $subnode->_attributes = new stdClass();
82190 hhl 82
          foreach ($node->attributes as $attr) {
83
            $a_nodename = $attr->localName;
100082 fvs 84
            $subnode->_attributes->{$a_nodename} = new stdClass();
82190 hhl 85
            if ($attr->namespaceURI)
86
              $subnode->_attributes->{$a_nodename}->_namespace = htmlspecialchars($attr->namespaceURI);
87
            $subnode->_attributes->{$a_nodename}->_value = $attr->nodeValue;
88
          }
89
        }
90
      }
91
      $ret->{$localName}[] = $subnode;
92
      unset($subnode);
93
    }
94
 
95
    // remove unnecessary level(s) for text-nodes and non-repeated tags
96
    if ($ret) {
97
      if (count((array)$ret) == 1 && isset($ret->{'#text'})) {
98
        $ret = $ret->{'#text'}[0]->_value;
99
      }
100
      else {
101
        foreach ($ret as $tag => $obj) {
102
          if (count($obj) == 1) {
103
            $ret->$tag = $obj[0];
104
          }
105
        }
106
        reset($ret);
107
      }
108
    }
109
 
110
    return $ret;
111
 
112
  }
113
 
114
}
115
 
116