Skip to content

Commit 5be2120

Browse files
committed
Fixes to Context and InputStateValidator
- Added missing `Context::error()` method. :-( - Added `InputStateValidator::new()` method. - Updated `InputStateValidator::rule()` so that callback can accept `string` or `string[]` (e.g., for multi-selects) values.
1 parent 444a7a3 commit 5be2120

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/Context.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SlackPhp\BlockKit\Surfaces\{AppHome, Message};
1515
use SlackPhp\Framework\Contexts\{
1616
Blocks,
17+
Error,
1718
HasData,
1819
Home,
1920
Modals,
@@ -370,6 +371,22 @@ public function say($message, ?string $channel = null, ?string $threadTs = null)
370371
}
371372
}
372373

374+
/**
375+
* @param OptionList|array|null $options
376+
*/
377+
public function options($options): void
378+
{
379+
if (!$this->payload->isType(PayloadType::blockSuggestion())) {
380+
throw new Exception('Can only to use `options()` for block_suggestion requests');
381+
}
382+
383+
if (is_array($options)) {
384+
$options = OptionList::new()->options($options);
385+
}
386+
387+
$this->ack($options);
388+
}
389+
373390
/**
374391
* @param AppHome|array|string|callable(): AppHome $appHome
375392
* @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
392409
}
393410
}
394411

412+
/**
413+
* Perform an operation on the App Home.
414+
*
415+
* @return Home
416+
*/
395417
public function appHome(): Home
396418
{
397419
return new Home($this);
398420
}
399421

422+
/**
423+
* Perform an operation with modals.
424+
*
425+
* @return Modals
426+
*/
400427
public function modals(): Modals
401428
{
402429
return new Modals($this);
403430
}
404431

432+
/**
433+
* Ack with a response to the current modal.
434+
*
435+
* @return View
436+
*/
405437
public function view(): View
406438
{
407439
if (!$this->payload->isType(PayloadType::viewSubmission())) {
@@ -412,19 +444,14 @@ public function view(): View
412444
}
413445

414446
/**
415-
* @param OptionList|array|null $options
447+
* Log and display an error to the user.
448+
*
449+
* @param Throwable $exception
450+
* @return Error
416451
*/
417-
public function options($options): void
452+
public function error(Throwable $exception): Error
418453
{
419-
if (!$this->payload->isType(PayloadType::blockSuggestion())) {
420-
throw new Exception('Can only to use `options()` for block_suggestion requests');
421-
}
422-
423-
if (is_array($options)) {
424-
$options = OptionList::new()->options($options);
425-
}
426-
427-
$this->ack($options);
454+
return new Error($this, $exception);
428455
}
429456

430457
public function toArray(): array

src/Contexts/InputStateValidator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ class InputStateValidator
1111
{
1212
private array $rules;
1313

14+
public static function new(): self
15+
{
16+
return new self();
17+
}
18+
1419
/**
1520
* Adds a validation rule for an input state field.
1621
*
1722
* @param string $key The block ID for the input in the state to validate.
1823
* @param bool $required Whether the input is required.
19-
* @param null|callable(string): ?string $ruleFn Function that validates the input from the state and returns either
20-
* an error message string or null (for no error).
24+
* @param null|callable(string|string[]): ?string $ruleFn Function to validate the input from the state and returns
25+
* either an error message string or null (for no error).
2126
* @return $this
2227
*/
2328
public function rule(string $key, bool $required, ?callable $ruleFn = null): self
2429
{
25-
$this->rules[$key] = function (?string $value) use ($required, $ruleFn): ?string {
30+
$this->rules[$key] = function ($value) use ($required, $ruleFn): ?string {
2631
if ($required && !isset($value)) {
2732
return 'This value is required.';
2833
}

0 commit comments

Comments
 (0)