MustUnderstandValue   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 6
c 0
b 0
f 0
dl 0
loc 49
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromBoolean() 0 4 2
A validateValue() 0 4 1
A toBoolean() 0 3 1
A toAttribute() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP11\Type;
6
7
use SimpleSAML\SOAP11\Assert\Assert;
8
use SimpleSAML\SOAP11\Constants as C;
9
use SimpleSAML\XML\Attribute;
10
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
11
use SimpleSAML\XMLSchema\Type\BooleanValue;
12
use SimpleSAML\XMLSchema\Type\Interface\AttributeTypeInterface;
13
14
use function boolval;
15
16
/**
17
 * @package simplesaml/xml-common
18
 */
19
class MustUnderstandValue extends BooleanValue implements AttributeTypeInterface
20
{
21
    /** @var string */
22
    public const SCHEMA_TYPE = 'boolean';
23
24
25
    /**
26
     * Validate the value.
27
     *
28
     * @param string $value
29
     * @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
30
     * @return void
31
     */
32
    protected function validateValue(string $value): void
33
    {
34
        // Note: value must already be sanitized before validating
35
        Assert::validMustUnderstand($this->sanitizeValue($value), SchemaViolationException::class);
36
    }
37
38
39
    /**
40
     * @param boolean $value
41
     * @return static
42
     */
43
    public static function fromBoolean(bool $value): static
44
    {
45
        return new static(
46
            $value === true ? '1' : '0',
47
        );
48
    }
49
50
51
    /**
52
     * @return boolean $value
53
     */
54
    public function toBoolean(): bool
55
    {
56
        return boolval($this->getValue());
57
    }
58
59
60
    /**
61
     * Convert this value to an attribute
62
     *
63
     * @return \SimpleSAML\XML\Attribute
64
     */
65
    public function toAttribute(): Attribute
66
    {
67
        return new Attribute(C::NS_SOAP_ENV, 'SOAP-ENV', 'mustUnderstand', $this);
68
    }
69
}
70