Issues (343)

src/XML/Attribute.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML;
6
7
use DOMAttr;
8
use DOMElement;
9
use SimpleSAML\XML\Assert\Assert;
10
use SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface;
11
use SimpleSAML\XMLSchema\Type\StringValue;
0 ignored issues
show
The type SimpleSAML\XMLSchema\Type\StringValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
use function array_keys;
14
use function strval;
15
16
/**
17
 * Class to represent an arbitrary namespaced attribute.
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
final class Attribute implements ArrayizableElementInterface
22
{
23
    /**
24
     * Create an Attribute class
25
     *
26
     * @param string|null $namespaceURI
27
     * @param string|null $namespacePrefix
28
     * @param string $attrName
29
     * @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface $attrValue
30
     */
31
    public function __construct(
32
        protected ?string $namespaceURI,
33
        protected ?string $namespacePrefix,
34
        protected string $attrName,
35
        protected ValueTypeInterface $attrValue,
36
    ) {
37
        Assert::nullOrValidAnyURI($namespaceURI);
38
        if ($namespaceURI !== null) {
39
            Assert::nullOrValidNCName($namespacePrefix);
40
        }
41
        Assert::validNCName($attrName);
42
    }
43
44
45
    /**
46
     * Collect the value of the namespaceURI-property
47
     */
48
    public function getNamespaceURI(): ?string
49
    {
50
        return $this->namespaceURI;
51
    }
52
53
54
    /**
55
     * Collect the value of the namespacePrefix-property
56
     */
57
    public function getNamespacePrefix(): ?string
58
    {
59
        return $this->namespacePrefix;
60
    }
61
62
63
    /**
64
     * Collect the value of the localName-property
65
     */
66
    public function getAttrName(): string
67
    {
68
        return $this->attrName;
69
    }
70
71
72
    /**
73
     * Collect the value of the value-property
74
     *
75
     * @return \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface
76
     */
77
    public function getAttrValue(): ValueTypeInterface
78
    {
79
        return $this->attrValue;
80
    }
81
82
83
    /**
84
     * Create a class from XML
85
     *
86
     * @param \DOMAttr $attr
87
     */
88
    public static function fromXML(DOMAttr $attr): static
89
    {
90
        return new static($attr->namespaceURI, $attr->prefix, $attr->localName, StringValue::fromString($attr->value));
91
    }
92
93
94
    /**
95
     * Create XML from this class
96
     *
97
     * @param \DOMElement $parent
98
     */
99
    public function toXML(DOMElement $parent): DOMElement
100
    {
101
        if ($this->getNamespaceURI() !== null && !$parent->lookupPrefix($this->getNamespacePrefix())) {
102
            $parent->setAttributeNS(
103
                'http://www.w3.org/2000/xmlns/',
104
                'xmlns:' . $this->getNamespacePrefix(),
105
                $this->getNamespaceURI(),
106
            );
107
        }
108
109
        $parent->setAttributeNS(
110
            $this->getNamespaceURI(),
111
            !in_array($this->getNamespacePrefix(), ['', null])
112
                ? ($this->getNamespacePrefix() . ':' . $this->getAttrName())
113
                : $this->getAttrName(),
114
            strval($this->getAttrValue()),
115
        );
116
117
        return $parent;
118
    }
119
120
121
    /**
122
     * Create a class from an array
123
     *
124
     * @param array{
125
     *   namespaceURI: string,
126
     *   namespacePrefix: string|null,
127
     *   attrName: string,
128
     *   attrValue:  \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface,
129
     * } $data
130
     */
131
    public static function fromArray(array $data): static
132
    {
133
        $data = self::processArrayContents($data);
134
135
        return new static(
136
            $data['namespaceURI'],
137
            $data['namespacePrefix'],
138
            $data['attrName'],
139
            StringValue::fromString($data['attrValue']),
140
        );
141
    }
142
143
144
    /**
145
     * Validates an array representation of this object and returns the same array with rationalized keys
146
     *
147
     * @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data
148
     * @return array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed}
149
     */
150
    private static function processArrayContents(array $data): array
151
    {
152
        $data = array_change_key_case($data, CASE_LOWER);
153
154
        /** @var array{namespaceuri: string, namespaceprefix: string|null, attrname: string, attrvalue: mixed} $data */
155
        Assert::allOneOf(
156
            array_keys($data),
157
            ['namespaceuri', 'namespaceprefix', 'attrname', 'attrvalue'],
158
        );
159
160
        Assert::keyExists($data, 'namespaceuri');
161
        Assert::keyExists($data, 'namespaceprefix');
162
        Assert::keyExists($data, 'attrname');
163
        Assert::keyExists($data, 'attrvalue');
164
165
        Assert::nullOrValidAnyURI($data['namespaceuri']);
166
        Assert::nullOrValidNCName($data['namespaceprefix']);
167
        Assert::nullOrValidNCName($data['attrname']);
168
        Assert::string($data['attrvalue']);
169
170
        return [
171
            'namespaceURI' => $data['namespaceuri'],
172
            'namespacePrefix' => $data['namespaceprefix'],
173
            'attrName' => $data['attrname'],
174
            'attrValue' => $data['attrvalue'],
175
        ];
176
    }
177
178
179
    /**
180
     * Create an array from this class
181
     *
182
     * @return array{
183
     *   attrName: string,
184
     *   attrValue: string,
185
     *   namespacePrefix: string,
186
     *   namespaceURI: null|string,
187
     * }
188
     */
189
    public function toArray(): array
190
    {
191
        return [
192
            'namespaceURI' => $this->getNamespaceURI(),
193
            'namespacePrefix' => $this->getNamespacePrefix(),
194
            'attrName' => $this->getAttrName(),
195
            'attrValue' => $this->getAttrValue()->getValue(),
196
        ];
197
    }
198
}
199