Passed
Branch feature/php8.3 (4d3b0a)
by Tim
17:15
created

IntegerValue   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\Type;
6
7
use SimpleSAML\XML\Assert\Assert;
8
use SimpleSAML\XMLSchema\Exception\RuntimeException;
9
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
10
11
use function bccomp;
12
use function intval;
13
use function strval;
14
15
/**
16
 * @package simplesaml/xml-common
17
 */
18
class IntegerValue extends DecimalValue
19
{
20
    public const string SCHEMA_TYPE = 'integer';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 20 at column 24
Loading history...
21
22
23
    /**
24
     * Validate the value.
25
     *
26
     * @param string $value
27
     * @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
28
     */
29
    protected function validateValue(string $value): void
30
    {
31
        Assert::validInteger($this->sanitizeValue($value), SchemaViolationException::class);
32
    }
33
34
35
    /**
36
     * Convert from integer
37
     *
38
     * @param int $value
39
     */
40
    public static function fromInteger(int $value): static
41
    {
42
        return new static(strval($value));
43
    }
44
45
46
    /**
47
     * Convert to integer
48
     */
49
    public function toInteger(): int
50
    {
51
        $value = $this->getValue();
52
53
        if (bccomp($value, strval(PHP_INT_MAX)) === 1) {
54
            throw new RuntimeException("Cannot convert to integer: out of bounds.");
55
        }
56
57
        return intval($value);
58
    }
59
}
60