Skip to content

Commit

Permalink
reuse Payload for fillProperties() (decrease complexity. resolves #…
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 committed Feb 3, 2021
1 parent b26b2a7 commit 53a102b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 63 deletions.
31 changes: 14 additions & 17 deletions src/ActionConfirmation.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Maknz\Slack;

class ActionConfirmation
class ActionConfirmation extends Payload
{
/**
* The required title for the pop up window.
Expand Down Expand Up @@ -31,29 +31,26 @@ class ActionConfirmation
*/
protected $dismissText;

/**
* Internal attribute to property map.
*
* @var array
*/
protected static $availableAttributes = [
'title' => 'title',
'text' => 'text',
'ok_text' => 'ok_text',
'dismiss_text' => 'dismiss_text',
];

/**
* Instantiate a new ActionConfirmation.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes)
{
if (isset($attributes['title'])) {
$this->setTitle($attributes['title']);
}

if (isset($attributes['text'])) {
$this->setText($attributes['text']);
}

if (isset($attributes['ok_text'])) {
$this->setOkText($attributes['ok_text']);
}

if (isset($attributes['dismiss_text'])) {
$this->setDismissText($attributes['dismiss_text']);
}
parent::__construct($attributes);
}

/**
Expand Down
49 changes: 19 additions & 30 deletions src/AttachmentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use InvalidArgumentException;

class AttachmentAction
class AttachmentAction extends Payload
{
const TYPE_BUTTON = 'button';

Expand Down Expand Up @@ -60,42 +60,31 @@ class AttachmentAction
*/
protected $confirm;

/**
* Internal attribute to property map.
*
* @var array
*/
protected static $availableAttributes = [
'name' => 'name',
'text' => 'text',
'style' => 'style',
'type' => 'type',
'url' => 'url',
'value' => 'value',
'confirm' => 'confirm',
];

/**
* Instantiate a new AttachmentAction.
*
* @param array $attributes
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function __construct(array $attributes)
{
if (isset($attributes['name'])) {
$this->setName($attributes['name']);
}

if (isset($attributes['text'])) {
$this->setText($attributes['text']);
}

if (isset($attributes['style'])) {
$this->setStyle($attributes['style']);
}

if (isset($attributes['type'])) {
$this->setType($attributes['type']);
}

if (isset($attributes['url'])) {
$this->setUrl($attributes['url']);
}

if (isset($attributes['value'])) {
$this->setValue($attributes['value']);
}

if (isset($attributes['confirm'])) {
$this->setConfirm($attributes['confirm']);
}
parent::__construct($attributes);
}

/**
Expand Down Expand Up @@ -225,7 +214,7 @@ public function getConfirm()
*
* @return AttachmentAction
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setConfirm($confirm)
{
Expand Down
26 changes: 13 additions & 13 deletions src/AttachmentField.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Maknz\Slack;

class AttachmentField implements Field
class AttachmentField extends Payload implements Field
{
/**
* The required title field of the field.
Expand All @@ -25,25 +25,25 @@ class AttachmentField implements Field
*/
protected $short = false;

/**
* Internal attribute to property map.
*
* @var array
*/
protected static $availableAttributes = [
'title' => 'title',
'value' => 'value',
'short' => 'short',
];

/**
* Instantiate a new AttachmentField.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes)
{
if (isset($attributes['title'])) {
$this->setTitle($attributes['title']);
}

if (isset($attributes['value'])) {
$this->setValue($attributes['value']);
}

if (isset($attributes['short'])) {
$this->setShort($attributes['short']);
}
parent::__construct($attributes);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ abstract class Payload
* @param array $attributes
*/
public function __construct(array $attributes)
{
$this->fillProperties($attributes);
}

/**
* @param array $attributes
*
* @return $this
*/
protected function fillProperties(array $attributes): self
{
foreach ($attributes as $attribute => $value) {
$setter = self::getAttributeSetter($attribute);
if ($setter !== null) {
$this->$setter($value);
}
}

return $this;
}

/**
Expand All @@ -32,7 +44,7 @@ public function __construct(array $attributes)
*
* @return null|string
*/
protected static function getAttributeSetter(string $attribute)
private static function getAttributeSetter(string $attribute)
{
$property = self::getAttributeProperty($attribute);

Expand All @@ -46,7 +58,7 @@ protected static function getAttributeSetter(string $attribute)
*
* @return string|null
*/
protected static function getAttributeProperty(string $attribute)
private static function getAttributeProperty(string $attribute)
{
return static::$availableAttributes[$attribute] ?? null;
}
Expand All @@ -58,7 +70,7 @@ protected static function getAttributeProperty(string $attribute)
*
* @return string
*/
protected static function propertyToSetter(string $property): string
private static function propertyToSetter(string $property): string
{
$property = str_replace('_', ' ', $property);
$property = ucwords($property);
Expand Down

0 comments on commit 53a102b

Please sign in to comment.