Passed
Pull Request — master (#55)
by Tim
01:54
created

QNameValue   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 1
b 0
f 0
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 4 1
A getLocalName() 0 8 2
A getNamespacePrefix() 0 8 2
A sanitizeValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML\Type;
6
7
use SimpleSAML\XML\Assert\Assert;
8
use SimpleSAML\XML\Exception\SchemaViolationException;
9
10
use function explode;
11
12
/**
13
 * @package simplesaml/xml-common
14
 */
15
class QNameValue extends AbstractValueType
16
{
17
    /**
18
     * Sanitize the value.
19
     *
20
     * @param string $value  The unsanitized value
21
     * @return string
22
     */
23
    protected function sanitizeValue(string $value): string
24
    {
25
        return static::collapseWhitespace(static::normalizeWhitespace($value));
26
    }
27
28
29
    /**
30
     * Validate the value.
31
     *
32
     * @param string $value
33
     * @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
34
     * @return void
35
     */
36
    protected function validateValue(string $value): void
37
    {
38
        // Note: value must already be sanitized before validating
39
        Assert::validQName($this->sanitizeValue($value), SchemaViolationException::class);
40
    }
41
42
43
    /**
44
     * Get the namespace-prefix for this qualified name.
45
     *
46
     * @return \SimpleSAML\XML\Type\NCNameValue|null
47
     */
48
    public function getNamespacePrefix(): ?NCNameValue
49
    {
50
        $qname = explode(':', $this->getValue(), 2);
51
        if (count($qname) === 2) {
52
            return NCNameValue::fromString($qname[0]);
53
        }
54
55
        return null;
56
    }
57
58
59
    /**
60
     * Get the local name for this qualified name.
61
     *
62
     * @return \SimpleSAML\XML\Type\NCNameValue
63
     */
64
    public function getLocalName(): NCNameValue
65
    {
66
        $qname = explode(':', $this->getValue(), 2);
67
        if (count($qname) === 2) {
68
            return NCNameValue::fromString($qname[1]);
69
        }
70
71
        return NCNameValue::fromString($qname[0]);
72
    }
73
}
74