Skip to content

Commit 7a72723

Browse files
committed
Create assertion and type-class for xs:int
1 parent 1d00197 commit 7a72723

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @method static void validID(mixed $value, string $message = '', string $exception = '')
2424
* @method static void validIDRef(mixed $value, string $message = '', string $exception = '')
2525
* @method static void validIDRefs(mixed $value, string $message = '', string $exception = '')
26+
* @method static void validInt(mixed $value, string $message = '', string $exception = '')
2627
* @method static void validInteger(mixed $value, string $message = '', string $exception = '')
2728
* @method static void validLang(mixed $value, string $message = '', string $exception = '')
2829
* @method static void validLong(mixed $value, string $message = '', string $exception = '')
@@ -55,6 +56,7 @@
5556
* @method static void nullOrValidID(mixed $value, string $message = '', string $exception = '')
5657
* @method static void nullOrValidIDRef(mixed $value, string $message = '', string $exception = '')
5758
* @method static void nullOrValidIDRefs(mixed $value, string $message = '', string $exception = '')
59+
* @method static void nullOrValidInt(mixed $value, string $message = '', string $exception = '')
5860
* @method static void nullOrValidInteger(mixed $value, string $message = '', string $exception = '')
5961
* @method static void nullOrValidLang(mixed $value, string $message = '', string $exception = '')
6062
* @method static void nullOrValidLong(mixed $value, string $message = '', string $exception = '')
@@ -87,6 +89,7 @@
8789
* @method static void allValidID(mixed $value, string $message = '', string $exception = '')
8890
* @method static void allValidIDRef(mixed $value, string $message = '', string $exception = '')
8991
* @method static void allValidIDRefs(mixed $value, string $message = '', string $exception = '')
92+
* @method static void allValidInt(mixed $value, string $message = '', string $exception = '')
9093
* @method static void allValidInteger(mixed $value, string $message = '', string $exception = '')
9194
* @method static void allValidLang(mixed $value, string $message = '', string $exception = '')
9295
* @method static void allValidLong(mixed $value, string $message = '', string $exception = '')
@@ -122,6 +125,7 @@ class Assert extends BaseAssert
122125
use IDTrait;
123126
use IDRefTrait;
124127
use IDRefsTrait;
128+
use IntTrait;
125129
use IntegerTrait;
126130
use LangTrait;
127131
use LongTrait;

src/Assert/IntTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-common
11+
*/
12+
trait IntTrait
13+
{
14+
/** @var string */
15+
private static string $int_regex = '/^(((-[0]*([0-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|[1-8][0-9]{4}|9[0-8][0-9]{3}|99[0-8][0-9]{2}|999[0-8][0-9]|9999[0-9]|[1-8][0-9]{5}|9[0-8][0-9]{4}|99[0-8][0-9]{3}|999[0-8][0-9]{2}|9999[0-8][0-9]|99999[0-9]|[1-8][0-9]{6}|9[0-8][0-9]{5}|99[0-8][0-9]{4}|999[0-8][0-9]{3}|9999[0-8][0-9]{2}|99999[0-8][0-9]|999999[0-9]|[1-8][0-9]{7}|9[0-8][0-9]{6}|99[0-8][0-9]{5}|999[0-8][0-9]{4}|9999[0-8][0-9]{3}|99999[0-8][0-9]{2}|999999[0-8][0-9]|9999999[0-9]|[1-8][0-9]{8}|9[0-8][0-9]{7}|99[0-8][0-9]{6}|999[0-8][0-9]{5}|9999[0-8][0-9]{4}|99999[0-8][0-9]{3}|999999[0-8][0-9]{2}|9999999[0-8][0-9]|99999999[0-9]|1[0-9]{9}|20[0-9]{8}|21[0-3][0-9]{7}|214[0-6][0-9]{6}|2147[0-3][0-9]{5}|21474[0-7][0-9]{4}|214748[0-2][0-9]{3}|2147483[0-5][0-9]{2}|21474836[0-3][0-9]|214748364[0-8]))|([+]?[0]*(([0-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|[1-8][0-9]{4}|9[0-8][0-9]{3}|99[0-8][0-9]{2}|999[0-8][0-9]|9999[0-9]|[1-8][0-9]{5}|9[0-8][0-9]{4}|99[0-8][0-9]{3}|999[0-8][0-9]{2}|9999[0-8][0-9]|99999[0-9]|[1-8][0-9]{6}|9[0-8][0-9]{5}|99[0-8][0-9]{4}|999[0-8][0-9]{3}|9999[0-8][0-9]{2}|99999[0-8][0-9]|999999[0-9]|[1-8][0-9]{7}|9[0-8][0-9]{6}|99[0-8][0-9]{5}|999[0-8][0-9]{4}|9999[0-8][0-9]{3}|99999[0-8][0-9]{2}|999999[0-8][0-9]|9999999[0-9]|[1-8][0-9]{8}|9[0-8][0-9]{7}|99[0-8][0-9]{6}|999[0-8][0-9]{5}|9999[0-8][0-9]{4}|99999[0-8][0-9]{3}|999999[0-8][0-9]{2}|9999999[0-8][0-9]|99999999[0-9]|1[0-9]{9}|20[0-9]{8}|21[0-3][0-9]{7}|214[0-6][0-9]{6}|2147[0-3][0-9]{5}|21474[0-7][0-9]{4}|214748[0-2][0-9]{3}|2147483[0-5][0-9]{2}|21474836[0-3][0-9]|214748364[0-7])))|0))$/D';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validInt(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$int_regex,
27+
$message ?: '%s is not a valid xs:int',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/IntValue.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
/**
11+
* @package simplesaml/xml-common
12+
*/
13+
class IntValue extends IntegerValue
14+
{
15+
/**
16+
* Validate the value.
17+
*
18+
* @param string $value
19+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
20+
* @return void
21+
*/
22+
protected function validateValue(string $value): void
23+
{
24+
Assert::validInt($this->sanitizeValue($value), SchemaViolationException::class);
25+
}
26+
}

tests/Assert/IntTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Assert;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Assert\AssertionFailedException;
11+
use SimpleSAML\XML\Assert\Assert;
12+
13+
/**
14+
* Class \SimpleSAML\Test\XML\Assert\IntTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class IntTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $int
24+
*/
25+
#[DataProvider('provideInt')]
26+
public function testValidInt(bool $shouldPass, string $int): void
27+
{
28+
try {
29+
Assert::validInt($int);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideInt(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive signed' => [true, '+2147483647'],
45+
'valid negative signed' => [true, '-2147483648'],
46+
'valid non-signed' => [true, '123'],
47+
'valid leading zeros' => [true, '-0001'],
48+
'valid zero' => [true, '0'],
49+
'invalid positive signed out-of-bounds' => [false, '+2147483648'],
50+
'invalid negative signed out-of-bounds' => [false, '-2147483649'],
51+
'invalid with space' => [false, '1 234'],
52+
'invalid with fractional' => [false, '1234.'],
53+
'invalid with thousands-delimiter' => [false, '+1,234'],
54+
];
55+
}
56+
}

tests/Type/IntValueTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Type;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Type\IntValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\IntValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(IntValue::class)]
19+
final class IntValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $int
24+
*/
25+
#[DataProvider('provideInt')]
26+
public function testInt(bool $shouldPass, string $int): void
27+
{
28+
try {
29+
IntValue::fromString($int);
30+
$this->assertTrue($shouldPass);
31+
} catch (SchemaViolationException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideInt(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive signed' => [true, '+2147483647'],
45+
'valid negative signed' => [true, '-2147483648'],
46+
'valid non-signed' => [true, '123'],
47+
'valid leading zeros' => [true, '-0001'],
48+
'valid zero' => [true, '0'],
49+
'invalid positive signed out-of-bounds' => [false, '+2147483648'],
50+
'invalid negative signed out-of-bounds' => [false, '-2147483649'],
51+
'valid with whitespace collapse' => [true, " 1 234 \n"],
52+
'invalid with fractional' => [false, '1234.'],
53+
'invalid with thousands-delimiter' => [false, '+1,234'],
54+
];
55+
}
56+
}

0 commit comments

Comments
 (0)