Skip to content

Commit b48628b

Browse files
committed
Create assertion and type-class for xs:float
1 parent 8f3c1fb commit b48628b

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
2121
* @method static void validEntity(mixed $value, string $message = '', string $exception = '')
2222
* @method static void validEntities(mixed $value, string $message = '', string $exception = '')
23+
* @method static void validFloat(mixed $value, string $message = '', string $exception = '')
2324
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
2425
* @method static void validID(mixed $value, string $message = '', string $exception = '')
2526
* @method static void validIDRef(mixed $value, string $message = '', string $exception = '')
@@ -55,6 +56,7 @@
5556
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
5657
* @method static void nullOrValidEntity(mixed $value, string $message = '', string $exception = '')
5758
* @method static void nullOrValidEntities(mixed $value, string $message = '', string $exception = '')
59+
* @method static void nullOrValidFloat(mixed $value, string $message = '', string $exception = '')
5860
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
5961
* @method static void nullOrValidID(mixed $value, string $message = '', string $exception = '')
6062
* @method static void nullOrValidIDRef(mixed $value, string $message = '', string $exception = '')
@@ -90,6 +92,7 @@
9092
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
9193
* @method static void allValidEntity(mixed $value, string $message = '', string $exception = '')
9294
* @method static void allValidEntities(mixed $value, string $message = '', string $exception = '')
95+
* @method static void allValidFloat(mixed $value, string $message = '', string $exception = '')
9396
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
9497
* @method static void allValidID(mixed $value, string $message = '', string $exception = '')
9598
* @method static void allValidIDRef(mixed $value, string $message = '', string $exception = '')
@@ -129,6 +132,7 @@ class Assert extends BaseAssert
129132
use HexBinaryTrait;
130133
use EntitiesTrait;
131134
use EntityTrait;
135+
use FloatTrait;
132136
use IDTrait;
133137
use IDRefTrait;
134138
use IDRefsTrait;

src/Assert/FloatTrait.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 FloatTrait
13+
{
14+
/** @var string */
15+
private static string $float_regex = '/^(([+-]?([0-9]+[.][0-9]*|[.][0-9]+)([e][+-]?[0-9]+)?)|NaN|[-]?FIN)$/D';
16+
17+
/**
18+
* @param string $value
19+
* @param string $message
20+
*/
21+
protected static function validFloat(string $value, string $message = ''): void
22+
{
23+
parent::regex(
24+
$value,
25+
self::$float_regex,
26+
$message ?: '%s is not a valid xs:float',
27+
InvalidArgumentException::class,
28+
);
29+
}
30+
}

src/Type/FloatValue.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 FloatValue extends AbstractValueType
14+
{
15+
/**
16+
* Sanitize the value.
17+
*
18+
* @param string $value The unsanitized value
19+
* @return string
20+
*/
21+
protected function sanitizeValue(string $value): string
22+
{
23+
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
24+
}
25+
26+
27+
/**
28+
* Validate the value.
29+
*
30+
* @param string $value
31+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
32+
* @return void
33+
*/
34+
protected function validateValue(string $value): void
35+
{
36+
Assert::validFloat($this->sanitizeValue($value), SchemaViolationException::class);
37+
}
38+
}

tests/Assert/FloatTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\FloatTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class FloatTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $float
24+
*/
25+
#[DataProvider('provideFloat')]
26+
public function testValidFloat(bool $shouldPass, string $float): void
27+
{
28+
try {
29+
Assert::validFloat($float);
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 provideFloat(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive signed' => [true, '+123.456'],
45+
'valid negative signed' => [true, '-123.456'],
46+
'valid non-signed' => [true, '123.456'],
47+
'valid leading zeros' => [true, '-0123.456'],
48+
'valid zero' => [true, '0.0'],
49+
'valid NaN' => [true, 'NaN'],
50+
'case-sensitive NaN' => [false, 'NAN'],
51+
'valid negative FIN' => [true, '-FIN'],
52+
'valid FIN' => [true, 'FIN'],
53+
'invalid +FIN' => [false, '+FIN'],
54+
'invalid with space' => [false, '1 23.0'],
55+
'invalid without fractional' => [false, '123'],
56+
];
57+
}
58+
}

tests/Type/FloatValueTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\FloatValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\FloatValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(FloatValue::class)]
19+
final class FloatValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $float
24+
*/
25+
#[DataProvider('provideFloat')]
26+
public function testFloat(bool $shouldPass, string $float): void
27+
{
28+
try {
29+
FloatValue::fromString($float);
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 provideFloat(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid positive signed' => [true, '+123.456'],
45+
'valid negative signed' => [true, '-123.456'],
46+
'valid non-signed' => [true, '123.456'],
47+
'valid leading zeros' => [true, '-0123.456'],
48+
'valid zero' => [true, '0.0'],
49+
'valid NaN' => [true, 'NaN'],
50+
'case-sensitive NaN' => [false, 'NAN'],
51+
'valid negative FIN' => [true, '-FIN'],
52+
'valid FIN' => [true, 'FIN'],
53+
'invalid +FIN' => [false, '+FIN'],
54+
'valid with whitespace collapse' => [true, ' 1 234.456 '],
55+
'invalid without fractional' => [false, '123'],
56+
];
57+
}
58+
}

0 commit comments

Comments
 (0)