1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Navigation; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Html5; |
8
|
|
|
use AbterPhp\Framework\Html\Collection; |
9
|
|
|
use AbterPhp\Framework\Html\Component; |
10
|
|
|
use AbterPhp\Framework\Html\Helper\StringHelper; |
11
|
|
|
use AbterPhp\Framework\Html\ICollection; |
12
|
|
|
use AbterPhp\Framework\Html\IComponent; |
13
|
|
|
use AbterPhp\Framework\Html\INode; |
14
|
|
|
|
15
|
|
|
class Dropdown extends Component |
16
|
|
|
{ |
17
|
|
|
protected const DEFAULT_TAG = Html5::TAG_UL; |
18
|
|
|
|
19
|
|
|
public const WRAPPER_INTENT = 'dropdown-wrapper-intent'; |
20
|
|
|
|
21
|
|
|
/** @var IComponent|null */ |
22
|
|
|
protected $wrapper; |
23
|
|
|
|
24
|
|
|
/** @var ICollection */ |
25
|
|
|
protected $prefix; |
26
|
|
|
|
27
|
|
|
/** @var ICollection */ |
28
|
|
|
protected $postfix; |
29
|
|
|
|
30
|
|
|
/** @var Item[] */ |
31
|
|
|
protected $nodes; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $nodeClass = Item::class; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Component constructor. |
38
|
|
|
* |
39
|
|
|
* @param INode[]|INode|string|null $content |
40
|
|
|
* @param string[] $intents |
41
|
|
|
* @param array $attributes |
42
|
|
|
* @param string|null $tag |
43
|
|
|
*/ |
44
|
|
|
public function __construct($content = null, array $intents = [], array $attributes = [], ?string $tag = null) |
45
|
|
|
{ |
46
|
|
|
$this->wrapper = new Component(null, [static::WRAPPER_INTENT], [], Html5::TAG_DIV); |
47
|
|
|
|
48
|
|
|
$this->prefix = new Collection(); |
49
|
|
|
$this->postfix = new Collection(); |
50
|
|
|
|
51
|
|
|
parent::__construct($content, $intents, $attributes, $tag); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return IComponent|null |
56
|
|
|
*/ |
57
|
|
|
public function getWrapper(): ?IComponent |
58
|
|
|
{ |
59
|
|
|
return $this->wrapper; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param IComponent|null $wrapper |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setWrapper(?IComponent $wrapper): Dropdown |
68
|
|
|
{ |
69
|
|
|
$this->wrapper = $wrapper; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return ICollection |
76
|
|
|
*/ |
77
|
|
|
public function getPrefix(): ICollection |
78
|
|
|
{ |
79
|
|
|
return $this->prefix; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param ICollection $collection |
84
|
|
|
* |
85
|
|
|
* @return Dropdown |
86
|
|
|
*/ |
87
|
|
|
public function setPrefix(ICollection $collection): Dropdown |
88
|
|
|
{ |
89
|
|
|
$this->prefix = $collection; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return ICollection |
96
|
|
|
*/ |
97
|
|
|
public function getPostfix(): ICollection |
98
|
|
|
{ |
99
|
|
|
return $this->postfix; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param ICollection $collection |
104
|
|
|
* |
105
|
|
|
* @return Dropdown |
106
|
|
|
*/ |
107
|
|
|
public function setPostfix(ICollection $collection): Dropdown |
108
|
|
|
{ |
109
|
|
|
$this->postfix = $collection; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return INode[] |
116
|
|
|
*/ |
117
|
|
|
public function getExtendedNodes(): array |
118
|
|
|
{ |
119
|
|
|
$nodes = [$this->prefix, $this->postfix]; |
120
|
|
|
|
121
|
|
|
if ($this->wrapper) { |
122
|
|
|
$nodes[] = $this->wrapper; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return array_merge($nodes, $this->getNodes()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function __toString(): string |
132
|
|
|
{ |
133
|
|
|
$content = Collection::__toString(); |
134
|
|
|
$content = StringHelper::wrapInTag($content, $this->tag, $this->attributes); |
135
|
|
|
$content = (string)$this->prefix . $content . (string)$this->postfix; |
136
|
|
|
|
137
|
|
|
if ($this->wrapper) { |
138
|
|
|
$this->wrapper->setContent($content); |
139
|
|
|
|
140
|
|
|
return (string)$this->wrapper; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $content; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|