diff --git a/src/Context.php b/src/Context.php index 7c7e6b9..f061cb9 100644 --- a/src/Context.php +++ b/src/Context.php @@ -14,6 +14,7 @@ use SlackPhp\BlockKit\Surfaces\{AppHome, Message}; use SlackPhp\Framework\Contexts\{ Blocks, + Error, HasData, Home, Modals, @@ -370,6 +371,22 @@ public function say($message, ?string $channel = null, ?string $threadTs = null) } } + /** + * @param OptionList|array|null $options + */ + public function options($options): void + { + if (!$this->payload->isType(PayloadType::blockSuggestion())) { + throw new Exception('Can only to use `options()` for block_suggestion requests'); + } + + if (is_array($options)) { + $options = OptionList::new()->options($options); + } + + $this->ack($options); + } + /** * @param AppHome|array|string|callable(): AppHome $appHome * @param string|null $userId If null, the value from the current payload will be used. @@ -392,16 +409,31 @@ public function home($appHome, ?string $userId = null, bool $useHashIfAvailable } } + /** + * Perform an operation on the App Home. + * + * @return Home + */ public function appHome(): Home { return new Home($this); } + /** + * Perform an operation with modals. + * + * @return Modals + */ public function modals(): Modals { return new Modals($this); } + /** + * Ack with a response to the current modal. + * + * @return View + */ public function view(): View { if (!$this->payload->isType(PayloadType::viewSubmission())) { @@ -412,19 +444,14 @@ public function view(): View } /** - * @param OptionList|array|null $options + * Log and display an error to the user. + * + * @param Throwable $exception + * @return Error */ - public function options($options): void + public function error(Throwable $exception): Error { - if (!$this->payload->isType(PayloadType::blockSuggestion())) { - throw new Exception('Can only to use `options()` for block_suggestion requests'); - } - - if (is_array($options)) { - $options = OptionList::new()->options($options); - } - - $this->ack($options); + return new Error($this, $exception); } public function toArray(): array diff --git a/src/Contexts/InputStateValidator.php b/src/Contexts/InputStateValidator.php index 736e867..856c9be 100644 --- a/src/Contexts/InputStateValidator.php +++ b/src/Contexts/InputStateValidator.php @@ -11,18 +11,23 @@ class InputStateValidator { private array $rules; + public static function new(): self + { + return new self(); + } + /** * Adds a validation rule for an input state field. * * @param string $key The block ID for the input in the state to validate. * @param bool $required Whether the input is required. - * @param null|callable(string): ?string $ruleFn Function that validates the input from the state and returns either - * an error message string or null (for no error). + * @param null|callable(string|string[]): ?string $ruleFn Function to validate the input from the state and returns + * either an error message string or null (for no error). * @return $this */ public function rule(string $key, bool $required, ?callable $ruleFn = null): self { - $this->rules[$key] = function (?string $value) use ($required, $ruleFn): ?string { + $this->rules[$key] = function ($value) use ($required, $ruleFn): ?string { if ($required && !isset($value)) { return 'This value is required.'; }