1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Model\ProfileSettings; |
6
|
|
|
|
7
|
|
|
use Kerox\Messenger\Helper\ValidatorTrait; |
8
|
|
|
use Kerox\Messenger\Model\Common\Button\AbstractButton; |
9
|
|
|
|
10
|
|
|
class PersistentMenu implements ProfileSettingsInterface, \JsonSerializable |
11
|
|
|
{ |
12
|
|
|
use ValidatorTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $locale; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $composerInputDisabled = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Kerox\Messenger\Model\Common\Button\AbstractButton[] |
26
|
|
|
*/ |
27
|
|
|
protected $buttons = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* PersistentMenu constructor. |
31
|
|
|
* |
32
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
33
|
|
|
*/ |
34
|
3 |
|
public function __construct(string $locale = self::DEFAULT_LOCALE) |
35
|
|
|
{ |
36
|
3 |
|
if ($locale !== self::DEFAULT_LOCALE) { |
37
|
1 |
|
$this->isValidLocale($locale); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$this->locale = $locale; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
45
|
|
|
* |
46
|
|
|
* @return \Kerox\Messenger\Model\ProfileSettings\PersistentMenu |
47
|
|
|
*/ |
48
|
3 |
|
public static function create(string $locale = self::DEFAULT_LOCALE): self |
49
|
|
|
{ |
50
|
3 |
|
return new self($locale); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \Kerox\Messenger\Model\ProfileSettings\PersistentMenu |
55
|
|
|
*/ |
56
|
3 |
|
public function setComposerInputDisabled(bool $composerInputDisabled): self |
57
|
|
|
{ |
58
|
3 |
|
$this->composerInputDisabled = $composerInputDisabled; |
59
|
|
|
|
60
|
3 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons |
65
|
|
|
* |
66
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
67
|
|
|
* |
68
|
|
|
* @return \Kerox\Messenger\Model\ProfileSettings\PersistentMenu |
69
|
|
|
*/ |
70
|
3 |
View Code Duplication |
public function addButtons(array $buttons): self |
|
|
|
|
71
|
|
|
{ |
72
|
3 |
|
$this->isValidArray($buttons, 5); |
73
|
3 |
|
$this->isValidButtons($buttons, $this->getAllowedButtonsType()); |
74
|
|
|
|
75
|
1 |
|
$this->buttons = $buttons; |
76
|
|
|
|
77
|
1 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
protected function getAllowedButtonsType(): array |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
3 |
|
AbstractButton::TYPE_WEB_URL, |
84
|
|
|
AbstractButton::TYPE_POSTBACK, |
85
|
|
|
AbstractButton::TYPE_NESTED, |
|
|
|
|
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function toArray(): array |
90
|
|
|
{ |
91
|
|
|
$array = [ |
92
|
1 |
|
'locale' => $this->locale, |
93
|
1 |
|
'composer_input_disabled' => $this->composerInputDisabled, |
94
|
1 |
|
'call_to_actions' => $this->buttons, |
95
|
|
|
]; |
96
|
|
|
|
97
|
1 |
|
return array_filter($array); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function jsonSerialize(): array |
101
|
|
|
{ |
102
|
1 |
|
return $this->toArray(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.