| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Client\Tasks\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 9 |
|
| 10 |
/** |
| 11 |
* Data associated with a task. |
| 12 |
* |
| 13 |
* @since 2025-11-25 |
| 14 |
* |
| 15 |
* @mcp-domain Client |
| 16 |
* @mcp-subdomain Tasks |
| 17 |
* @mcp-version 2025-11-25 |
| 18 |
*/ |
| 19 |
class Task extends AbstractDataTransferObject |
| 20 |
{ |
| 21 |
use ValidatesRequiredFields; |
| 22 |
|
| 23 |
/** |
| 24 |
* The task identifier. |
| 25 |
* |
| 26 |
* @since 2025-11-25 |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected string $taskId; |
| 31 |
|
| 32 |
/** |
| 33 |
* Current task state. |
| 34 |
* |
| 35 |
* @since 2025-11-25 |
| 36 |
* |
| 37 |
* @var 'working'|'input_required'|'completed'|'failed'|'cancelled' |
| 38 |
*/ |
| 39 |
protected string $status; |
| 40 |
|
| 41 |
/** |
| 42 |
* Optional human-readable message describing the current task state. |
| 43 |
* This can provide context for any status, including: |
| 44 |
* - Reasons for "cancelled" status |
| 45 |
* - Summaries for "completed" status |
| 46 |
* - Diagnostic information for "failed" status (e.g., error details, what went wrong) |
| 47 |
* |
| 48 |
* @since 2025-11-25 |
| 49 |
* |
| 50 |
* @var string|null |
| 51 |
*/ |
| 52 |
protected ?string $statusMessage; |
| 53 |
|
| 54 |
/** |
| 55 |
* ISO 8601 timestamp when the task was created. |
| 56 |
* |
| 57 |
* @since 2025-11-25 |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
protected string $createdAt; |
| 62 |
|
| 63 |
/** |
| 64 |
* ISO 8601 timestamp when the task was last updated. |
| 65 |
* |
| 66 |
* @since 2025-11-25 |
| 67 |
* |
| 68 |
* @var string |
| 69 |
*/ |
| 70 |
protected string $lastUpdatedAt; |
| 71 |
|
| 72 |
/** |
| 73 |
* Actual retention duration from creation in milliseconds, null for unlimited. |
| 74 |
* |
| 75 |
* @since 2025-11-25 |
| 76 |
* |
| 77 |
* @var int|null |
| 78 |
*/ |
| 79 |
protected ?int $ttl; |
| 80 |
|
| 81 |
/** |
| 82 |
* Suggested polling interval in milliseconds. |
| 83 |
* |
| 84 |
* @since 2025-11-25 |
| 85 |
* |
| 86 |
* @var int|null |
| 87 |
*/ |
| 88 |
protected ?int $pollInterval; |
| 89 |
|
| 90 |
/** |
| 91 |
* @param string $taskId @since 2025-11-25 |
| 92 |
* @param 'working'|'input_required'|'completed'|'failed'|'cancelled' $status @since 2025-11-25 |
| 93 |
* @param string $createdAt @since 2025-11-25 |
| 94 |
* @param string $lastUpdatedAt @since 2025-11-25 |
| 95 |
* @param int|null $ttl @since 2025-11-25 |
| 96 |
* @param string|null $statusMessage @since 2025-11-25 |
| 97 |
* @param int|null $pollInterval @since 2025-11-25 |
| 98 |
*/ |
| 99 |
public function __construct( |
| 100 |
string $taskId, |
| 101 |
string $status, |
| 102 |
string $createdAt, |
| 103 |
string $lastUpdatedAt, |
| 104 |
?int $ttl, |
| 105 |
?string $statusMessage = null, |
| 106 |
?int $pollInterval = null |
| 107 |
) { |
| 108 |
$this->taskId = $taskId; |
| 109 |
$this->status = $status; |
| 110 |
$this->createdAt = $createdAt; |
| 111 |
$this->lastUpdatedAt = $lastUpdatedAt; |
| 112 |
$this->ttl = $ttl; |
| 113 |
$this->statusMessage = $statusMessage; |
| 114 |
$this->pollInterval = $pollInterval; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Creates an instance from an array. |
| 119 |
* |
| 120 |
* @param array{ |
| 121 |
* taskId: string, |
| 122 |
* status: 'working'|'input_required'|'completed'|'failed'|'cancelled', |
| 123 |
* statusMessage?: string|null, |
| 124 |
* createdAt: string, |
| 125 |
* lastUpdatedAt: string, |
| 126 |
* ttl: int|null, |
| 127 |
* pollInterval?: int|null |
| 128 |
* } $data |
| 129 |
* @phpstan-param array<string, mixed> $data |
| 130 |
* @return self |
| 131 |
*/ |
| 132 |
public static function fromArray(array $data): self |
| 133 |
{ |
| 134 |
self::assertRequired($data, ['taskId', 'status', 'createdAt', 'lastUpdatedAt', 'ttl']); |
| 135 |
|
| 136 |
/** @var 'working'|'input_required'|'completed'|'failed'|'cancelled' $status */ |
| 137 |
$status = self::asString($data['status']); |
| 138 |
|
| 139 |
return new self( |
| 140 |
self::asString($data['taskId']), |
| 141 |
$status, |
| 142 |
self::asString($data['createdAt']), |
| 143 |
self::asString($data['lastUpdatedAt']), |
| 144 |
self::asInt($data['ttl']), |
| 145 |
self::asStringOrNull($data['statusMessage'] ?? null), |
| 146 |
self::asIntOrNull($data['pollInterval'] ?? null) |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Converts the instance to an array. |
| 152 |
* |
| 153 |
* @return array<string, mixed> |
| 154 |
*/ |
| 155 |
public function toArray(): array |
| 156 |
{ |
| 157 |
$result = []; |
| 158 |
|
| 159 |
$result['taskId'] = $this->taskId; |
| 160 |
$result['status'] = $this->status; |
| 161 |
if ($this->statusMessage !== null) { |
| 162 |
$result['statusMessage'] = $this->statusMessage; |
| 163 |
} |
| 164 |
$result['createdAt'] = $this->createdAt; |
| 165 |
$result['lastUpdatedAt'] = $this->lastUpdatedAt; |
| 166 |
if ($this->ttl !== null) { |
| 167 |
$result['ttl'] = $this->ttl; |
| 168 |
} |
| 169 |
if ($this->pollInterval !== null) { |
| 170 |
$result['pollInterval'] = $this->pollInterval; |
| 171 |
} |
| 172 |
|
| 173 |
return $result; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public function getTaskId(): string |
| 180 |
{ |
| 181 |
return $this->taskId; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @return 'working'|'input_required'|'completed'|'failed'|'cancelled' |
| 186 |
*/ |
| 187 |
public function getStatus(): string |
| 188 |
{ |
| 189 |
return $this->status; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @return string|null |
| 194 |
*/ |
| 195 |
public function getStatusMessage(): ?string |
| 196 |
{ |
| 197 |
return $this->statusMessage; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* @return string |
| 202 |
*/ |
| 203 |
public function getCreatedAt(): string |
| 204 |
{ |
| 205 |
return $this->createdAt; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* @return string |
| 210 |
*/ |
| 211 |
public function getLastUpdatedAt(): string |
| 212 |
{ |
| 213 |
return $this->lastUpdatedAt; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @return int|null |
| 218 |
*/ |
| 219 |
public function getTtl(): ?int |
| 220 |
{ |
| 221 |
return $this->ttl; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* @return int|null |
| 226 |
*/ |
| 227 |
public function getPollInterval(): ?int |
| 228 |
{ |
| 229 |
return $this->pollInterval; |
| 230 |
} |
| 231 |
} |
| 232 |
|