Skip to content

Commit

Permalink
Merge pull request #145 from microsoft/dev
Browse files Browse the repository at this point in the history
Release 1.3.0
  • Loading branch information
SilasKenneth authored May 2, 2024
2 parents e065420 + 4daf25b commit a8dbca9
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto-merge-dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"

Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

final class Constants
{
public const VERSION = '1.2.0';
public const VERSION = '1.3.0';
}
31 changes: 31 additions & 0 deletions src/Serialization/ParseNodeFromStringTrait.php
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;
}
}
40 changes: 40 additions & 0 deletions src/Serialization/SerializationWriterToStringTrait.php
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");
}
}
35 changes: 35 additions & 0 deletions tests/Serialization/ParseNodeTraitTest.php
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 tests/Serialization/SerializationWriterToStringTraitTest.php
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);
}
}

0 comments on commit a8dbca9

Please sign in to comment.