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 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 = []) |
|
76 | 414 | ||
77 | 552 | /** |
|
78 | * Start the engine. |
||
79 | */ |
||
80 | private function init() |
||
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() |
||
102 | 312 | ||
103 | /** |
||
104 | * Set title. |
||
105 | * |
||
106 | * @param string $title |
||
107 | * |
||
108 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
109 | */ |
||
110 | public function set($title) |
||
117 | 552 | ||
118 | /** |
||
119 | * Get site name. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getSiteName() |
||
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) |
||
141 | 552 | ||
142 | /** |
||
143 | * Get title separator. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getSeparator() |
||
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) |
||
165 | 552 | ||
166 | /** |
||
167 | * Set title first. |
||
168 | * |
||
169 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
170 | */ |
||
171 | public function setFirst() |
||
175 | 24 | ||
176 | /** |
||
177 | * Set title last. |
||
178 | * |
||
179 | * @return \Arcanedev\SeoHelper\Entities\Title |
||
180 | */ |
||
181 | public function setLast() |
||
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) |
||
199 | 552 | ||
200 | /** |
||
201 | * Check if title is first. |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function isTitleFirst() |
||
209 | 312 | ||
210 | /** |
||
211 | * Get title max length. |
||
212 | * |
||
213 | * @return int |
||
214 | */ |
||
215 | public function getMax() |
||
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) |
||
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 = '-') |
||
258 | 9 | ||
259 | /** |
||
260 | * Render the tag. |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function render() |
||
273 | 288 | ||
274 | /** |
||
275 | 288 | * Render the separator. |
|
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | protected function renderSeparator() |
||
283 | 288 | ||
284 | /** |
||
285 | 288 | * Render the tag. |
|
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | public function __toString() |
||
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() |
||
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) |
|
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) |
|
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) |
||
371 | 288 | ||
372 | /** |
||
373 | * Render title last. |
||
374 | * |
||
375 | * @param string $separator |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | private function renderTitleLast($separator) |
||
392 | 12 | ||
393 | /** |
||
394 | * Prepare the title output. |
||
395 | * |
||
396 | * @param string $output |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | 288 | private function prepareTitleOutput($output) |
|
406 | } |
||
407 |