Skip to content

Commit e39b59d

Browse files
committed
Further improve regex and unit tests
1 parent 141e178 commit e39b59d

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ jobs:
154154
echo "Could not read extra.branch-alias.dev-master from composer.json" >&2
155155
exit 1
156156
fi
157-
echo "COMPOSER_ROOT_VERSION=$ROOT_VERSION" >> "GITHUB_ENV"
157+
echo "COMPOSER_ROOT_VERSION=$ROOT_VERSION" >> "$env:GITHUB_ENV"
158158
159159
- name: Cache composer dependencies
160160
uses: actions/cache@v5

src/XMLSchema/Type/DateTimeValue.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ class DateTimeValue extends AbstractAnySimpleType
3131
protected function sanitizeValue(string $value): string
3232
{
3333
$normalized = static::collapseWhitespace(static::normalizeWhitespace($value));
34+
$sanitized = preg_replace('/\.(\d{0,6})\d*/', '.$1', $normalized);
3435

35-
$sanitized = preg_replace('/\.0+/', '', $normalized);
36-
$sanitized = preg_replace('/\.(?!\d)/', '', $sanitized);
37-
38-
return $sanitized;
36+
// Remove all trailing zeros after the dot, and remove the dot if only zeros were present
37+
$sanitized = preg_replace('/\.(?=\d)(?:\d*?[1-9])?\K0+(?=[^0-9]|$)/', '', $sanitized);
38+
return preg_replace('/\.(?!\d)/', '', $sanitized);
3939
}
4040

4141

src/XMLSchema/Type/TimeValue.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ class TimeValue extends AbstractAnySimpleType
3131
protected function sanitizeValue(string $value): string
3232
{
3333
$normalized = static::collapseWhitespace(static::normalizeWhitespace($value));
34+
$sanitized = preg_replace('/\.(\d{0,6})\d*/', '.$1', $normalized);
3435

35-
$sanitized = preg_replace('/\.0+/', '', $normalized);
36-
$sanitized = preg_replace('/\.(?!\d)/', '', $sanitized);
37-
38-
return $sanitized;
36+
// Remove all trailing zeros after the dot, and remove the dot if only zeros were present
37+
$sanitized = preg_replace('/\.(?=\d)(?:\d*?[1-9])?\K0+(?=[^0-9]|$)/', '', $sanitized);
38+
return preg_replace('/\.(?!\d)/', '', $sanitized);
3939
}
4040

4141

tests/XMLSchema/Type/DateTimeValueTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ public function testFromDateTime(): void
5454
}
5555

5656

57+
/**
58+
*/
59+
public function testSubSeconds(): void
60+
{
61+
// Strip sub-second trailing zero's and make sure the decimal sign is removed
62+
$dateTimeValue = DateTimeValue::fromString('2001-10-26T21:32:52.00');
63+
$this->assertEquals('2001-10-26T21:32:52', $dateTimeValue->getValue());
64+
65+
// Strip sub-second trailing zero's
66+
$dateTimeValue = DateTimeValue::fromString('2001-10-26T21:32:52.12300');
67+
$this->assertEquals('2001-10-26T21:32:52.123', $dateTimeValue->getValue());
68+
69+
// Strip sub-seconds over microsecond precision
70+
$dateTimeValue = DateTimeValue::fromString('2001-10-26T21:32:52.1234567');
71+
$this->assertEquals('2001-10-26T21:32:52.123456', $dateTimeValue->getValue());
72+
73+
// Strip sub-second trailing zero's and make sure the decimal sign is removed
74+
$dateTimeValue = DateTimeValue::fromString('2001-10-26T21:32:52.00Z');
75+
$this->assertEquals('2001-10-26T21:32:52Z', $dateTimeValue->getValue());
76+
77+
// Strip sub-seconds over microsecond precision with timezone
78+
$dateTimeValue = DateTimeValue::fromString('2001-10-26T21:32:52.1234567+01:00');
79+
$this->assertEquals('2001-10-26T21:32:52.123456+01:00', $dateTimeValue->getValue());
80+
81+
// Strip sub-seconds over microsecond precision with timezone Zulu
82+
$dateTimeValue = DateTimeValue::fromString('2001-10-26T21:32:52.1234567Z');
83+
$this->assertEquals('2001-10-26T21:32:52.123456Z', $dateTimeValue->getValue());
84+
}
85+
86+
5787
/**
5888
* @return array<string, array{0: true, 1: string}>
5989
*/

tests/XMLSchema/Type/TimeValueTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,29 @@ public function testFromDateTime(): void
5858
*/
5959
public function testSubSeconds(): void
6060
{
61+
// Strip sub-second trailing zero's and make sure the decimal sign is removed
6162
$timeValue = TimeValue::fromString('21:32:52.00');
6263
$this->assertEquals('21:32:52', $timeValue->getValue());
64+
65+
// Strip sub-second trailing zero's
66+
$timeValue = TimeValue::fromString('21:32:52.12300');
67+
$this->assertEquals('21:32:52.123', $timeValue->getValue());
68+
69+
// Strip sub-seconds over microsecond precision
70+
$timeValue = TimeValue::fromString('21:32:52.1234567');
71+
$this->assertEquals('21:32:52.123456', $timeValue->getValue());
72+
73+
// Strip sub-second trailing zero's and make sure the decimal sign is removed
74+
$timeValue = TimeValue::fromString('21:32:52.00Z');
75+
$this->assertEquals('21:32:52Z', $timeValue->getValue());
76+
77+
// Strip sub-seconds over microsecond precision with timezone
78+
$timeValue = TimeValue::fromString('21:32:52.1234567+01:00');
79+
$this->assertEquals('21:32:52.123456+01:00', $timeValue->getValue());
80+
81+
// Strip sub-seconds over microsecond precision with timezone Zulu
82+
$timeValue = TimeValue::fromString('21:32:52.1234567Z');
83+
$this->assertEquals('21:32:52.123456Z', $timeValue->getValue());
6384
}
6485

6586

0 commit comments

Comments
 (0)