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