Skip to content

Commit 53a102b

Browse files
committed
reuse Payload for fillProperties() (decrease complexity. resolves #13
1 parent b26b2a7 commit 53a102b

4 files changed

Lines changed: 61 additions & 63 deletions

File tree

src/ActionConfirmation.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Maknz\Slack;
33

4-
class ActionConfirmation
4+
class ActionConfirmation extends Payload
55
{
66
/**
77
* The required title for the pop up window.
@@ -31,29 +31,26 @@ class ActionConfirmation
3131
*/
3232
protected $dismissText;
3333

34+
/**
35+
* Internal attribute to property map.
36+
*
37+
* @var array
38+
*/
39+
protected static $availableAttributes = [
40+
'title' => 'title',
41+
'text' => 'text',
42+
'ok_text' => 'ok_text',
43+
'dismiss_text' => 'dismiss_text',
44+
];
45+
3446
/**
3547
* Instantiate a new ActionConfirmation.
3648
*
3749
* @param array $attributes
38-
* @return void
3950
*/
4051
public function __construct(array $attributes)
4152
{
42-
if (isset($attributes['title'])) {
43-
$this->setTitle($attributes['title']);
44-
}
45-
46-
if (isset($attributes['text'])) {
47-
$this->setText($attributes['text']);
48-
}
49-
50-
if (isset($attributes['ok_text'])) {
51-
$this->setOkText($attributes['ok_text']);
52-
}
53-
54-
if (isset($attributes['dismiss_text'])) {
55-
$this->setDismissText($attributes['dismiss_text']);
56-
}
53+
parent::__construct($attributes);
5754
}
5855

5956
/**

src/AttachmentAction.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use InvalidArgumentException;
55

6-
class AttachmentAction
6+
class AttachmentAction extends Payload
77
{
88
const TYPE_BUTTON = 'button';
99

@@ -60,42 +60,31 @@ class AttachmentAction
6060
*/
6161
protected $confirm;
6262

63+
/**
64+
* Internal attribute to property map.
65+
*
66+
* @var array
67+
*/
68+
protected static $availableAttributes = [
69+
'name' => 'name',
70+
'text' => 'text',
71+
'style' => 'style',
72+
'type' => 'type',
73+
'url' => 'url',
74+
'value' => 'value',
75+
'confirm' => 'confirm',
76+
];
77+
6378
/**
6479
* Instantiate a new AttachmentAction.
6580
*
6681
* @param array $attributes
6782
*
68-
* @throws \InvalidArgumentException
83+
* @throws InvalidArgumentException
6984
*/
7085
public function __construct(array $attributes)
7186
{
72-
if (isset($attributes['name'])) {
73-
$this->setName($attributes['name']);
74-
}
75-
76-
if (isset($attributes['text'])) {
77-
$this->setText($attributes['text']);
78-
}
79-
80-
if (isset($attributes['style'])) {
81-
$this->setStyle($attributes['style']);
82-
}
83-
84-
if (isset($attributes['type'])) {
85-
$this->setType($attributes['type']);
86-
}
87-
88-
if (isset($attributes['url'])) {
89-
$this->setUrl($attributes['url']);
90-
}
91-
92-
if (isset($attributes['value'])) {
93-
$this->setValue($attributes['value']);
94-
}
95-
96-
if (isset($attributes['confirm'])) {
97-
$this->setConfirm($attributes['confirm']);
98-
}
87+
parent::__construct($attributes);
9988
}
10089

10190
/**
@@ -225,7 +214,7 @@ public function getConfirm()
225214
*
226215
* @return AttachmentAction
227216
*
228-
* @throws \InvalidArgumentException
217+
* @throws InvalidArgumentException
229218
*/
230219
public function setConfirm($confirm)
231220
{

src/AttachmentField.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Maknz\Slack;
33

4-
class AttachmentField implements Field
4+
class AttachmentField extends Payload implements Field
55
{
66
/**
77
* The required title field of the field.
@@ -25,25 +25,25 @@ class AttachmentField implements Field
2525
*/
2626
protected $short = false;
2727

28+
/**
29+
* Internal attribute to property map.
30+
*
31+
* @var array
32+
*/
33+
protected static $availableAttributes = [
34+
'title' => 'title',
35+
'value' => 'value',
36+
'short' => 'short',
37+
];
38+
2839
/**
2940
* Instantiate a new AttachmentField.
3041
*
3142
* @param array $attributes
32-
* @return void
3343
*/
3444
public function __construct(array $attributes)
3545
{
36-
if (isset($attributes['title'])) {
37-
$this->setTitle($attributes['title']);
38-
}
39-
40-
if (isset($attributes['value'])) {
41-
$this->setValue($attributes['value']);
42-
}
43-
44-
if (isset($attributes['short'])) {
45-
$this->setShort($attributes['short']);
46-
}
46+
parent::__construct($attributes);
4747
}
4848

4949
/**

src/Payload.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@ abstract class Payload
1616
* @param array $attributes
1717
*/
1818
public function __construct(array $attributes)
19+
{
20+
$this->fillProperties($attributes);
21+
}
22+
23+
/**
24+
* @param array $attributes
25+
*
26+
* @return $this
27+
*/
28+
protected function fillProperties(array $attributes): self
1929
{
2030
foreach ($attributes as $attribute => $value) {
2131
$setter = self::getAttributeSetter($attribute);
2232
if ($setter !== null) {
2333
$this->$setter($value);
2434
}
2535
}
36+
37+
return $this;
2638
}
2739

2840
/**
@@ -32,7 +44,7 @@ public function __construct(array $attributes)
3244
*
3345
* @return null|string
3446
*/
35-
protected static function getAttributeSetter(string $attribute)
47+
private static function getAttributeSetter(string $attribute)
3648
{
3749
$property = self::getAttributeProperty($attribute);
3850

@@ -46,7 +58,7 @@ protected static function getAttributeSetter(string $attribute)
4658
*
4759
* @return string|null
4860
*/
49-
protected static function getAttributeProperty(string $attribute)
61+
private static function getAttributeProperty(string $attribute)
5062
{
5163
return static::$availableAttributes[$attribute] ?? null;
5264
}
@@ -58,7 +70,7 @@ protected static function getAttributeProperty(string $attribute)
5870
*
5971
* @return string
6072
*/
61-
protected static function propertyToSetter(string $property): string
73+
private static function propertyToSetter(string $property): string
6274
{
6375
$property = str_replace('_', ' ', $property);
6476
$property = ucwords($property);

0 commit comments

Comments
 (0)