1 | <?php namespace Arcanedev\SeoHelper\Entities; |
||
14 | class Title implements TitleInterface |
||
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 = []) |
|
78 | |||
79 | /** |
||
80 | * Start the engine. |
||
81 | */ |
||
82 | 552 | private function init() |
|
90 | |||
91 | /* ------------------------------------------------------------------------------------------------ |
||
92 | | Getters & Setters |
||
93 | | ------------------------------------------------------------------------------------------------ |
||
94 | */ |
||
95 | /** |
||
96 | * Get title only (without site name or separator). |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 312 | public function getTitleOnly() |
|
104 | |||
105 | /** |
||
106 | * Set title. |
||
107 | * |
||
108 | * @param string $title |
||
109 | * |
||
110 | * @return self |
||
111 | */ |
||
112 | 552 | public function set($title) |
|
119 | |||
120 | /** |
||
121 | * Get site name. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 312 | public function getSiteName() |
|
129 | |||
130 | /** |
||
131 | * Set site name. |
||
132 | * |
||
133 | * @param string $siteName |
||
134 | * |
||
135 | * @return self |
||
136 | */ |
||
137 | 552 | public function setSiteName($siteName) |
|
143 | |||
144 | /** |
||
145 | * Get title separator. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 312 | public function getSeparator() |
|
153 | |||
154 | /** |
||
155 | * Set title separator. |
||
156 | * |
||
157 | * @param string $separator |
||
158 | * |
||
159 | * @return self |
||
160 | */ |
||
161 | 552 | public function setSeparator($separator) |
|
167 | |||
168 | /** |
||
169 | * Set title first. |
||
170 | * |
||
171 | * @return self |
||
172 | */ |
||
173 | 24 | public function setFirst() |
|
177 | |||
178 | /** |
||
179 | * Set title last. |
||
180 | * |
||
181 | * @return self |
||
182 | */ |
||
183 | 24 | public function setLast() |
|
187 | |||
188 | /** |
||
189 | * Switch title position. |
||
190 | * |
||
191 | * @param bool $first |
||
192 | * |
||
193 | * @return self |
||
194 | */ |
||
195 | 552 | private function switchPosition($first) |
|
201 | |||
202 | /** |
||
203 | * Check if title is first. |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | 312 | public function isTitleFirst() |
|
211 | |||
212 | /** |
||
213 | * Get title max length. |
||
214 | * |
||
215 | * @return int |
||
216 | */ |
||
217 | 300 | public function getMax() |
|
221 | |||
222 | /** |
||
223 | * Set title max length. |
||
224 | * |
||
225 | * @param int $max |
||
226 | * |
||
227 | * @return self |
||
228 | */ |
||
229 | 552 | public function setMax($max) |
|
237 | |||
238 | /* ------------------------------------------------------------------------------------------------ |
||
239 | | Main Functions |
||
240 | | ------------------------------------------------------------------------------------------------ |
||
241 | */ |
||
242 | /** |
||
243 | * Make a Title instance. |
||
244 | * |
||
245 | * @param string $title |
||
246 | * @param string $siteName |
||
247 | * @param string $separator |
||
248 | * |
||
249 | * @return self |
||
250 | */ |
||
251 | 12 | public static function make($title, $siteName = '', $separator = '-') |
|
260 | |||
261 | /** |
||
262 | * Render the tag. |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | 288 | public function render() |
|
277 | |||
278 | /** |
||
279 | * Render the separator. |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | 288 | protected function renderSeparator() |
|
287 | |||
288 | /** |
||
289 | * Render the tag. |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | 48 | public function __toString() |
|
297 | |||
298 | /* ------------------------------------------------------------------------------------------------ |
||
299 | | Check Functions |
||
300 | | ------------------------------------------------------------------------------------------------ |
||
301 | */ |
||
302 | /** |
||
303 | * Check title. |
||
304 | * |
||
305 | * @param string $title |
||
306 | * |
||
307 | * @throws \Arcanedev\SeoHelper\Exceptions\InvalidArgumentException |
||
308 | */ |
||
309 | 552 | private function checkTitle(&$title) |
|
327 | |||
328 | /** |
||
329 | * Check title max length. |
||
330 | * |
||
331 | * @param int $max |
||
332 | * |
||
333 | * @throws \Arcanedev\SeoHelper\Exceptions\InvalidArgumentException |
||
334 | */ |
||
335 | 552 | private function checkMax($max) |
|
349 | |||
350 | /* ------------------------------------------------------------------------------------------------ |
||
351 | | Other Functions |
||
352 | | ------------------------------------------------------------------------------------------------ |
||
353 | */ |
||
354 | /** |
||
355 | * Render title first. |
||
356 | * |
||
357 | * @param string $separator |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | 288 | private function renderTitleFirst($separator) |
|
373 | |||
374 | /** |
||
375 | * Render title last. |
||
376 | * |
||
377 | * @param string $separator |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | 12 | private function renderTitleLast($separator) |
|
394 | |||
395 | /** |
||
396 | * Check if site name exists. |
||
397 | * |
||
398 | * @return bool |
||
399 | */ |
||
400 | 288 | private function hasSiteName() |
|
404 | } |
||
405 |