-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create assertion and type-class for xs:double
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XML\Assert; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* @package simplesamlphp/xml-common | ||
*/ | ||
trait DoubleTrait | ||
{ | ||
/** @var string */ | ||
private static string $double_regex = '/^(([+-]?([0-9]+[.][0-9]*|[.][0-9]+)([e][+-]?[0-9]+)?)|NaN|[-]?FIN)$/D'; | ||
|
||
/** | ||
* @param string $value | ||
* @param string $message | ||
*/ | ||
protected static function validDouble(string $value, string $message = ''): void | ||
{ | ||
parent::regex( | ||
$value, | ||
self::$double_regex, | ||
$message ?: '%s is not a valid xs:double', | ||
InvalidArgumentException::class, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XML\Type; | ||
|
||
use SimpleSAML\XML\Assert\Assert; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
|
||
/** | ||
* @package simplesaml/xml-common | ||
*/ | ||
class DoubleValue extends AbstractValueType | ||
{ | ||
/** | ||
* Sanitize the value. | ||
* | ||
* @param string $value The unsanitized value | ||
* @return string | ||
*/ | ||
protected function sanitizeValue(string $value): string | ||
{ | ||
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value); | ||
} | ||
|
||
|
||
/** | ||
* Validate the value. | ||
* | ||
* @param string $value | ||
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure | ||
* @return void | ||
*/ | ||
protected function validateValue(string $value): void | ||
{ | ||
Assert::validDouble($this->sanitizeValue($value), SchemaViolationException::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML\Assert; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Assert\AssertionFailedException; | ||
use SimpleSAML\XML\Assert\Assert; | ||
|
||
/** | ||
* Class \SimpleSAML\Test\XML\Assert\DoubleTest | ||
* | ||
* @package simplesamlphp/xml-common | ||
*/ | ||
#[CoversClass(Assert::class)] | ||
final class DoubleTest extends TestCase | ||
{ | ||
/** | ||
* @param boolean $shouldPass | ||
* @param string $double | ||
*/ | ||
#[DataProvider('provideDouble')] | ||
public function testValidDouble(bool $shouldPass, string $double): void | ||
{ | ||
try { | ||
Assert::validDouble($double); | ||
$this->assertTrue($shouldPass); | ||
} catch (AssertionFailedException $e) { | ||
$this->assertFalse($shouldPass); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return array<string, array{0: bool, 1: string}> | ||
*/ | ||
public static function provideDouble(): array | ||
{ | ||
return [ | ||
'empty' => [false, ''], | ||
'valid positive signed' => [true, '+123.456'], | ||
'valid negative signed' => [true, '-123.456'], | ||
'valid non-signed' => [true, '123.456'], | ||
'valid leading zeros' => [true, '-0123.456'], | ||
'valid zero' => [true, '0.0'], | ||
'valid NaN' => [true, 'NaN'], | ||
'case-sensitive NaN' => [false, 'NAN'], | ||
'valid negative FIN' => [true, '-FIN'], | ||
'valid FIN' => [true, 'FIN'], | ||
'invalid +FIN' => [false, '+FIN'], | ||
'invalid with space' => [false, '1 23.0'], | ||
'invalid without fractional' => [false, '123'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\XML\Type; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
use SimpleSAML\XML\Type\DoubleValue; | ||
|
||
/** | ||
* Class \SimpleSAML\Test\Type\DoubleValueTest | ||
* | ||
* @package simplesamlphp/xml-common | ||
*/ | ||
#[CoversClass(DoubleValue::class)] | ||
final class DoubleValueTest extends TestCase | ||
{ | ||
/** | ||
* @param boolean $shouldPass | ||
* @param string $double | ||
*/ | ||
#[DataProvider('provideDouble')] | ||
public function testDouble(bool $shouldPass, string $double): void | ||
{ | ||
try { | ||
DoubleValue::fromString($double); | ||
$this->assertTrue($shouldPass); | ||
} catch (SchemaViolationException $e) { | ||
$this->assertFalse($shouldPass); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return array<string, array{0: bool, 1: string}> | ||
*/ | ||
public static function provideDouble(): array | ||
{ | ||
return [ | ||
'empty' => [false, ''], | ||
'valid positive signed' => [true, '+123.456'], | ||
'valid negative signed' => [true, '-123.456'], | ||
'valid non-signed' => [true, '123.456'], | ||
'valid leading zeros' => [true, '-0123.456'], | ||
'valid zero' => [true, '0.0'], | ||
'valid NaN' => [true, 'NaN'], | ||
'case-sensitive NaN' => [false, 'NAN'], | ||
'valid negative FIN' => [true, '-FIN'], | ||
'valid FIN' => [true, 'FIN'], | ||
'invalid +FIN' => [false, '+FIN'], | ||
'valid with whitespace collapse' => [true, ' 1 234.456 '], | ||
'invalid without fractional' => [false, '123'], | ||
]; | ||
} | ||
} |