Skip to content

Commit 9fb86b2

Browse files
committed
Add toInteger/fromInteger helpers
1 parent 98bf400 commit 9fb86b2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"require": {
2929
"php": "^8.1",
30+
"ext-bcmath": "*",
3031
"ext-date": "*",
3132
"ext-dom": "*",
3233
"ext-filter": "*",

src/Type/IntegerValue.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use SimpleSAML\XML\Assert\Assert;
88
use SimpleSAML\XML\Exception\SchemaViolationException;
99

10+
use function bccomp;
11+
use function intval;
12+
use function strval;
13+
1014
/**
1115
* @package simplesaml/xml-common
1216
*/
@@ -27,4 +31,33 @@ protected function validateValue(string $value): void
2731
{
2832
Assert::validInteger($this->sanitizeValue($value), SchemaViolationException::class);
2933
}
34+
35+
36+
/**
37+
* Convert from integer
38+
*
39+
* @param int $value
40+
* @return static
41+
*/
42+
protected static function fromInteger(int $value): static
43+
{
44+
return new static(strval($value));
45+
}
46+
47+
48+
/**
49+
* Convert to integer
50+
*
51+
* @return int
52+
*/
53+
protected function toInteger(): int
54+
{
55+
$value = $this->getValue();
56+
57+
if (bccomp($value, strval(PHP_INT_MAX)) === 1) {
58+
throw new RuntimeException("Cannot convert to integer: out of bounds.");
59+
}
60+
61+
return intval($value);
62+
}
3063
}

0 commit comments

Comments
 (0)