-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from microsoft/dev
Release 1.3.0
- Loading branch information
Showing
9 changed files
with
174 additions
and
4 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
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
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 |
---|---|---|
|
@@ -4,5 +4,5 @@ | |
|
||
final class Constants | ||
{ | ||
public const VERSION = '1.2.0'; | ||
public const VERSION = '1.3.0'; | ||
} |
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,31 @@ | ||
<?php | ||
|
||
namespace Microsoft\Kiota\Abstractions\Serialization; | ||
|
||
|
||
use DateInterval; | ||
use Exception; | ||
|
||
/** | ||
* This trait contains utility functions for parsing strings to different types. | ||
*/ | ||
trait ParseNodeFromStringTrait | ||
{ | ||
/** | ||
* @throws Exception | ||
*/ | ||
public function parseDateIntervalFromString(string $value): DateInterval | ||
{ | ||
$negativeValPosition = strpos($value, '-'); | ||
$invert = 0; | ||
$str = $value; | ||
if ($negativeValPosition !== false && $negativeValPosition === 0) { | ||
// Invert the interval | ||
$invert = 1; | ||
$str = substr($value, 1); | ||
} | ||
$dateInterval = new DateInterval($str); | ||
$dateInterval->invert = $invert; | ||
return $dateInterval; | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace Microsoft\Kiota\Abstractions\Serialization; | ||
|
||
use DateInterval; | ||
use DateTime; | ||
use DateTimeInterface; | ||
|
||
/** | ||
* This Trait contains common to-string conversion for common types. | ||
*/ | ||
trait SerializationWriterToStringTrait | ||
{ | ||
public function getDateIntervalValueAsString(DateInterval $value): string | ||
{ | ||
$year = $value->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"); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Microsoft\Kiota\Abstractions\Tests\Serialization; | ||
|
||
use DateInterval; | ||
use Microsoft\Kiota\Abstractions\Serialization\ParseNodeFromStringTrait; | ||
use Microsoft\Kiota\Abstractions\Serialization\SerializationWriterToStringTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ParseNodeTraitTest extends TestCase | ||
{ | ||
public function testDateIntervalValueFromStringNegative(): void | ||
{ | ||
$parseNode = new class | ||
{ | ||
use ParseNodeFromStringTrait; | ||
}; | ||
$value = $parseNode->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); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
tests/Serialization/SerializationWriterToStringTraitTest.php
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,57 @@ | ||
<?php | ||
|
||
namespace Microsoft\Kiota\Abstractions\Tests\Serialization; | ||
|
||
use DateInterval; | ||
use DateTime; | ||
use Microsoft\Kiota\Abstractions\Serialization\SerializationWriterToStringTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SerializationWriterToStringTraitTest extends TestCase | ||
{ | ||
public function testGetDateIntervalValueAsString(): void | ||
{ | ||
$serializationWriter = new class { | ||
use SerializationWriterToStringTrait; | ||
}; | ||
$dateInterval = new DateInterval('P1D'); | ||
$dateInterval->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); | ||
} | ||
} |