1
|
|
|
<?php namespace Arcanedev\SeoHelper\Entities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\SeoHelper\Contracts\Entities\Title as TitleContract; |
4
|
|
|
use Arcanedev\SeoHelper\Exceptions\InvalidArgumentException; |
5
|
|
|
use Arcanedev\Support\Traits\Configurable; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Title |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\SeoHelper\Entities |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Title implements TitleContract |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Traits |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
use Configurable; |
21
|
|
|
|
22
|
|
|
/* ------------------------------------------------------------------------------------------------ |
23
|
|
|
| Properties |
24
|
|
|
| ------------------------------------------------------------------------------------------------ |
25
|
|
|
*/ |
26
|
|
|
/** |
27
|
|
|
* The title content. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $title = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The site name. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $siteName = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The title separator. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $separator = '-'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Display the title first. |
49
|
|
|
* |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
protected $titleFirst = true; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The maximum title length. |
56
|
|
|
* |
57
|
|
|
* @var int |
58
|
|
|
*/ |
59
|
|
|
protected $max = 55; |
60
|
|
|
|
61
|
|
|
/* ------------------------------------------------------------------------------------------------ |
62
|
|
|
| Constructor |
63
|
|
|
| ------------------------------------------------------------------------------------------------ |
64
|
|
|
*/ |
65
|
|
|
/** |
66
|
|
|
* Make the Title instance. |
67
|
|
|
* |
68
|
|
|
* @param array $configs |
69
|
|
|
*/ |
70
|
552 |
|
public function __construct(array $configs = []) |
71
|
|
|
{ |
72
|
552 |
|
$this->setConfigs($configs); |
73
|
|
|
|
74
|
552 |
|
if ( ! empty($configs)) $this->init(); |
75
|
552 |
|
} |
76
|
414 |
|
|
77
|
552 |
|
/** |
78
|
|
|
* Start the engine. |
79
|
|
|
*/ |
80
|
|
|
private function init() |
81
|
|
|
{ |
82
|
552 |
|
$this->set($this->getConfig('default', '')); |
83
|
|
|
$this->setSiteName($this->getConfig('site-name', '')); |
84
|
552 |
|
$this->setSeparator($this->getConfig('separator', '-')); |
85
|
552 |
|
$this->switchPosition($this->getConfig('first', true)); |
86
|
552 |
|
$this->setMax($this->getConfig('max', 55)); |
87
|
552 |
|
} |
88
|
552 |
|
|
89
|
552 |
|
/* ------------------------------------------------------------------------------------------------ |
90
|
|
|
| Getters & Setters |
91
|
|
|
| ------------------------------------------------------------------------------------------------ |
92
|
|
|
*/ |
93
|
|
|
/** |
94
|
|
|
* Get title only (without site name or separator). |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getTitleOnly() |
99
|
|
|
{ |
100
|
312 |
|
return $this->title; |
101
|
|
|
} |
102
|
312 |
|
|
103
|
|
|
/** |
104
|
|
|
* Set title. |
105
|
|
|
* |
106
|
|
|
* @param string $title |
107
|
|
|
* |
108
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Title |
109
|
|
|
*/ |
110
|
|
|
public function set($title) |
111
|
|
|
{ |
112
|
552 |
|
$this->checkTitle($title); |
113
|
|
|
$this->title = $title; |
114
|
552 |
|
|
115
|
552 |
|
return $this; |
116
|
|
|
} |
117
|
552 |
|
|
118
|
|
|
/** |
119
|
|
|
* Get site name. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getSiteName() |
124
|
|
|
{ |
125
|
312 |
|
return $this->siteName; |
126
|
|
|
} |
127
|
312 |
|
|
128
|
|
|
/** |
129
|
|
|
* Set site name. |
130
|
|
|
* |
131
|
|
|
* @param string $siteName |
132
|
|
|
* |
133
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Title |
134
|
|
|
*/ |
135
|
|
|
public function setSiteName($siteName) |
136
|
|
|
{ |
137
|
552 |
|
$this->siteName = $siteName; |
138
|
|
|
|
139
|
552 |
|
return $this; |
140
|
|
|
} |
141
|
552 |
|
|
142
|
|
|
/** |
143
|
|
|
* Get title separator. |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getSeparator() |
148
|
|
|
{ |
149
|
312 |
|
return $this->separator; |
150
|
|
|
} |
151
|
312 |
|
|
152
|
|
|
/** |
153
|
|
|
* Set title separator. |
154
|
|
|
* |
155
|
|
|
* @param string $separator |
156
|
|
|
* |
157
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Title |
158
|
|
|
*/ |
159
|
|
|
public function setSeparator($separator) |
160
|
|
|
{ |
161
|
552 |
|
$this->separator = trim($separator); |
162
|
|
|
|
163
|
552 |
|
return $this; |
164
|
|
|
} |
165
|
552 |
|
|
166
|
|
|
/** |
167
|
|
|
* Set title first. |
168
|
|
|
* |
169
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Title |
170
|
|
|
*/ |
171
|
|
|
public function setFirst() |
172
|
|
|
{ |
173
|
24 |
|
return $this->switchPosition(true); |
174
|
|
|
} |
175
|
24 |
|
|
176
|
|
|
/** |
177
|
|
|
* Set title last. |
178
|
|
|
* |
179
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Title |
180
|
|
|
*/ |
181
|
|
|
public function setLast() |
182
|
|
|
{ |
183
|
24 |
|
return $this->switchPosition(false); |
184
|
|
|
} |
185
|
24 |
|
|
186
|
|
|
/** |
187
|
|
|
* Switch title position. |
188
|
|
|
* |
189
|
|
|
* @param bool $first |
190
|
|
|
* |
191
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Title |
192
|
|
|
*/ |
193
|
|
|
private function switchPosition($first) |
194
|
|
|
{ |
195
|
552 |
|
$this->titleFirst = boolval($first); |
196
|
|
|
|
197
|
552 |
|
return $this; |
198
|
|
|
} |
199
|
552 |
|
|
200
|
|
|
/** |
201
|
|
|
* Check if title is first. |
202
|
|
|
* |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
public function isTitleFirst() |
206
|
|
|
{ |
207
|
312 |
|
return $this->titleFirst; |
208
|
|
|
} |
209
|
312 |
|
|
210
|
|
|
/** |
211
|
|
|
* Get title max length. |
212
|
|
|
* |
213
|
|
|
* @return int |
214
|
|
|
*/ |
215
|
|
|
public function getMax() |
216
|
|
|
{ |
217
|
300 |
|
return $this->max; |
218
|
|
|
} |
219
|
300 |
|
|
220
|
|
|
/** |
221
|
|
|
* Set title max length. |
222
|
|
|
* |
223
|
|
|
* @param int $max |
224
|
|
|
* |
225
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Title |
226
|
|
|
*/ |
227
|
|
|
public function setMax($max) |
228
|
|
|
{ |
229
|
552 |
|
$this->checkMax($max); |
230
|
|
|
|
231
|
552 |
|
$this->max = $max; |
232
|
|
|
|
233
|
552 |
|
return $this; |
234
|
|
|
} |
235
|
552 |
|
|
236
|
|
|
/* ------------------------------------------------------------------------------------------------ |
237
|
|
|
| Main Functions |
238
|
|
|
| ------------------------------------------------------------------------------------------------ |
239
|
|
|
*/ |
240
|
|
|
/** |
241
|
|
|
* Make a Title instance. |
242
|
|
|
* |
243
|
|
|
* @param string $title |
244
|
|
|
* @param string $siteName |
245
|
|
|
* @param string $separator |
246
|
|
|
* |
247
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Title |
248
|
|
|
*/ |
249
|
|
|
public static function make($title, $siteName = '', $separator = '-') |
250
|
|
|
{ |
251
|
12 |
|
return new self([ |
252
|
|
|
'default' => $title, |
253
|
12 |
|
'site-name' => $siteName, |
254
|
12 |
|
'separator' => $separator, |
255
|
12 |
|
'first' => true |
256
|
12 |
|
]); |
257
|
|
|
} |
258
|
9 |
|
|
259
|
|
|
/** |
260
|
|
|
* Render the tag. |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
public function render() |
265
|
|
|
{ |
266
|
288 |
|
$separator = $this->renderSeparator(); |
267
|
|
|
$output = $this->isTitleFirst() |
268
|
288 |
|
? $this->renderTitleFirst($separator) |
269
|
288 |
|
: $this->renderTitleLast($separator); |
270
|
288 |
|
|
271
|
288 |
|
return '<title>' . $this->prepareTitleOutput($output) . '</title>'; |
272
|
|
|
} |
273
|
288 |
|
|
274
|
|
|
/** |
275
|
288 |
|
* Render the separator. |
276
|
|
|
* |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
protected function renderSeparator() |
280
|
|
|
{ |
281
|
|
|
return empty($separator = $this->getSeparator()) ? ' ' : " $separator "; |
282
|
|
|
} |
283
|
288 |
|
|
284
|
|
|
/** |
285
|
288 |
|
* Render the tag. |
286
|
|
|
* |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
public function __toString() |
290
|
|
|
{ |
291
|
|
|
return $this->render(); |
292
|
|
|
} |
293
|
48 |
|
|
294
|
|
|
/* ------------------------------------------------------------------------------------------------ |
295
|
48 |
|
| Check Functions |
296
|
|
|
| ------------------------------------------------------------------------------------------------ |
297
|
|
|
*/ |
298
|
|
|
/** |
299
|
|
|
* Check if site name exists. |
300
|
|
|
* |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
|
|
private function hasSiteName() |
304
|
|
|
{ |
305
|
|
|
return ! empty($this->getSiteName()); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
552 |
|
* Check title. |
310
|
|
|
* |
311
|
552 |
|
* @param string $title |
312
|
12 |
|
* |
313
|
|
|
* @throws \Arcanedev\SeoHelper\Exceptions\InvalidArgumentException |
314
|
12 |
|
*/ |
315
|
12 |
|
private function checkTitle(&$title) |
316
|
9 |
|
{ |
317
|
|
|
if ( ! is_string($title)) { |
318
|
|
|
$type = gettype($title); |
319
|
552 |
|
|
320
|
|
|
throw new InvalidArgumentException("The title must be a string value, [$type] is given."); |
321
|
552 |
|
} |
322
|
12 |
|
|
323
|
3 |
|
$title = trim($title); |
324
|
9 |
|
|
325
|
|
|
if (empty($title)) { |
326
|
552 |
|
throw new InvalidArgumentException('The title is required and must not be empty.'); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Check title max length. |
332
|
|
|
* |
333
|
|
|
* @param int $max |
334
|
|
|
* |
335
|
552 |
|
* @throws \Arcanedev\SeoHelper\Exceptions\InvalidArgumentException |
336
|
|
|
*/ |
337
|
552 |
|
private function checkMax($max) |
338
|
12 |
|
{ |
339
|
3 |
|
if ( ! is_int($max)) { |
340
|
9 |
|
throw new InvalidArgumentException('The title maximum lenght must be integer.'); |
341
|
|
|
} |
342
|
|
|
|
343
|
552 |
|
if ($max <= 0) { |
344
|
12 |
|
throw new InvalidArgumentException('The title maximum lenght must be greater 0.'); |
345
|
3 |
|
} |
346
|
9 |
|
} |
347
|
|
|
|
348
|
552 |
|
/* ------------------------------------------------------------------------------------------------ |
349
|
|
|
| Other Functions |
350
|
|
|
| ------------------------------------------------------------------------------------------------ |
351
|
|
|
*/ |
352
|
|
|
/** |
353
|
|
|
* Render title first. |
354
|
|
|
* |
355
|
|
|
* @param string $separator |
356
|
|
|
* |
357
|
|
|
* @return string |
358
|
|
|
*/ |
359
|
|
|
private function renderTitleFirst($separator) |
360
|
|
|
{ |
361
|
288 |
|
$output = []; |
362
|
|
|
$output[] = $this->getTitleOnly(); |
363
|
288 |
|
|
364
|
288 |
|
if ($this->hasSiteName()) { |
365
|
|
|
$output[] = $separator; |
366
|
288 |
|
$output[] = $this->getSiteName(); |
367
|
60 |
|
} |
368
|
60 |
|
|
369
|
45 |
|
return implode('', $output); |
370
|
|
|
} |
371
|
288 |
|
|
372
|
|
|
/** |
373
|
|
|
* Render title last. |
374
|
|
|
* |
375
|
|
|
* @param string $separator |
376
|
|
|
* |
377
|
|
|
* @return string |
378
|
|
|
*/ |
379
|
|
|
private function renderTitleLast($separator) |
380
|
|
|
{ |
381
|
12 |
|
$output = []; |
382
|
|
|
|
383
|
12 |
|
if ($this->hasSiteName()) { |
384
|
|
|
$output[] = $this->getSiteName(); |
385
|
12 |
|
$output[] = $separator; |
386
|
12 |
|
} |
387
|
12 |
|
|
388
|
9 |
|
$output[] = $this->getTitleOnly(); |
389
|
|
|
|
390
|
12 |
|
return implode('', $output); |
391
|
|
|
} |
392
|
12 |
|
|
393
|
|
|
/** |
394
|
|
|
* Prepare the title output. |
395
|
|
|
* |
396
|
|
|
* @param string $output |
397
|
|
|
* |
398
|
|
|
* @return string |
399
|
|
|
*/ |
400
|
288 |
|
private function prepareTitleOutput($output) |
401
|
|
|
{ |
402
|
288 |
|
return htmlspecialchars( |
403
|
|
|
Str::limit(strip_tags($output), $this->getMax()), ENT_QUOTES, 'UTF-8', false |
404
|
|
|
); |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|