-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseClass.php
164 lines (142 loc) · 3.46 KB
/
BaseClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* This class contains commonly-used features on HTML element,
* such as slot, custom class, create element ID and store event handler.
* By extending this class, it allows all Bootstrap components to have
* their common functionalities without writing the codes repeatedly
*/
class BaseClass
{
/**
* @var string|array
*/
protected $slots = null;
/**
* @var string
*/
protected $additionalClass = '';
/**
* @var string
*/
protected $elemId = '';
/**
* @var array
*/
protected $attributes = [];
/**
* @var boolean
*
* If set to TRUE, SimpleTag render() function
* will return HTML string, otherwise, it
* will print the output directly to the browser
*/
protected $sendAsValue = false;
/**
* @var string
*/
protected $positionClass = '';
/**
* @var string
*/
protected $positionType = 'position-absolute';
/**
* Set position of an element.
* See https://getbootstrap.com/docs/5.1/utilities/position/ for detail explanation.
*
* @param string $positionStart
* @param string $positionEnd
* @param string $translate
*
* @return this
*/
public function position(string $positionStart, string $positionEnd, string $translate = '')
{
$classArray = [
$this->positionType,
$positionStart,
$positionEnd,
! empty($translate) ? 'translate-' . $translate : ''
];
$this->positionClass = ' ' . implode(' ', $classArray);
return $this;
}
/**
* Set position values: static, relative, absolute, fixed, sticky
*
* @param string $value
*
* @return this
*/
public function positionType(string $value)
{
$acceptedValues = ['static', 'relative', 'absolute', 'fixed', 'sticky'];
if(array_search($value, $acceptedValues)) {
$this->positionType = 'position-' . $value;
return $this;
}
}
/**
* Prevent SimpleTag render() function to send
* the output to the browser directly
*
* @return this
*/
public function preventBrowserOutput()
{
$this->sendAsValue = true;
return $this;
}
/**
* Add slot to SimpleTag::content()
* Note:
* Not all components support slot(), see the API Docs
* for each component to know them.
*
* @param string|array $slots
*
* @return this
*/
public function slot(string|array $slots)
{
$this->slots = $slots;
return $this;
}
/**
* Add custom class to element
*
* @param string|array $class
*
* @return this
*/
public function addClass(string|array $class)
{
is_array($class)
? $this->additionalClass = ' ' . implode(' ', $class)
: $this->additionalClass = " $class";
return $this;
}
/**
* Create ID for an element
*
* @param string $id
*
* @return this
*/
public function id(string $id)
{
$this->attributes['id'] = $id;
return $this;
}
/**
* Store element attributes
*
* @param array $attributes
*
* @return this
*/
public function attr(array $attributes = [])
{
$this->attributes = array_merge($this->attributes, $attributes);
return $this;
}
}