Skip to content

Commit c97dc71

Browse files
committed
Create assertion and type-class for xs:long
1 parent 2704771 commit c97dc71

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @method static void validIDRefs(mixed $value, string $message = '', string $exception = '')
2626
* @method static void validInteger(mixed $value, string $message = '', string $exception = '')
2727
* @method static void validLang(mixed $value, string $message = '', string $exception = '')
28+
* @method static void validLong(mixed $value, string $message = '', string $exception = '')
2829
* @method static void validMonth(mixed $value, string $message = '', string $exception = '')
2930
* @method static void validName(mixed $value, string $message = '', string $exception = '')
3031
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
@@ -56,6 +57,7 @@
5657
* @method static void nullOrValidIDRefs(mixed $value, string $message = '', string $exception = '')
5758
* @method static void nullOrValidInteger(mixed $value, string $message = '', string $exception = '')
5859
* @method static void nullOrValidLang(mixed $value, string $message = '', string $exception = '')
60+
* @method static void nullOrValidLong(mixed $value, string $message = '', string $exception = '')
5961
* @method static void nullOrValidMonth(mixed $value, string $message = '', string $exception = '')
6062
* @method static void nullOrValidName(mixed $value, string $message = '', string $exception = '')
6163
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
@@ -87,6 +89,7 @@
8789
* @method static void allValidIDRefs(mixed $value, string $message = '', string $exception = '')
8890
* @method static void allValidInteger(mixed $value, string $message = '', string $exception = '')
8991
* @method static void allValidLang(mixed $value, string $message = '', string $exception = '')
92+
* @method static void allValidLong(mixed $value, string $message = '', string $exception = '')
9093
* @method static void allValidMonth(mixed $value, string $message = '', string $exception = '')
9194
* @method static void allValidName(mixed $value, string $message = '', string $exception = '')
9295
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
@@ -121,6 +124,7 @@ class Assert extends BaseAssert
121124
use IDRefsTrait;
122125
use IntegerTrait;
123126
use LangTrait;
127+
use LongTrait;
124128
use MonthTrait;
125129
use NameTrait;
126130
use NCNameTrait;

src/Assert/LongTrait.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 LongTrait
13+
{
14+
/** @var string */
15+
private static string $long_regex = '/^(((-[0]*([1-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-8][0-9]{9}|9[0-8][0-9]{8}|99[0-8][0-9]{7}|999[0-8][0-9]{6}|9999[0-8][0-9]{5}|99999[0-8][0-9]{4}|999999[0-8][0-9]{3}|9999999[0-8][0-9]{2}|99999999[0-8][0-9]|999999999[0-9]|[1-8][0-9]{10}|9[0-8][0-9]{9}|99[0-8][0-9]{8}|999[0-8][0-9]{7}|9999[0-8][0-9]{6}|99999[0-8][0-9]{5}|999999[0-8][0-9]{4}|9999999[0-8][0-9]{3}|99999999[0-8][0-9]{2}|999999999[0-8][0-9]|9999999999[0-9]|[1-8][0-9]{11}|9[0-8][0-9]{10}|99[0-8][0-9]{9}|999[0-8][0-9]{8}|9999[0-8][0-9]{7}|99999[0-8][0-9]{6}|999999[0-8][0-9]{5}|9999999[0-8][0-9]{4}|99999999[0-8][0-9]{3}|999999999[0-8][0-9]{2}|9999999999[0-8][0-9]|99999999999[0-9]|[1-8][0-9]{12}|9[0-8][0-9]{11}|99[0-8][0-9]{10}|999[0-8][0-9]{9}|9999[0-8][0-9]{8}|99999[0-8][0-9]{7}|999999[0-8][0-9]{6}|9999999[0-8][0-9]{5}|99999999[0-8][0-9]{4}|999999999[0-8][0-9]{3}|9999999999[0-8][0-9]{2}|99999999999[0-8][0-9]|999999999999[0-9]|[1-8][0-9]{13}|9[0-8][0-9]{12}|99[0-8][0-9]{11}|999[0-8][0-9]{10}|9999[0-8][0-9]{9}|99999[0-8][0-9]{8}|999999[0-8][0-9]{7}|9999999[0-8][0-9]{6}|99999999[0-8][0-9]{5}|999999999[0-8][0-9]{4}|9999999999[0-8][0-9]{3}|99999999999[0-8][0-9]{2}|999999999999[0-8][0-9]|9999999999999[0-9]|[1-8][0-9]{14}|9[0-8][0-9]{13}|99[0-8][0-9]{12}|999[0-8][0-9]{11}|9999[0-8][0-9]{10}|99999[0-8][0-9]{9}|999999[0-8][0-9]{8}|9999999[0-8][0-9]{7}|99999999[0-8][0-9]{6}|999999999[0-8][0-9]{5}|9999999999[0-8][0-9]{4}|99999999999[0-8][0-9]{3}|999999999999[0-8][0-9]{2}|9999999999999[0-8][0-9]|99999999999999[0-9]|[1-8][0-9]{15}|9[0-8][0-9]{14}|99[0-8][0-9]{13}|999[0-8][0-9]{12}|9999[0-8][0-9]{11}|99999[0-8][0-9]{10}|999999[0-8][0-9]{9}|9999999[0-8][0-9]{8}|99999999[0-8][0-9]{7}|999999999[0-8][0-9]{6}|9999999999[0-8][0-9]{5}|99999999999[0-8][0-9]{4}|999999999999[0-8][0-9]{3}|9999999999999[0-8][0-9]{2}|99999999999999[0-8][0-9]|999999999999999[0-9]|[1-8][0-9]{16}|9[0-8][0-9]{15}|99[0-8][0-9]{14}|999[0-8][0-9]{13}|9999[0-8][0-9]{12}|99999[0-8][0-9]{11}|999999[0-8][0-9]{10}|9999999[0-8][0-9]{9}|99999999[0-8][0-9]{8}|999999999[0-8][0-9]{7}|9999999999[0-8][0-9]{6}|99999999999[0-8][0-9]{5}|999999999999[0-8][0-9]{4}|9999999999999[0-8][0-9]{3}|99999999999999[0-8][0-9]{2}|999999999999999[0-8][0-9]|9999999999999999[0-9]|[1-8][0-9]{17}|9[0-8][0-9]{16}|99[0-8][0-9]{15}|999[0-8][0-9]{14}|9999[0-8][0-9]{13}|99999[0-8][0-9]{12}|999999[0-8][0-9]{11}|9999999[0-8][0-9]{10}|99999999[0-8][0-9]{9}|999999999[0-8][0-9]{8}|9999999999[0-8][0-9]{7}|99999999999[0-8][0-9]{6}|999999999999[0-8][0-9]{5}|9999999999999[0-8][0-9]{4}|99999999999999[0-8][0-9]{3}|999999999999999[0-8][0-9]{2}|9999999999999999[0-8][0-9]|99999999999999999[0-9]|[1-8][0-9]{18}|9[01][0-9]{17}|92[01][0-9]{16}|922[0-2][0-9]{15}|9223[0-2][0-9]{14}|92233[0-6][0-9]{13}|922337[01][0-9]{12}|92233720[0-2][0-9]{10}|922337203[0-5][0-9]{9}|9223372036[0-7][0-9]{8}|92233720368[0-4][0-9]{7}|922337203685[0-3][0-9]{6}|9223372036854[0-6][0-9]{5}|92233720368547[0-6][0-9]{4}|922337203685477[0-4][0-9]{3}|9223372036854775[0-7][0-9]{2}|922337203685477580[0-8]))|([+]?[0]*([1-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-8][0-9]{9}|9[0-8][0-9]{8}|99[0-8][0-9]{7}|999[0-8][0-9]{6}|9999[0-8][0-9]{5}|99999[0-8][0-9]{4}|999999[0-8][0-9]{3}|9999999[0-8][0-9]{2}|99999999[0-8][0-9]|999999999[0-9]|[1-8][0-9]{10}|9[0-8][0-9]{9}|99[0-8][0-9]{8}|999[0-8][0-9]{7}|9999[0-8][0-9]{6}|99999[0-8][0-9]{5}|999999[0-8][0-9]{4}|9999999[0-8][0-9]{3}|99999999[0-8][0-9]{2}|999999999[0-8][0-9]|9999999999[0-9]|[1-8][0-9]{11}|9[0-8][0-9]{10}|99[0-8][0-9]{9}|999[0-8][0-9]{8}|9999[0-8][0-9]{7}|99999[0-8][0-9]{6}|999999[0-8][0-9]{5}|9999999[0-8][0-9]{4}|99999999[0-8][0-9]{3}|999999999[0-8][0-9]{2}|9999999999[0-8][0-9]|99999999999[0-9]|[1-8][0-9]{12}|9[0-8][0-9]{11}|99[0-8][0-9]{10}|999[0-8][0-9]{9}|9999[0-8][0-9]{8}|99999[0-8][0-9]{7}|999999[0-8][0-9]{6}|9999999[0-8][0-9]{5}|99999999[0-8][0-9]{4}|999999999[0-8][0-9]{3}|9999999999[0-8][0-9]{2}|99999999999[0-8][0-9]|999999999999[0-9]|[1-8][0-9]{13}|9[0-8][0-9]{12}|99[0-8][0-9]{11}|999[0-8][0-9]{10}|9999[0-8][0-9]{9}|99999[0-8][0-9]{8}|999999[0-8][0-9]{7}|9999999[0-8][0-9]{6}|99999999[0-8][0-9]{5}|999999999[0-8][0-9]{4}|9999999999[0-8][0-9]{3}|99999999999[0-8][0-9]{2}|999999999999[0-8][0-9]|9999999999999[0-9]|[1-8][0-9]{14}|9[0-8][0-9]{13}|99[0-8][0-9]{12}|999[0-8][0-9]{11}|9999[0-8][0-9]{10}|99999[0-8][0-9]{9}|999999[0-8][0-9]{8}|9999999[0-8][0-9]{7}|99999999[0-8][0-9]{6}|999999999[0-8][0-9]{5}|9999999999[0-8][0-9]{4}|99999999999[0-8][0-9]{3}|999999999999[0-8][0-9]{2}|9999999999999[0-8][0-9]|99999999999999[0-9]|[1-8][0-9]{15}|9[0-8][0-9]{14}|99[0-8][0-9]{13}|999[0-8][0-9]{12}|9999[0-8][0-9]{11}|99999[0-8][0-9]{10}|999999[0-8][0-9]{9}|9999999[0-8][0-9]{8}|99999999[0-8][0-9]{7}|999999999[0-8][0-9]{6}|9999999999[0-8][0-9]{5}|99999999999[0-8][0-9]{4}|999999999999[0-8][0-9]{3}|9999999999999[0-8][0-9]{2}|99999999999999[0-8][0-9]|999999999999999[0-9]|[1-8][0-9]{16}|9[0-8][0-9]{15}|99[0-8][0-9]{14}|999[0-8][0-9]{13}|9999[0-8][0-9]{12}|99999[0-8][0-9]{11}|999999[0-8][0-9]{10}|9999999[0-8][0-9]{9}|99999999[0-8][0-9]{8}|999999999[0-8][0-9]{7}|9999999999[0-8][0-9]{6}|99999999999[0-8][0-9]{5}|999999999999[0-8][0-9]{4}|9999999999999[0-8][0-9]{3}|99999999999999[0-8][0-9]{2}|999999999999999[0-8][0-9]|9999999999999999[0-9]|[1-8][0-9]{17}|9[0-8][0-9]{16}|99[0-8][0-9]{15}|999[0-8][0-9]{14}|9999[0-8][0-9]{13}|99999[0-8][0-9]{12}|999999[0-8][0-9]{11}|9999999[0-8][0-9]{10}|99999999[0-8][0-9]{9}|999999999[0-8][0-9]{8}|9999999999[0-8][0-9]{7}|99999999999[0-8][0-9]{6}|999999999999[0-8][0-9]{5}|9999999999999[0-8][0-9]{4}|99999999999999[0-8][0-9]{3}|999999999999999[0-8][0-9]{2}|9999999999999999[0-8][0-9]|99999999999999999[0-9]|[1-8][0-9]{18}|9[01][0-9]{17}|92[01][0-9]{16}|922[0-2][0-9]{15}|9223[0-2][0-9]{14}|92233[0-6][0-9]{13}|922337[01][0-9]{12}|92233720[0-2][0-9]{10}|922337203[0-5][0-9]{9}|9223372036[0-7][0-9]{8}|92233720368[0-4][0-9]{7}|922337203685[0-3][0-9]{6}|9223372036854[0-6][0-9]{5}|92233720368547[0-6][0-9]{4}|922337203685477[0-4][0-9]{3}|9223372036854775[0-7][0-9]{2}|922337203685477580[0-7]))|0))$/D';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validLong(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$long_regex,
27+
$message ?: '%s is not a valid xs:long',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/LongValue.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 LongValue 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::validLong($this->sanitizeValue($value), SchemaViolationException::class);
25+
}
26+
}

tests/Assert/LongTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\LongTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class LongTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $long
24+
*/
25+
#[DataProvider('provideLong')]
26+
public function testValidLong(bool $shouldPass, string $long): void
27+
{
28+
try {
29+
Assert::validLong($long);
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 provideLong(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive signed' => [true, '+9223372036854775807'],
45+
'valid negative signed' => [true, '-9223372036854775808'],
46+
'valid leading zeros' => [true, '-0001'],
47+
'valid zero' => [true, '0'],
48+
'invalid positive signed out-of-bounds' => [false, '+9223372036854775808'],
49+
'invalid negative signed out-of-bounds' => [false, '-9223372036854775809'],
50+
'invalid with space' => [false, '1 234'],
51+
'invalid with fractional' => [false, '1234.'],
52+
'invalid with thousands-delimiter' => [false, '+1,234'],
53+
];
54+
}
55+
}

tests/Type/LongValueTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\LongValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\LongValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(LongValue::class)]
19+
final class LongValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $long
24+
*/
25+
#[DataProvider('provideLong')]
26+
public function testLong(bool $shouldPass, string $long): void
27+
{
28+
try {
29+
LongValue::fromString($long);
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 provideLong(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive signed' => [true, '+9223372036854775807'],
45+
'valid negative signed' => [true, '-9223372036854775808'],
46+
'valid leading zeros' => [true, '-0001'],
47+
'valid zero' => [true, '0'],
48+
'valid with whitespace collapse' => [true, " 1 234 \n"],
49+
'invalid positive signed out-of-bounds' => [false, '+9223372036854775808'],
50+
'invalid negative signed out-of-bounds' => [false, '-9223372036854775809'],
51+
'invalid with fractional' => [false, '1234.'],
52+
'invalid with thousands-delimiter' => [false, '+1,234'],
53+
];
54+
}
55+
}

0 commit comments

Comments
 (0)