diff --git a/.github/workflows/auto-merge-dependabot.yml b/.github/workflows/auto-merge-dependabot.yml index 145487d..72d96dd 100644 --- a/.github/workflows/auto-merge-dependabot.yml +++ b/.github/workflows/auto-merge-dependabot.yml @@ -19,7 +19,7 @@ jobs: steps: - name: Dependabot metadata id: metadata - uses: dependabot/fetch-metadata@v2.0.0 + uses: dependabot/fetch-metadata@v2.1.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/CHANGELOG.md b/CHANGELOG.md index 675d477..c0dfc0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.3.0] + +### Added +- Provide helper traits for dealing with DateIntervals, booleans and DateTime. [#133](https://github.com/microsoft/kiota-abstractions-php/pull/133) + +### Changed + ## [1.2.0] ### Added diff --git a/README.md b/README.md index b19b5a5..1af5654 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ run `composer require microsoft/kiota-abstractions` or add the following to your ```Shell { "require": { - "microsoft/kiota-abstractions": "^1.2.0" + "microsoft/kiota-abstractions": "^1.3.0" } } ``` diff --git a/composer.json b/composer.json index f9781b4..7f8ebc7 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "doctrine/annotations": "^1.13 || ^2.0", "open-telemetry/sdk": "^1.0.0", "ramsey/uuid": "^3 || ^4", - "stduritemplate/stduritemplate": "^0.0.53 || ^0.0.54 || ^0.0.55", + "stduritemplate/stduritemplate": "^0.0.53 || ^0.0.54 || ^0.0.55 || ^0.0.56 || ^0.0.57", "psr/http-message": "^1.1 || ^2.0" }, "require-dev": { diff --git a/src/Constants.php b/src/Constants.php index 1ac29df..fcc6d7a 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -4,5 +4,5 @@ final class Constants { - public const VERSION = '1.2.0'; + public const VERSION = '1.3.0'; } diff --git a/src/Serialization/ParseNodeFromStringTrait.php b/src/Serialization/ParseNodeFromStringTrait.php new file mode 100644 index 0000000..e083054 --- /dev/null +++ b/src/Serialization/ParseNodeFromStringTrait.php @@ -0,0 +1,31 @@ +invert = $invert; + return $dateInterval; + } +} diff --git a/src/Serialization/SerializationWriterToStringTrait.php b/src/Serialization/SerializationWriterToStringTrait.php new file mode 100644 index 0000000..c51e238 --- /dev/null +++ b/src/Serialization/SerializationWriterToStringTrait.php @@ -0,0 +1,40 @@ +y > 0 ? "%yY" : ""; + $month = $value->m > 0 ? "%mM" : ""; + $day = $value->d > 0 ? '%dD' : ""; + $hour = $value->h > 0 ? '%hH' : ""; + $minute = $value->i > 0 ? '%iM' : ""; + $second = $value->s > 0 ? '%sS' : ""; + $timePart = $hour.$minute.$second; + $time = !empty($timePart) ? "T$timePart" : ''; + return $value->format("%rP$year$month{$day}$time"); + } + public function getDateTimeValueAsString(DateTime $value): string + { + return $value->format(DateTimeInterface::RFC3339); + } + + public function getBooleanValueAsString(bool $value): string + { + return $value ? 'true' : 'false'; + } + + public function getStringValueAsEscapedString(string $value): string + { + return addcslashes($value, "\\\r\n\"\t"); + } +} diff --git a/tests/Serialization/ParseNodeTraitTest.php b/tests/Serialization/ParseNodeTraitTest.php new file mode 100644 index 0000000..c0d28db --- /dev/null +++ b/tests/Serialization/ParseNodeTraitTest.php @@ -0,0 +1,35 @@ +parseDateIntervalFromString('-P1DT23H59M19S'); + $dateInterval = new DateInterval('P1DT23H59M19S'); + $dateInterval->invert = 1; + + $this->assertEquals($dateInterval, $value); + } + public function testDateIntervalValueFromString(): void + { + $parseNode = new class + { + use ParseNodeFromStringTrait; + }; + $value = $parseNode->parseDateIntervalFromString('P1DT23H59M19S'); + $dateInterval = new DateInterval('P1DT23H59M19S'); + + $this->assertEquals($dateInterval, $value); + } +} diff --git a/tests/Serialization/SerializationWriterToStringTraitTest.php b/tests/Serialization/SerializationWriterToStringTraitTest.php new file mode 100644 index 0000000..11f792d --- /dev/null +++ b/tests/Serialization/SerializationWriterToStringTraitTest.php @@ -0,0 +1,57 @@ +invert = 1; + $dateInterval2 = new DateInterval('P1DT11S'); + $dateInterval1ToString = $serializationWriter->getDateIntervalValueAsString($dateInterval); + $dateInterval2ToString = $serializationWriter->getDateIntervalValueAsString($dateInterval2); + $this->assertEquals('-P1D', $dateInterval1ToString); + $this->assertEquals('P1DT11S', $dateInterval2ToString); + } + + public function testGetDateTimeValueAsString(): void + { + $serializationWriter = new class { + use SerializationWriterToStringTrait; + }; + $dateTime = new DateTime('2024-04-29T15:12'); + $dateTimeString = $serializationWriter->getDateTimeValueAsString($dateTime); + $this->assertEquals('2024-04-29T15:12:00+00:00', $dateTimeString); + } + + public function testGetBooleanValueAsString(): void + { + $serializationWriter = new class { + use SerializationWriterToStringTrait; + }; + + $booleanValue = $serializationWriter->getBooleanValueAsString(true); + $this->assertEquals('true', $booleanValue); + $booleanValue = $serializationWriter->getBooleanValueAsString(false); + $this->assertEquals('false', $booleanValue); + } + + public function testGetEscapedValueAsEscapedString(): void + { + $serializationWriter = new class { + use SerializationWriterToStringTrait; + }; + $originalString = 'Kenneth\n\Hello world'; + $escapedString = $serializationWriter->getStringValueAsEscapedString($originalString); + $this->assertEquals('Kenneth\\\n\\\Hello world', $escapedString); + } +}