|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Laravel\Mcp; |
| 6 | + |
| 7 | +use Illuminate\Support\Arr; |
| 8 | +use Illuminate\Support\Collection; |
| 9 | +use Illuminate\Support\Traits\Conditionable; |
| 10 | +use Illuminate\Support\Traits\Macroable; |
| 11 | +use InvalidArgumentException; |
| 12 | +use Laravel\Mcp\Server\Concerns\HasMeta; |
| 13 | + |
| 14 | +class ResponseFactory |
| 15 | +{ |
| 16 | + use Conditionable; |
| 17 | + use HasMeta; |
| 18 | + use Macroable; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var Collection<int, Response> |
| 22 | + */ |
| 23 | + protected Collection $responses; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param Response|array<int, Response> $responses |
| 27 | + */ |
| 28 | + public function __construct(Response|array $responses) |
| 29 | + { |
| 30 | + $wrapped = Arr::wrap($responses); |
| 31 | + |
| 32 | + foreach ($wrapped as $index => $response) { |
| 33 | + if (! $response instanceof Response) { |
| 34 | + throw new InvalidArgumentException( |
| 35 | + "Invalid response type at index {$index}: Expected ".Response::class.', but received '.get_debug_type($response).'.' |
| 36 | + ); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + $this->responses = collect($wrapped); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param string|array<string, mixed> $meta |
| 45 | + */ |
| 46 | + public function withMeta(string|array $meta, mixed $value = null): static |
| 47 | + { |
| 48 | + $this->setMeta($meta, $value); |
| 49 | + |
| 50 | + return $this; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return Collection<int, Response> |
| 55 | + */ |
| 56 | + public function responses(): Collection |
| 57 | + { |
| 58 | + return $this->responses; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return array<string, mixed>|null |
| 63 | + */ |
| 64 | + public function getMeta(): ?array |
| 65 | + { |
| 66 | + return $this->meta; |
| 67 | + } |
| 68 | +} |
0 commit comments