diff --git a/src/Resource/BlobResourceContents.php b/src/Resource/BlobResourceContents.php index 441add5..b5eebb4 100644 --- a/src/Resource/BlobResourceContents.php +++ b/src/Resource/BlobResourceContents.php @@ -28,15 +28,11 @@ public function __construct( * Converts the blob resource contents to an array format. * Includes 'uri', 'blob', and 'mimeType'. * - * @return array{uri: string, blob: string, mimeType?: string} The array representation. + * @return array{uri: string, blob: string, mimeType: string} The array representation. */ public function toArray(): array { - $data = ['uri' => $this->uri, 'blob' => $this->blob]; - // $this->mimeType is guaranteed by constructor, but check doesn't hurt for consistency - if ($this->mimeType !== null) { - $data['mimeType'] = $this->mimeType; - } + $data = ['uri' => $this->uri, 'blob' => $this->blob, 'mimeType' => $this->mimeType]; return $data; } } diff --git a/src/Resource/Resource.php b/src/Resource/Resource.php index a1404c3..1116177 100644 --- a/src/Resource/Resource.php +++ b/src/Resource/Resource.php @@ -145,7 +145,7 @@ protected function text(string $text, ?string $mimeType = null, array $parameter return new TextResourceContents( $this->resolveUri($this->getUri(), $parameters), $text, - $mimeType + $mimeType ?? 'text/plain' ); } diff --git a/src/Resource/ResourceContents.php b/src/Resource/ResourceContents.php index 887d02e..8afcc97 100644 --- a/src/Resource/ResourceContents.php +++ b/src/Resource/ResourceContents.php @@ -11,17 +11,33 @@ */ abstract class ResourceContents { + /** + * The MIME type of the resource content. + * @var string + */ + public readonly string $mimeType; + /** * Constructs a ResourceContents instance. * * @param string $uri The URI of the resource content. This might be a resolved URI if the resource URI was a template. - * @param string|null $mimeType The MIME type of the resource content, if applicable. + * @param string $mimeType The MIME type of the resource content. */ public function __construct( /** The URI of the resource content. */ public readonly string $uri, - /** The MIME type of the resource content, if applicable. */ - public readonly ?string $mimeType = null + string $mimeType ) { + $this->mimeType = $mimeType; + } + + /** + * Converts the resource contents to an array format. + * + * @return array{uri: string, mimeType: string} The array representation. + */ + public function toArray(): array + { + return ['uri' => $this->uri, 'mimeType' => $this->mimeType]; } } diff --git a/src/Resource/TextResourceContents.php b/src/Resource/TextResourceContents.php index d92787d..7cd9f85 100644 --- a/src/Resource/TextResourceContents.php +++ b/src/Resource/TextResourceContents.php @@ -15,28 +15,26 @@ class TextResourceContents extends ResourceContents * * @param string $uri The URI of the resource. * @param string $text The text content. - * @param string|null $mimeType The MIME type of the content. Defaults to "text/plain" if null. + * @param string $mimeType The MIME type of the resource. Defaults to 'text/plain'. */ public function __construct( string $uri, public readonly string $text, - ?string $mimeType = null + string $mimeType = 'text/plain' ) { - parent::__construct($uri, $mimeType ?? 'text/plain'); + parent::__construct($uri, $mimeType); } /** * Converts the text resource contents to an array format. - * Includes 'uri', 'text', and 'mimeType'. + * Includes 'uri', 'mimeType', and 'text'. * - * @return array{uri: string, text: string, mimeType?: string} The array representation. + * @return array{uri: string, mimeType: string, text: string} The array representation. */ public function toArray(): array { - $data = ['uri' => $this->uri, 'text' => $this->text]; - if ($this->mimeType !== null) { - $data['mimeType'] = $this->mimeType; - } + $data = parent::toArray(); // Gets 'uri' and 'mimeType' from ResourceContents + $data['text'] = $this->text; return $data; } } diff --git a/tests/Resource/BlobResourceContentsTest.php b/tests/Resource/BlobResourceContentsTest.php new file mode 100644 index 0000000..395c19d --- /dev/null +++ b/tests/Resource/BlobResourceContentsTest.php @@ -0,0 +1,28 @@ + $uri, + 'blob' => $blobData, + 'mimeType' => $mimeType, + ]; + + $this->assertSame($expectedArray, $blobContents->toArray()); + } +} diff --git a/tests/Resource/TextResourceContentsTest.php b/tests/Resource/TextResourceContentsTest.php new file mode 100644 index 0000000..25c4a5d --- /dev/null +++ b/tests/Resource/TextResourceContentsTest.php @@ -0,0 +1,52 @@ +assertSame($uri, $contents->uri); + $this->assertSame($text, $contents->text); + $this->assertSame($mimeType, $contents->mimeType); + + $expectedArray = [ + 'uri' => $uri, + 'mimeType' => $mimeType, + 'text' => $text, + ]; + $this->assertSame($expectedArray, $contents->toArray()); + } + + public function testConstructorAndToArrayWithDefaultMimeType(): void + { + $uri = 'test://text/2'; + $text = 'Another message'; + $defaultMimeType = 'text/plain'; // Expected default + + // Instantiate without providing mimeType to use the default + $contents = new TextResourceContents($uri, $text); + + $this->assertSame($uri, $contents->uri); + $this->assertSame($text, $contents->text); + $this->assertSame($defaultMimeType, $contents->mimeType); + + $expectedArray = [ + 'uri' => $uri, + 'mimeType' => $defaultMimeType, + 'text' => $text, + ]; + $this->assertSame($expectedArray, $contents->toArray()); + } +}