| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Server\Core\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 9 |
|
| 10 |
/** |
| 11 |
* @mcp-domain Server |
| 12 |
* @mcp-subdomain Core |
| 13 |
* @mcp-version 2025-11-25 |
| 14 |
*/ |
| 15 |
class CompleteResultCompletion extends AbstractDataTransferObject |
| 16 |
{ |
| 17 |
use ValidatesRequiredFields; |
| 18 |
|
| 19 |
/** Maximum number of items allowed in values per MCP spec */ |
| 20 |
public const MAX_VALUES = 100; |
| 21 |
|
| 22 |
/** |
| 23 |
* An array of completion values. Must not exceed 100 items. |
| 24 |
* |
| 25 |
* @var array<string> |
| 26 |
*/ |
| 27 |
protected array $values; |
| 28 |
|
| 29 |
/** |
| 30 |
* The total number of completion options available. This can exceed the number of values actually sent in the response. |
| 31 |
* |
| 32 |
* @var int|null |
| 33 |
*/ |
| 34 |
protected ?int $total; |
| 35 |
|
| 36 |
/** |
| 37 |
* Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown. |
| 38 |
* |
| 39 |
* @var bool|null |
| 40 |
*/ |
| 41 |
protected ?bool $hasMore; |
| 42 |
|
| 43 |
/** |
| 44 |
* @param array<string> $values |
| 45 |
* @param int|null $total |
| 46 |
* @param bool|null $hasMore |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
array $values, |
| 50 |
?int $total = null, |
| 51 |
?bool $hasMore = null |
| 52 |
) { |
| 53 |
$this->values = $values; |
| 54 |
$this->total = $total; |
| 55 |
$this->hasMore = $hasMore; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Creates an instance from an array. |
| 60 |
* |
| 61 |
* @param array{ |
| 62 |
* values: array<string>, |
| 63 |
* total?: int|null, |
| 64 |
* hasMore?: bool|null |
| 65 |
* } $data |
| 66 |
* @phpstan-param array<string, mixed> $data |
| 67 |
* @return self |
| 68 |
*/ |
| 69 |
public static function fromArray(array $data): self |
| 70 |
{ |
| 71 |
self::assertRequired($data, ['values']); |
| 72 |
|
| 73 |
if (is_array($data['values']) && count($data['values']) > self::MAX_VALUES) { |
| 74 |
throw new \InvalidArgumentException(sprintf( |
| 75 |
'%s::values must not exceed %d items, got %d', |
| 76 |
static::class, |
| 77 |
self::MAX_VALUES, |
| 78 |
count($data['values']) |
| 79 |
)); |
| 80 |
} |
| 81 |
|
| 82 |
return new self( |
| 83 |
self::asStringArray($data['values']), |
| 84 |
self::asIntOrNull($data['total'] ?? null), |
| 85 |
self::asBoolOrNull($data['hasMore'] ?? null) |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Converts the instance to an array. |
| 91 |
* |
| 92 |
* @return array<string, mixed> |
| 93 |
*/ |
| 94 |
public function toArray(): array |
| 95 |
{ |
| 96 |
$result = []; |
| 97 |
|
| 98 |
$result['values'] = $this->values; |
| 99 |
if ($this->total !== null) { |
| 100 |
$result['total'] = $this->total; |
| 101 |
} |
| 102 |
if ($this->hasMore !== null) { |
| 103 |
$result['hasMore'] = $this->hasMore; |
| 104 |
} |
| 105 |
|
| 106 |
return $result; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @return array<string> |
| 111 |
*/ |
| 112 |
public function getValues(): array |
| 113 |
{ |
| 114 |
return $this->values; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @return int|null |
| 119 |
*/ |
| 120 |
public function getTotal(): ?int |
| 121 |
{ |
| 122 |
return $this->total; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @return bool|null |
| 127 |
*/ |
| 128 |
public function getHasMore(): ?bool |
| 129 |
{ |
| 130 |
return $this->hasMore; |
| 131 |
} |
| 132 |
} |
| 133 |
|