|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraComponents\Seo\Entities; |
|
4
|
|
|
|
|
5
|
|
|
use LaraComponents\Seo\Traits\HasLimit; |
|
6
|
|
|
use LaraComponents\Seo\Contracts\Title as TitleContract; |
|
7
|
|
|
|
|
8
|
|
|
class Title implements TitleContract |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
use HasLimit; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $segments = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $separator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $siteName; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $maxSegment; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $maxTitle; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $reverse; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var bool |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $siteNameVisibility; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Create a new title instance. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $config |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(array $config = []) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->initConfig($config); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Init config. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $config |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function initConfig(array $config) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->setSeparator(isset($config['separator']) ? $config['separator'] : ' :: '); |
|
66
|
|
|
$this->setSiteName(isset($config['site_name']) ? $config['site_name'] : null); |
|
67
|
|
|
$this->setMaxSegemnt(isset($config['max_segment']) ? $config['max_segment'] : 0); |
|
68
|
|
|
$this->setMaxTitle(isset($config['max_title']) ? $config['max_title'] : 0); |
|
69
|
|
|
$this->setReverse(isset($config['reverse']) ? $config['reverse'] : false); |
|
70
|
|
|
$this->setSiteNameVisibility(isset($config['site_name_visible']) ? $config['site_name_visible'] : true); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set title separator. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $separator |
|
77
|
|
|
* @return \LaraComponents\Seo\Title |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setSeparator($separator) |
|
80
|
|
|
{ |
|
81
|
|
|
if (! is_null($separator)) { |
|
82
|
|
|
$this->separator = (string) $separator; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get title separator. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getSeparator() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->separator; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set site name. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $siteName |
|
102
|
|
|
* @return \LaraComponents\Seo\Title |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setSiteName($siteName) |
|
105
|
|
|
{ |
|
106
|
|
|
if (! is_null($siteName)) { |
|
107
|
|
|
$this->siteName = (string) $siteName; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get site name. |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getSiteName() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->siteName; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set segment max length. |
|
125
|
|
|
* |
|
126
|
|
|
* @param int $max |
|
127
|
|
|
* @return \LaraComponents\Seo\Title |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setMaxSegemnt($max) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->maxSegment = (int) $max; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get segment max length. |
|
138
|
|
|
* |
|
139
|
|
|
* @return int |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getMaxSegment() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->maxSegment; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set title max length. |
|
148
|
|
|
* |
|
149
|
|
|
* @param int $max |
|
150
|
|
|
* @return \LaraComponents\Seo\Title |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setMaxTitle($max) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->maxTitle = (int) $max; |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get title max length. |
|
161
|
|
|
* |
|
162
|
|
|
* @return int |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getMaxTitle() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->maxTitle; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Set reverse title position. |
|
171
|
|
|
* |
|
172
|
|
|
* @param bool $reverse |
|
173
|
|
|
* @return \LaraComponents\Seo\Title |
|
174
|
|
|
*/ |
|
175
|
|
|
public function setReverse($reverse = true) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->reverse = (bool) $reverse; |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Check reverse title position. |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
public function isReverse() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->reverse; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Set the site name visibility. |
|
194
|
|
|
* |
|
195
|
|
|
* @param bool $visible |
|
196
|
|
|
*/ |
|
197
|
|
|
public function setSiteNameVisibility($visible) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->siteNameVisibility = (bool) $visible; |
|
200
|
|
|
|
|
201
|
|
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Check if site name exists and visible. |
|
206
|
|
|
* |
|
207
|
|
|
* @return bool |
|
208
|
|
|
*/ |
|
209
|
|
|
public function isSiteNameVisible() |
|
210
|
|
|
{ |
|
211
|
|
|
return ! is_null($this->siteName) && $this->siteNameVisibility; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Appent title to segments. |
|
216
|
|
|
* |
|
217
|
|
|
* @return \LaraComponents\Seo\Title |
|
218
|
|
|
*/ |
|
219
|
|
|
public function add() |
|
220
|
|
|
{ |
|
221
|
|
|
$segments = func_get_args(); |
|
222
|
|
|
|
|
223
|
|
|
foreach ($this->normalizeSegments($segments) as $segment) { |
|
224
|
|
|
$this->segments[] = $segment; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $this; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Set title segments. |
|
232
|
|
|
* |
|
233
|
|
|
* @return \LaraComponents\Seo\Title |
|
234
|
|
|
*/ |
|
235
|
|
|
public function set() |
|
236
|
|
|
{ |
|
237
|
|
|
$segments = func_get_args(); |
|
238
|
|
|
|
|
239
|
|
|
$this->segments = $this->normalizeSegments($segments); |
|
240
|
|
|
|
|
241
|
|
|
return $this; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get title segments. |
|
246
|
|
|
* |
|
247
|
|
|
* @return array |
|
248
|
|
|
*/ |
|
249
|
|
|
public function get() |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->segments; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Make the title string. |
|
256
|
|
|
* |
|
257
|
|
|
* @return string |
|
258
|
|
|
*/ |
|
259
|
|
|
public function toString() |
|
260
|
|
|
{ |
|
261
|
|
|
$parts = []; |
|
262
|
|
|
if ($this->isSiteNameVisible()) { |
|
263
|
|
|
$parts[] = $this->siteName; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
if (count($this->segments)) { |
|
267
|
|
|
$segments = $this->isReverse() ? array_reverse($this->segments) : $this->segments; |
|
268
|
|
|
|
|
269
|
|
|
$title = implode($this->separator, $segments); |
|
270
|
|
|
$title = $this->maxTitle > 0 ? $this->limit($title, $this->maxTitle) : $title; |
|
271
|
|
|
|
|
272
|
|
|
$this->isReverse() ? array_push($parts, $title) : array_unshift($parts, $title); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return implode($this->separator, $parts); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Normalize the get segments. |
|
280
|
|
|
* |
|
281
|
|
|
* @param array $segments |
|
282
|
|
|
* @return array |
|
283
|
|
|
*/ |
|
284
|
|
|
protected function normalizeSegments($segments) |
|
285
|
|
|
{ |
|
286
|
|
|
$normalized = []; |
|
287
|
|
|
|
|
288
|
|
|
foreach ($segments as $segment) { |
|
289
|
|
|
if (is_array($segment)) { |
|
290
|
|
|
$normalized = array_merge($normalized, $this->normalizeSegments($segment)); |
|
291
|
|
|
} else { |
|
292
|
|
|
$segment = trim(strip_tags((string) $segment)); |
|
293
|
|
|
|
|
294
|
|
|
if (mb_strlen($segment) > 0) { |
|
295
|
|
|
$normalized[] = $this->maxSegment > 0 ? $this->limit($segment, $this->maxSegment) : $segment; |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
return $normalized; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Convert object to string. |
|
305
|
|
|
* |
|
306
|
|
|
* @return string |
|
307
|
|
|
*/ |
|
308
|
|
|
public function __toString() |
|
309
|
|
|
{ |
|
310
|
|
|
return $this->toString(); |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|