BooleanValue   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitizeValue() 0 3 1
A validateValue() 0 4 1
A fromBoolean() 0 4 2
A toBoolean() 0 4 2
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 boolval;
11
use function in_array;
12
13
/**
14
 * @package simplesaml/xml-common
15
 */
16
class BooleanValue extends AbstractValueType
17
{
18
    /** @var string */
19
    public const SCHEMA_TYPE = 'boolean';
20
21
22
    /**
23
     * Sanitize the value.
24
     *
25
     * @param string $value  The unsanitized value
26
     * @return string
27
     */
28
    protected function sanitizeValue(string $value): string
29
    {
30
        return static::collapseWhitespace(static::normalizeWhitespace($value));
31
    }
32
33
34
    /**
35
     * Validate the value.
36
     *
37
     * @param string $value
38
     * @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
39
     * @return void
40
     */
41
    protected function validateValue(string $value): void
42
    {
43
        // Note: value must already be sanitized before validating
44
        Assert::validBoolean($this->sanitizeValue($value), SchemaViolationException::class);
45
    }
46
47
48
    /**
49
     * @param boolean $value
50
     * @return static
51
     */
52
    public static function fromBoolean(bool $value): static
53
    {
54
        return new static(
55
            $value === true ? 'true' : 'false',
56
        );
57
    }
58
59
60
    /**
61
     * @return boolean $value
62
     */
63
    public function toBoolean(): bool
64
    {
65
        return boolval(
66
            in_array($this->getValue(), ['1', 'true']) ? '1' : '0',
67
        );
68
    }
69
}
70