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