| Total Complexity | 4 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 18 | class IntegerValue extends DecimalValue |
||
| 19 | { |
||
| 20 | public const string SCHEMA_TYPE = 'integer'; |
||
|
|
|||
| 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 |