Skip to content

Commit

Permalink
Updates HasOptions to account for initialOptions (#12)
Browse files Browse the repository at this point in the history
Udpates all HasOptions-using elements. Also adds setExtra to base
Element object to allow for patching on new Slack features in a
non-conflicting way prior to the library being updated.
  • Loading branch information
jeremeamia authored Sep 30, 2020
1 parent 6828ae7 commit 39b4898
Show file tree
Hide file tree
Showing 26 changed files with 551 additions and 180 deletions.
31 changes: 30 additions & 1 deletion src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ abstract class Element implements JsonSerializable
/** @var Element|null */
protected $parent;

/** @var array */
protected $extra;

/**
* @return static
*/
Expand Down Expand Up @@ -46,6 +49,26 @@ public function getType(): string
return Type::mapClass(static::class);
}

/**
* Allows setting arbitrary extra fields on an element.
*
* Ideally, this is only used to allow setting new Slack features that are not yet implemented in this library.
*
* @param string $key
* @param mixed $value
* @return static
*/
public function setExtra(string $key, $value): Element
{
if (!is_scalar($value) && !($value instanceof Element)) {
throw new Exception('Invalid extra field set in %d.', [static::class]);
}

$this->extra[$key] = $value;

return $this;
}

/**
* @throws Exception if the block kit item is invalid (e.g., missing data).
*/
Expand All @@ -59,7 +82,13 @@ public function toArray(): array
$this->validate();
$type = $this->getType();

return !in_array($type, Type::HIDDEN_TYPES, true) ? compact('type') : [];
$data = !in_array($type, Type::HIDDEN_TYPES, true) ? compact('type') : [];

foreach ($this->extra ?? [] as $key => $value) {
$data[$key] = $value instanceof Element ? $value->toArray() : $value;
}

return $data;
}

/**
Expand Down
33 changes: 10 additions & 23 deletions src/Inputs/Checkboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,37 @@

namespace Jeremeamia\Slack\BlockKit\Inputs;

use Jeremeamia\Slack\BlockKit\Exception;
use Jeremeamia\Slack\BlockKit\Partials\HasOptions;
use Jeremeamia\Slack\BlockKit\Partials\Option;
use Jeremeamia\Slack\BlockKit\Partials\{HasOptions, OptionsConfig};

class Checkboxes extends InputElement
{
use HasConfirm;
use HasOptions;

private const MIN_OPTIONS = 1;
private const MAX_OPTIONS = 10;
protected function getOptionsConfig(): OptionsConfig
{
return OptionsConfig::new()
->setMinOptions(1)
->setMaxOptions(10)
->setMaxInitialOptions(10);
}

public function validate(): void
{

$this->validateOptions();
$this->validateInitialOptions();

if (!empty($this->confirm)) {
$this->confirm->validate();
}

if (count($this->options) > self::MAX_OPTIONS) {
throw new Exception('Option Size cannot exceed %d', [self::MAX_OPTIONS]);
}

if (count($this->options) < self::MIN_OPTIONS) {
throw new Exception('Option Size must be at least %d', [self::MIN_OPTIONS]);
}
}

/**
* @return array
*/
public function toArray(): array
{
$data = parent::toArray();

foreach ($this->options as $option) {
if ($option->isInitial()) {
$data['initial_options'][] = $option->toArray();
}
}

$data += $this->getOptionsAsArray();
$data = parent::toArray() + $this->getOptionsAsArray() + $this->getInitialOptionsAsArray();

if (!empty($this->confirm)) {
$data['confirm'] = $this->confirm->toArray();
Expand Down
34 changes: 20 additions & 14 deletions src/Inputs/OverflowMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,48 @@

namespace Jeremeamia\Slack\BlockKit\Inputs;

use Jeremeamia\Slack\BlockKit\Exception;
use Jeremeamia\Slack\BlockKit\Partials\HasOptions;
use Jeremeamia\Slack\BlockKit\Partials\{HasOptions, Option, OptionsConfig};

class OverflowMenu extends InputElement
{
use HasConfirm;
use HasOptions;

private const MIN_OPTIONS = 2;
private const MAX_OPTIONS = 5;
protected function getOptionsConfig(): OptionsConfig
{
return OptionsConfig::new()
->setMinOptions(2)
->setMaxOptions(5)
->setMaxInitialOptions(0);
}

public function validate(): void
/**
* @param string $text
* @param string $value
* @param string $url
* @return static
*/
public function urlOption(string $text, string $value, string $url): self
{
return $this->addOption(Option::new($text, $value)->url($url));
}

public function validate(): void
{
$this->validateOptions();

if (!empty($this->confirm)) {
$this->confirm->validate();
}

if (count($this->options) > self::MAX_OPTIONS) {
throw new Exception('Option Size cannot exceed %d', [self::MAX_OPTIONS]);
}

if (count($this->options) < self::MIN_OPTIONS) {
throw new Exception('Option Size must be at least %d', [self::MIN_OPTIONS]);
}
}

/**
* @return array
*/
public function toArray(): array
{

$data = parent::toArray();

$data += $this->getOptionsAsArray();

if (!empty($this->confirm)) {
Expand Down
47 changes: 10 additions & 37 deletions src/Inputs/RadioButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,37 @@

namespace Jeremeamia\Slack\BlockKit\Inputs;

use Jeremeamia\Slack\BlockKit\Exception;
use Jeremeamia\Slack\BlockKit\Partials\HasOptions;
use Jeremeamia\Slack\BlockKit\Partials\Option;
use Jeremeamia\Slack\BlockKit\Partials\{HasOptions, OptionsConfig};

class RadioButtons extends InputElement
{
use HasConfirm;
use HasOptions;

private const MIN_OPTIONS = 1;
private const MAX_OPTIONS = 10;
protected function getOptionsConfig(): OptionsConfig
{
return OptionsConfig::new()
->setMinOptions(1)
->setMaxOptions(10)
->setMaxInitialOptions(1);
}

public function validate(): void
{

$this->validateOptions();

$hasInitial = false;

foreach ($this->options as $option) {
$option->validate();

if ($option->isInitial()) {
if ($hasInitial) {
throw new Exception('Only one initial Option is allowed for Radio Buttons');
}
$hasInitial = true;
}
}
$this->validateInitialOptions();

if (!empty($this->confirm)) {
$this->confirm->validate();
}

if (count($this->options) > self::MAX_OPTIONS) {
throw new Exception('Option Size cannot exceed %d', [self::MAX_OPTIONS]);
}

if (count($this->options) < self::MIN_OPTIONS) {
throw new Exception('Option Size must be at least %d', [self::MIN_OPTIONS]);
}
}

/**
* @return array
*/
public function toArray(): array
{
$data = parent::toArray();

foreach ($this->options as $option) {
if ($option->isInitial()) {
$data['initial_option'] = $option->toArray();
break;
}
}

$data += $this->getOptionsAsArray();
$data = parent::toArray() + $this->getOptionsAsArray() + $this->getInitialOptionsAsArray();

if (!empty($this->confirm)) {
$data['confirm'] = $this->confirm->toArray();
Expand Down
18 changes: 18 additions & 0 deletions src/Inputs/SelectMenus/ChannelSelectMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class ChannelSelectMenu extends SelectMenu
/** @var string */
private $initialChannel;

/** @var bool */
private $responseUrlEnabled;

/**
* @param string $initialChannel
* @return static
Expand All @@ -20,6 +23,17 @@ public function initialChannel(string $initialChannel): self
return $this;
}

/**
* @param bool $enabled
* @return static
*/
public function responseUrlEnabled(bool $enabled): self
{
$this->responseUrlEnabled = $enabled;

return $this;
}

/**
* @return array
*/
Expand All @@ -31,6 +45,10 @@ public function toArray(): array
$data['initial_channel'] = $this->initialChannel;
}

if (!empty($this->responseUrlEnabled)) {
$data['response_url_enabled'] = $this->responseUrlEnabled;
}

return $data;
}
}
38 changes: 38 additions & 0 deletions src/Inputs/SelectMenus/ConversationSelectMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class ConversationSelectMenu extends SelectMenu
/** @var string */
private $initialConversation;

/** @var bool */
private $responseUrlEnabled;

/** @var bool */
private $defaultToCurrentConversation;

/**
* @param string $initialConversation
* @return static
Expand All @@ -20,6 +26,30 @@ public function initialConversation(string $initialConversation): self
return $this;
}

/**
* @param bool $enabled
* @return static
*/
public function responseUrlEnabled(bool $enabled): self
{
$this->responseUrlEnabled = $enabled;

return $this;
}

/**
* @param bool $enabled
* @return static
*/
public function defaultToCurrentConversation(bool $enabled): self
{
$this->defaultToCurrentConversation = $enabled;

return $this;
}

// @TODO: filter - https://api.slack.com/reference/block-kit/block-elements#conversation_select

/**
* @return array
*/
Expand All @@ -31,6 +61,14 @@ public function toArray(): array
$data['initial_conversation'] = $this->initialConversation;
}

if (!empty($this->responseUrlEnabled)) {
$data['response_url_enabled'] = $this->responseUrlEnabled;
}

if (!empty($this->defaultToCurrentConversation)) {
$data['default_to_current_conversation'] = $this->defaultToCurrentConversation;
}

return $data;
}
}
2 changes: 1 addition & 1 deletion src/Inputs/SelectMenus/ExternalSelectMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ExternalSelectMenu extends SelectMenu
*/
public function initialOption(string $name, string $value): self
{
$this->initialOption = new Option($name, $value);
$this->initialOption = Option::new($name, $value);
$this->initialOption->setParent($this);

return $this;
Expand Down
20 changes: 20 additions & 0 deletions src/Inputs/SelectMenus/MultiConversationSelectMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class MultiConversationSelectMenu extends MultiSelectMenu
/** @var string[] */
private $initialConversations;

/** @var bool */
private $defaultToCurrentConversation;

/**
* @param string[] $initialConversations
* @return static
Expand All @@ -20,6 +23,19 @@ public function initialConversations(array $initialConversations): self
return $this;
}

/**
* @param bool $enabled
* @return static
*/
public function defaultToCurrentConversation(bool $enabled): self
{
$this->defaultToCurrentConversation = $enabled;

return $this;
}

// @TODO: filter - https://api.slack.com/reference/block-kit/block-elements#conversation_select

/**
* @return array
*/
Expand All @@ -31,6 +47,10 @@ public function toArray(): array
$data['initial_conversations'] = $this->initialConversations;
}

if (!empty($this->defaultToCurrentConversation)) {
$data['default_to_current_conversation'] = $this->defaultToCurrentConversation;
}

return $data;
}
}
Loading

0 comments on commit 39b4898

Please sign in to comment.