*/ private $usage = array(); /** * Provider's job ID when pending. * * @var string|null */ private $remote_id; /** * Error when failed. * * @var ProviderError|null */ private $error; /** * Use the named constructors. * * @param string $state One of the constants above. */ private function __construct( string $state ) { $this->state = $state; } /** * The image is ready. * * @param string $image Image bytes. * @param string $model Model name. * @param string|null $cost USD as a decimal string, or null when unknown. * @param array $usage Raw usage block. */ public static function done( string $image, string $model, ?string $cost, array $usage = array() ): self { $submission = new self( self::DONE ); $submission->image = $image; $submission->model = $model; $submission->cost = $cost; $submission->usage = $usage; return $submission; } /** * The provider is still working; poll with this ID. * * @param string $remote_id Provider's job ID. */ public static function pending( string $remote_id ): self { $submission = new self( self::PENDING ); $submission->remote_id = $remote_id; return $submission; } /** * The generation failed. * * @param ProviderError $error Why. */ public static function failed( ProviderError $error ): self { $submission = new self( self::FAILED ); $submission->error = $error; return $submission; } /** * Whether the image is ready. */ public function is_done(): bool { return self::DONE === $this->state; } /** * Whether the provider is still working. */ public function is_pending(): bool { return self::PENDING === $this->state; } /** * Whether the generation failed. */ public function is_failed(): bool { return self::FAILED === $this->state; } /** * Image bytes, when done. */ public function image(): ?string { return $this->image; } /** * Model name, when done. */ public function model(): string { return $this->model; } /** * Cost in USD, when done and known. */ public function cost(): ?string { return $this->cost; } /** * Raw usage block, when done. * * @return array */ public function usage(): array { return $this->usage; } /** * Provider's job ID, when pending. */ public function remote_id(): ?string { return $this->remote_id; } /** * The error, when failed; UNKNOWN otherwise so callers never get null. */ public function error(): ProviderError { return $this->error ?? new ProviderError( ProviderError::UNKNOWN ); } }