|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Gemini\Data; |
| 6 | + |
| 7 | +use Gemini\Contracts\Arrayable; |
| 8 | +use InvalidArgumentException; |
| 9 | + |
| 10 | +/** |
| 11 | + * Metadata about cached content. |
| 12 | + * |
| 13 | + * @link https://ai.google.dev/api/caching#CachedContent |
| 14 | + */ |
| 15 | +final class CachedContent implements Arrayable |
| 16 | +{ |
| 17 | + public function __construct( |
| 18 | + public readonly string $name, |
| 19 | + public readonly string $model, |
| 20 | + public readonly ?string $displayName, |
| 21 | + public readonly UsageMetadata $usageMetadata, |
| 22 | + public readonly string $createTime, |
| 23 | + public readonly string $updateTime, |
| 24 | + public readonly string $expireTime, |
| 25 | + ) {} |
| 26 | + |
| 27 | + /** |
| 28 | + * @param array<string, mixed> $attributes |
| 29 | + */ |
| 30 | + public static function from(array $attributes): self |
| 31 | + { |
| 32 | + |
| 33 | + if (! is_string($attributes['name'] ?? null)) { |
| 34 | + throw new InvalidArgumentException('Name must be a string'); |
| 35 | + } |
| 36 | + |
| 37 | + if (! is_string($attributes['model'] ?? null)) { |
| 38 | + throw new InvalidArgumentException('Model must be a string'); |
| 39 | + } |
| 40 | + |
| 41 | + if (isset($attributes['displayName']) && ! is_string($attributes['displayName'])) { |
| 42 | + throw new InvalidArgumentException('DisplayName must be a string'); |
| 43 | + } |
| 44 | + |
| 45 | + if (! is_array($attributes['usageMetadata'] ?? null)) { |
| 46 | + throw new InvalidArgumentException('UsageMetadata must be an array'); |
| 47 | + } |
| 48 | + |
| 49 | + if (! is_string($attributes['createTime'] ?? null)) { |
| 50 | + throw new InvalidArgumentException('CreateTime must be a string'); |
| 51 | + } |
| 52 | + |
| 53 | + if (! is_string($attributes['updateTime'] ?? null)) { |
| 54 | + throw new InvalidArgumentException('UpdateTime must be a string'); |
| 55 | + } |
| 56 | + |
| 57 | + if (! is_string($attributes['expireTime'] ?? null)) { |
| 58 | + throw new InvalidArgumentException('ExpireTime must be a string'); |
| 59 | + } |
| 60 | + |
| 61 | + $name = $attributes['name']; |
| 62 | + $model = $attributes['model']; |
| 63 | + /** @var string|null $displayName */ |
| 64 | + $displayName = $attributes['displayName'] ?? null; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var array{ |
| 68 | + * promptTokenCount: int, |
| 69 | + * totalTokenCount: int, |
| 70 | + * candidatesTokenCount: ?int, |
| 71 | + * cachedContentTokenCount: ?int, |
| 72 | + * toolUsePromptTokenCount: ?int, |
| 73 | + * thoughtsTokenCount: ?int, |
| 74 | + * promptTokensDetails: list<array{modality: string, tokenCount: int}>|null, |
| 75 | + * cacheTokensDetails: list<array{modality: string, tokenCount: int}>|null, |
| 76 | + * candidatesTokensDetails: list<array{modality: string, tokenCount: int}>|null, |
| 77 | + * toolUsePromptTokensDetails: list<array{modality: string, tokenCount: int}>|null |
| 78 | + * } $usageMetadataData |
| 79 | + */ |
| 80 | + $usageMetadataData = $attributes['usageMetadata']; |
| 81 | + $usageMetadata = UsageMetadata::from($usageMetadataData); |
| 82 | + $createTime = $attributes['createTime']; |
| 83 | + $updateTime = $attributes['updateTime']; |
| 84 | + $expireTime = $attributes['expireTime']; |
| 85 | + |
| 86 | + return new self( |
| 87 | + name: $name, |
| 88 | + model: $model, |
| 89 | + displayName: $displayName, |
| 90 | + usageMetadata: $usageMetadata, |
| 91 | + createTime: $createTime, |
| 92 | + updateTime: $updateTime, |
| 93 | + expireTime: $expireTime, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + public function toArray(): array |
| 98 | + { |
| 99 | + return [ |
| 100 | + 'name' => $this->name, |
| 101 | + 'model' => $this->model, |
| 102 | + 'displayName' => $this->displayName, |
| 103 | + 'usageMetadata' => $this->usageMetadata->toArray(), |
| 104 | + 'createTime' => $this->createTime, |
| 105 | + 'updateTime' => $this->updateTime, |
| 106 | + 'expireTime' => $this->expireTime, |
| 107 | + ]; |
| 108 | + } |
| 109 | +} |
0 commit comments