ok = $ok; $this->value = $value; $this->error_code = $error_code; $this->error_message = $error_message; } /** * Successful result. * * @template V * @param V $value Success payload. * @return self * * @since 0.1.0 */ public static function ok( mixed $value = null ): self { return new self( true, $value, null, null ); } /** * Failed result. * * @param string $error_code Stable code. * @param string $error_message Developer message. * @return self * * @since 0.1.0 */ public static function err( string $error_code, string $error_message ): self { return new self( false, null, $error_code, $error_message ); } /** * Whether the result is success. * * @since 0.1.0 */ public function is_ok(): bool { return $this->ok; } /** * Is err. * * @since 0.1.0 */ public function is_err(): bool { return ! $this->ok; } /** * Success payload. * * @return mixed * * @throws LogicException When the result is an error. * * @since 0.1.0 */ public function value(): mixed { if ( ! $this->ok ) { throw new LogicException( 'Cannot read value from an error Result.' ); } return $this->value; } /** * Failure code. * * @throws LogicException When the result is ok. * * @since 0.1.0 */ public function error_code(): string { if ( $this->ok ) { throw new LogicException( 'Cannot read error_code from an ok Result.' ); } return (string) $this->error_code; } /** * Failure message. * * @throws LogicException When the result is ok. * * @since 0.1.0 */ public function error_message(): string { if ( $this->ok ) { throw new LogicException( 'Cannot read error_message from an ok Result.' ); } return (string) $this->error_message; } }