Skip to content

Commit

Permalink
Fixes to Context and InputStateValidator
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
jeremeamia committed Aug 4, 2021
1 parent 444a7a3 commit 5be2120
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
49 changes: 38 additions & 11 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SlackPhp\BlockKit\Surfaces\{AppHome, Message};
use SlackPhp\Framework\Contexts\{
Blocks,
Error,
HasData,
Home,
Modals,
Expand Down Expand Up @@ -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.
Expand All @@ -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())) {
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/Contexts/InputStateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
}
Expand Down

0 comments on commit 5be2120

Please sign in to comment.