Skip to content

Commit

Permalink
Merge pull request #11 from microsoft/dev
Browse files Browse the repository at this point in the history
Merge to Main
  • Loading branch information
Ndiritu authored Nov 7, 2022
2 parents ac526b9 + bb679f8 commit 1f49fb4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
75 changes: 62 additions & 13 deletions src/RequestInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use League\Uri\Contracts\UriException;
use League\Uri\UriTemplate;
use Microsoft\Kiota\Abstractions\Serialization\Parsable;
use Microsoft\Kiota\Abstractions\Types\Date;
use Psr\Http\Message\StreamInterface;
use RuntimeException;

Expand Down Expand Up @@ -147,28 +146,78 @@ public function setStreamContent(StreamInterface $value): void {
}

/**
* Sets the request body from a model with the specified content type.
* Sets the request body from a model using the specified content type.
*
* @param RequestAdapter $requestAdapter The adapter service to get the serialization writer from.
* @param string $contentType the content type.
* @param Parsable ...$values the models.
* @param Parsable $value the models.
*/
public function setContentFromParsable(RequestAdapter $requestAdapter, string $contentType, Parsable ...$values): void {
if (empty($values)) {
throw new InvalidArgumentException('$values cannot be empty.');
public function setContentFromParsable(RequestAdapter $requestAdapter, string $contentType, Parsable $value): void {
try {
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeObjectValue(null, $value);
$this->headers[self::$contentTypeHeader] = $contentType;
$this->content = $writer->getSerializedContent();
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
}
}

/**
* Sets the request body from a collection of models using the specified content type
*
* @param RequestAdapter $requestAdapter
* @param string $contentType
* @param Parsable[] $values
* @return void
*/
public function setContentFromParsableCollection(RequestAdapter $requestAdapter, string $contentType, array $values): void
{
try {
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeCollectionOfObjectValues(null, $values);
$this->headers[self::$contentTypeHeader] = $contentType;
$this->content = $writer->getSerializedContent();
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
}
}

if (count($values) === 1) {
$writer->writeObjectValue(null, $values[0]);
} else {
$writer->writeCollectionOfObjectValues(null, $values);
}
/**
* Sets the request body from a scalar value(https://www.php.net/manual/en/language.types.intro.php)
*
* @param RequestAdapter $requestAdapter
* @param string $contentType
* @param int|string|bool|float $value
* @return void
*/
public function setContentFromScalar(RequestAdapter $requestAdapter, string $contentType, $value): void {
try {
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeAnyValue(null, $value);
$this->headers[self::$contentTypeHeader] = $contentType;
$this->content = $writer->getSerializedContent();
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
}
}

/**
* Sets the request body from a collection of scalar values(https://www.php.net/manual/en/language.types.intro.php) using the $contentType
*
* @param RequestAdapter $requestAdapter
* @param string $contentType
* @param array<int|float|string|bool> $values
* @return void
*/
public function setContentFromScalarCollection(RequestAdapter $requestAdapter, string $contentType, array $values): void {
try {
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeCollectionOfPrimitiveValues(null, $values);
$this->headers[self::$contentTypeHeader] = $contentType;
$this->content = $writer->getSerializedContent();
} catch (Exception $ex) {
throw new RuntimeException('could not serialize payload.', 1, $ex);
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Serialization/SerializationWriterFactoryRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getSerializationWriter(string $contentType): SerializationWriter
if (array_key_exists($cleanedContentType, $this->contentTypeAssociatedFactories)) {
return $this->contentTypeAssociatedFactories[$cleanedContentType]->getSerializationWriter($cleanedContentType);
}
throw new UnexpectedValueException('Content type ' . $contentType . ' does not have a factory to be parsed');
throw new UnexpectedValueException('Content type ' . $contentType . ' does not have a factory to be parsed. Add a SerializationWriter factory to support the content type.');
}

public function getValidContentType(): string {
Expand All @@ -66,4 +66,4 @@ public static function getDefaultInstance(): SerializationWriterFactoryRegistry
}
return self::$defaultInstance;
}
}
}

0 comments on commit 1f49fb4

Please sign in to comment.