1 | <?php namespace Arcanedev\SeoHelper\Entities; |
||
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 site name visibility. |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $siteNameVisibility = true; |
||
46 | |||
47 | /** |
||
48 | * The title separator. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $separator = '-'; |
||
53 | |||
54 | /** |
||
55 | * Display the title first. |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $titleFirst = true; |
||
60 | |||
61 | /** |
||
62 | * The maximum title length. |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $max = 55; |
||
67 | |||
68 | /* ----------------------------------------------------------------- |
||
69 | | Constructor |
||
70 | | ----------------------------------------------------------------- |
||
71 | */ |
||
72 | /** |
||
73 | * Make the Title instance. |
||
74 | * |
||
75 | * @param array $configs |
||
76 | */ |
||
77 | 153 | public function __construct(array $configs = []) |
|
83 | |||
84 | /** |
||
85 | * Start the engine. |
||
86 | */ |
||
87 | 153 | private function init() |
|
95 | |||
96 | /* ----------------------------------------------------------------- |
||
97 | | Getters & Setters |
||
98 | | ----------------------------------------------------------------- |
||
99 | */ |
||
100 | /** |
||
101 | * Get title only (without site name or separator). |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 93 | public function getTitleOnly() |
|
109 | |||
110 | /** |
||
111 | * Set title. |
||
112 | * |
||
113 | * @param string $title |
||
114 | * |
||
115 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
116 | */ |
||
117 | 153 | public function set($title) |
|
124 | |||
125 | /** |
||
126 | * Get site name. |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | 93 | public function getSiteName() |
|
134 | |||
135 | /** |
||
136 | * Set site name. |
||
137 | * |
||
138 | * @param string $siteName |
||
139 | * |
||
140 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
141 | */ |
||
142 | 153 | public function setSiteName($siteName) |
|
149 | |||
150 | /** |
||
151 | * Hide the site name. |
||
152 | * |
||
153 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
154 | */ |
||
155 | 6 | public function hideSiteName() |
|
159 | |||
160 | /** |
||
161 | * Show the site name. |
||
162 | * |
||
163 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
164 | */ |
||
165 | 6 | public function showSiteName() |
|
169 | |||
170 | /** |
||
171 | * Set the site name visibility. |
||
172 | * |
||
173 | * @param bool $visible |
||
174 | * |
||
175 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
176 | */ |
||
177 | 6 | public function setSiteNameVisibility($visible) |
|
183 | |||
184 | /** |
||
185 | * Get title separator. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 93 | public function getSeparator() |
|
193 | |||
194 | /** |
||
195 | * Set title separator. |
||
196 | * |
||
197 | * @param string $separator |
||
198 | * |
||
199 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
200 | */ |
||
201 | 153 | public function setSeparator($separator) |
|
208 | |||
209 | /** |
||
210 | * Set title first. |
||
211 | * |
||
212 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
213 | */ |
||
214 | 6 | public function setFirst() |
|
218 | |||
219 | /** |
||
220 | * Set title last. |
||
221 | * |
||
222 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
223 | */ |
||
224 | 6 | public function setLast() |
|
228 | |||
229 | /** |
||
230 | * Switch title position. |
||
231 | * |
||
232 | * @param bool $first |
||
233 | * |
||
234 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
235 | */ |
||
236 | 153 | private function switchPosition($first) |
|
242 | |||
243 | /** |
||
244 | * Check if title is first. |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 93 | public function isTitleFirst() |
|
252 | |||
253 | /** |
||
254 | * Get title max length. |
||
255 | * |
||
256 | * @return int |
||
257 | */ |
||
258 | 90 | public function getMax() |
|
262 | |||
263 | /** |
||
264 | * Set title max length. |
||
265 | * |
||
266 | * @param int $max |
||
267 | * |
||
268 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
269 | */ |
||
270 | 153 | public function setMax($max) |
|
278 | |||
279 | /* ----------------------------------------------------------------- |
||
280 | | Main Methods |
||
281 | | ----------------------------------------------------------------- |
||
282 | */ |
||
283 | /** |
||
284 | * Make a Title instance. |
||
285 | * |
||
286 | * @param string $title |
||
287 | * @param string $siteName |
||
288 | * @param string $separator |
||
289 | * |
||
290 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
291 | */ |
||
292 | 6 | public static function make($title, $siteName = '', $separator = '-') |
|
301 | |||
302 | /** |
||
303 | * Render the tag. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | 87 | public function render() |
|
316 | |||
317 | /** |
||
318 | * Render the separator. |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | 87 | protected function renderSeparator() |
|
326 | |||
327 | /** |
||
328 | * Render the tag. |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | 15 | public function __toString() |
|
336 | |||
337 | /* ----------------------------------------------------------------- |
||
338 | | Check Methods |
||
339 | | ----------------------------------------------------------------- |
||
340 | */ |
||
341 | /** |
||
342 | * Check if site name exists. |
||
343 | * |
||
344 | * @return bool |
||
345 | */ |
||
346 | 87 | private function hasSiteName() |
|
350 | |||
351 | /** |
||
352 | * Check title. |
||
353 | * |
||
354 | * @param string $title |
||
355 | * |
||
356 | * @throws \Arcanedev\SeoHelper\Exceptions\InvalidArgumentException |
||
357 | */ |
||
358 | 153 | private function checkTitle(&$title) |
|
372 | |||
373 | /** |
||
374 | * Check title max length. |
||
375 | * |
||
376 | * @param int $max |
||
377 | * |
||
378 | * @throws \Arcanedev\SeoHelper\Exceptions\InvalidArgumentException |
||
379 | */ |
||
380 | 153 | private function checkMax($max) |
|
390 | |||
391 | /* ----------------------------------------------------------------- |
||
392 | | Other Methods |
||
393 | | ----------------------------------------------------------------- |
||
394 | */ |
||
395 | /** |
||
396 | * Render title first. |
||
397 | * |
||
398 | * @param string $separator |
||
399 | * |
||
400 | * @return string |
||
401 | */ |
||
402 | 87 | private function renderTitleFirst($separator) |
|
414 | |||
415 | /** |
||
416 | * Render title last. |
||
417 | * |
||
418 | * @param string $separator |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | 3 | private function renderTitleLast($separator) |
|
435 | |||
436 | /** |
||
437 | * Prepare the title output. |
||
438 | * |
||
439 | * @param string $output |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | 87 | private function prepareTitleOutput($output) |
|
449 | } |
||
450 |