Completed
Push — master ( 381bb7...8e06a1 )
by Thijs
04:13
created

ContactPerson::toXML()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
c 0
b 0
f 0
nc 64
nop 1
dl 0
loc 42
rs 6.7272
1
<?php
2
3
namespace SAML2\XML\md;
4
5
use SAML2\Constants;
6
use SAML2\Utils;
7
8
/**
9
 * Class representing SAML 2 ContactPerson.
10
 *
11
 * @package SimpleSAMLphp
12
 */
13
class ContactPerson
14
{
15
    /**
16
     * The contact type.
17
     *
18
     * @var string
19
     */
20
    public $contactType;
21
22
    /**
23
     * Extensions on this element.
24
     *
25
     * Array of extension elements.
26
     *
27
     * @var array
28
     */
29
    public $Extensions = array();
30
31
    /**
32
     * The Company of this contact.
33
     *
34
     * @var string
35
     */
36
    public $Company = null;
37
38
    /**
39
     * The GivenName of this contact.
40
     *
41
     * @var string
42
     */
43
    public $GivenName = null;
44
45
    /**
46
     * The SurName of this contact.
47
     *
48
     * @var string
49
     */
50
    public $SurName = null;
51
52
    /**
53
     * The EmailAddresses of this contact.
54
     *
55
     * @var array
56
     */
57
    public $EmailAddress = array();
58
59
    /**
60
     * The TelephoneNumbers of this contact.
61
     *
62
     * @var array
63
     */
64
    public $TelephoneNumber = array();
65
66
    /**
67
     * Extra attributes on the contact element.
68
     *
69
     * @var array
70
     */
71
    public $ContactPersonAttributes = array();
72
73
    /**
74
     * Initialize a ContactPerson element.
75
     *
76
     * @param \DOMElement|null $xml The XML element we should load.
77
     * @throws \Exception
78
     */
79
    public function __construct(\DOMElement $xml = null)
80
    {
81
        if ($xml === null) {
82
            return;
83
        }
84
85
        if (!$xml->hasAttribute('contactType')) {
86
            throw new \Exception('Missing contactType on ContactPerson.');
87
        }
88
        $this->contactType = $xml->getAttribute('contactType');
89
90
        $this->Extensions = Extensions::getList($xml);
91
92
        $this->Company = self::getStringElement($xml, 'Company');
93
        $this->GivenName = self::getStringElement($xml, 'GivenName');
94
        $this->SurName = self::getStringElement($xml, 'SurName');
95
        $this->EmailAddress = self::getStringElements($xml, 'EmailAddress');
96
        $this->TelephoneNumber = self::getStringElements($xml, 'TelephoneNumber');
97
98
        foreach ($xml->attributes as $attr) {
99
            if ($attr->nodeName == "contactType") {
100
                continue;
101
            }
102
103
            $this->ContactPersonAttributes[$attr->nodeName] = $attr->nodeValue;
104
        }
105
    }
106
107
    /**
108
     * Retrieve the value of a child \DOMElements as an array of strings.
109
     *
110
     * @param  \DOMElement $parent The parent element.
111
     * @param  string     $name   The name of the child elements.
112
     * @return array      The value of the child elements.
113
     */
114
    private static function getStringElements(\DOMElement $parent, $name)
115
    {
116
        assert('is_string($name)');
117
118
        $e = Utils::xpQuery($parent, './saml_metadata:' . $name);
119
120
        $ret = array();
121
        foreach ($e as $i) {
122
            $ret[] = $i->textContent;
123
        }
124
125
        return $ret;
126
    }
127
128
    /**
129
     * Retrieve the value of a child \DOMElement as a string.
130
     *
131
     * @param  \DOMElement  $parent The parent element.
132
     * @param  string      $name   The name of the child element.
133
     * @return string|null The value of the child element.
134
     * @throws \Exception
135
     */
136
    private static function getStringElement(\DOMElement $parent, $name)
137
    {
138
        assert('is_string($name)');
139
140
        $e = self::getStringElements($parent, $name);
141
        if (empty($e)) {
142
            return null;
143
        }
144
        if (count($e) > 1) {
145
            throw new \Exception('More than one ' . $name . ' in ' . $parent->tagName);
146
        }
147
148
        return $e[0];
149
    }
150
151
    /**
152
     * Convert this ContactPerson to XML.
153
     *
154
     * @param  \DOMElement $parent The element we should add this contact to.
155
     * @return \DOMElement The new ContactPerson-element.
156
     */
157
    public function toXML(\DOMElement $parent)
158
    {
159
        assert('is_string($this->contactType)');
160
        assert('is_array($this->Extensions)');
161
        assert('is_null($this->Company) || is_string($this->Company)');
162
        assert('is_null($this->GivenName) || is_string($this->GivenName)');
163
        assert('is_null($this->SurName) || is_string($this->SurName)');
164
        assert('is_array($this->EmailAddress)');
165
        assert('is_array($this->TelephoneNumber)');
166
        assert('is_array($this->ContactPersonAttributes)');
167
168
        $doc = $parent->ownerDocument;
169
170
        $e = $doc->createElementNS(Constants::NS_MD, 'md:ContactPerson');
171
        $parent->appendChild($e);
172
173
        $e->setAttribute('contactType', $this->contactType);
174
175
        foreach ($this->ContactPersonAttributes as $attr => $val) {
176
            $e->setAttribute($attr, $val);
177
        }
178
179
        Extensions::addList($e, $this->Extensions);
180
181
        if (isset($this->Company)) {
182
            Utils::addString($e, Constants::NS_MD, 'md:Company', $this->Company);
183
        }
184
        if (isset($this->GivenName)) {
185
            Utils::addString($e, Constants::NS_MD, 'md:GivenName', $this->GivenName);
186
        }
187
        if (isset($this->SurName)) {
188
            Utils::addString($e, Constants::NS_MD, 'md:SurName', $this->SurName);
189
        }
190
        if (!empty($this->EmailAddress)) {
191
            Utils::addStrings($e, Constants::NS_MD, 'md:EmailAddress', false, $this->EmailAddress);
192
        }
193
        if (!empty($this->TelephoneNumber)) {
194
            Utils::addStrings($e, Constants::NS_MD, 'md:TelephoneNumber', false, $this->TelephoneNumber);
195
        }
196
197
        return $e;
198
    }
199
}
200