|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Schema\Content; |
| 13 | + |
| 14 | +use Mcp\Exception\InvalidArgumentException; |
| 15 | +use Mcp\Exception\RuntimeException; |
| 16 | +use Mcp\Schema\Annotations; |
| 17 | + |
| 18 | +/** |
| 19 | + * Represents audio content in MCP. |
| 20 | + * |
| 21 | + * @author Kyrian Obikwelu <[email protected]> |
| 22 | + */ |
| 23 | +class AudioContent extends Content |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + public readonly string $data, |
| 27 | + public readonly string $mimeType, |
| 28 | + public readonly ?Annotations $annotations = null, |
| 29 | + ) { |
| 30 | + parent::__construct('audio'); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Convert the content to an array. |
| 35 | + * |
| 36 | + * @return array{type: string, data: string, mimeType: string, annotations?: array} |
| 37 | + */ |
| 38 | + public function toArray(): array |
| 39 | + { |
| 40 | + $result = [ |
| 41 | + 'type' => 'audio', |
| 42 | + 'data' => $this->data, |
| 43 | + 'mimeType' => $this->mimeType, |
| 44 | + ]; |
| 45 | + |
| 46 | + if (null !== $this->annotations) { |
| 47 | + $result['annotations'] = $this->annotations->toArray(); |
| 48 | + } |
| 49 | + |
| 50 | + return $result; |
| 51 | + } |
| 52 | + |
| 53 | + public static function fromArray(array $data): static |
| 54 | + { |
| 55 | + if (!isset($data['data']) || !isset($data['mimeType'])) { |
| 56 | + throw new InvalidArgumentException('Invalid or missing "data" or "mimeType" in AudioContent data.'); |
| 57 | + } |
| 58 | + |
| 59 | + return new static($data['data'], $data['mimeType'], $data['annotations'] ?? null); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Create a new AudioContent from a file path. |
| 64 | + * |
| 65 | + * @param string $path Path to the audio file |
| 66 | + * @param string|null $mimeType Optional MIME type override |
| 67 | + * @param ?Annotations $annotations Optional annotations describing the content |
| 68 | + * |
| 69 | + * @throws InvalidArgumentException If the file doesn't exist |
| 70 | + */ |
| 71 | + public static function fromFile(string $path, ?string $mimeType = null, ?Annotations $annotations = null): static |
| 72 | + { |
| 73 | + if (!file_exists($path)) { |
| 74 | + throw new InvalidArgumentException(\sprintf('Audio file not found: "%s"', $path)); |
| 75 | + } |
| 76 | + |
| 77 | + $content = file_get_contents($path); |
| 78 | + if (false === $content) { |
| 79 | + throw new RuntimeException(\sprintf('Could not read audio file: "%s"', $path)); |
| 80 | + } |
| 81 | + $data = base64_encode($content); |
| 82 | + $detectedMime = $mimeType ?? mime_content_type($path) ?: 'application/octet-stream'; |
| 83 | + |
| 84 | + return new static($data, $detectedMime, $annotations); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Create a new AudioContent from a string. |
| 89 | + * |
| 90 | + * @param string $data The audio data |
| 91 | + * @param string $mimeType MIME type of the audio |
| 92 | + * @param ?Annotations $annotations Optional annotations describing the content |
| 93 | + */ |
| 94 | + public static function fromString(string $data, string $mimeType, ?Annotations $annotations = null): static |
| 95 | + { |
| 96 | + return new static(base64_encode($data), $mimeType, $annotations); |
| 97 | + } |
| 98 | + |
| 99 | + public function jsonSerialize(): array |
| 100 | + { |
| 101 | + return $this->toArray(); |
| 102 | + } |
| 103 | +} |
0 commit comments