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 | 540 | */ |
|
70 | public function __construct(array $configs = []) |
||
71 | 540 | { |
|
72 | $this->setConfigs($configs); |
||
73 | 540 | ||
74 | 540 | if ( ! empty($configs)) { |
|
75 | 405 | $this->init(); |
|
76 | 540 | } |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Start the engine. |
||
81 | 540 | */ |
|
82 | private function init() |
||
83 | 540 | { |
|
84 | 540 | $this->set($this->getConfig('default', '')); |
|
85 | 540 | $this->setSiteName($this->getConfig('site-name', '')); |
|
86 | 540 | $this->setSeparator($this->getConfig('separator', '-')); |
|
87 | 540 | $this->switchPosition($this->getConfig('first', true)); |
|
88 | 540 | $this->setMax($this->getConfig('max', 55)); |
|
89 | } |
||
90 | |||
91 | /* ------------------------------------------------------------------------------------------------ |
||
92 | | Getters & Setters |
||
93 | | ------------------------------------------------------------------------------------------------ |
||
94 | */ |
||
95 | /** |
||
96 | * Get title only (without site name or separator). |
||
97 | * |
||
98 | * @return string |
||
99 | 300 | */ |
|
100 | public function getTitleOnly() |
||
101 | 300 | { |
|
102 | return $this->title; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Set title. |
||
107 | * |
||
108 | * @param string $title |
||
109 | * |
||
110 | * @return self |
||
111 | 540 | */ |
|
112 | public function set($title) |
||
113 | 540 | { |
|
114 | 540 | $this->checkTitle($title); |
|
115 | $this->title = $title; |
||
116 | 540 | ||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Get site name. |
||
122 | * |
||
123 | * @return string |
||
124 | 300 | */ |
|
125 | public function getSiteName() |
||
126 | 300 | { |
|
127 | return $this->siteName; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Set site name. |
||
132 | * |
||
133 | * @param string $siteName |
||
134 | * |
||
135 | * @return self |
||
136 | 540 | */ |
|
137 | public function setSiteName($siteName) |
||
138 | 540 | { |
|
139 | $this->siteName = $siteName; |
||
140 | 540 | ||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get title separator. |
||
146 | * |
||
147 | * @return string |
||
148 | 300 | */ |
|
149 | public function getSeparator() |
||
150 | 300 | { |
|
151 | return $this->separator; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Set title separator. |
||
156 | * |
||
157 | * @param string $separator |
||
158 | * |
||
159 | * @return self |
||
160 | 540 | */ |
|
161 | public function setSeparator($separator) |
||
162 | 540 | { |
|
163 | $this->separator = trim($separator); |
||
164 | 540 | ||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Set title first. |
||
170 | * |
||
171 | * @return self |
||
172 | 24 | */ |
|
173 | public function setFirst() |
||
174 | 24 | { |
|
175 | return $this->switchPosition(true); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Set title last. |
||
180 | * |
||
181 | * @return self |
||
182 | 24 | */ |
|
183 | public function setLast() |
||
184 | 24 | { |
|
185 | return $this->switchPosition(false); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Switch title position. |
||
190 | * |
||
191 | * @param bool $first |
||
192 | * |
||
193 | * @return self |
||
194 | 540 | */ |
|
195 | private function switchPosition($first) |
||
196 | 540 | { |
|
197 | $this->titleFirst = boolval($first); |
||
198 | 540 | ||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Check if title is first. |
||
204 | * |
||
205 | * @return bool |
||
206 | 300 | */ |
|
207 | public function isTitleFirst() |
||
208 | 300 | { |
|
209 | return $this->titleFirst; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get title max length. |
||
214 | * |
||
215 | * @return int |
||
216 | 288 | */ |
|
217 | public function getMax() |
||
218 | 288 | { |
|
219 | return $this->max; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Set title max length. |
||
224 | * |
||
225 | * @param int $max |
||
226 | * |
||
227 | * @return self |
||
228 | 540 | */ |
|
229 | public function setMax($max) |
||
230 | 540 | { |
|
231 | $this->checkMax($max); |
||
232 | 540 | ||
233 | $this->max = $max; |
||
234 | 540 | ||
235 | return $this; |
||
236 | } |
||
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 | 12 | */ |
|
251 | public static function make($title, $siteName = '', $separator = '-') |
||
252 | 12 | { |
|
253 | 12 | return new self([ |
|
254 | 12 | 'default' => $title, |
|
255 | 12 | 'site-name' => $siteName, |
|
256 | 'separator' => $separator, |
||
257 | 9 | 'first' => true |
|
258 | ]); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Render the tag. |
||
263 | * |
||
264 | * @return string |
||
265 | 276 | */ |
|
266 | public function render() |
||
267 | 276 | { |
|
268 | $separator = $this->renderSeparator(); |
||
269 | 276 | $output = $this->isTitleFirst() |
|
270 | 276 | ? $this->renderTitleFirst($separator) |
|
271 | 276 | : $this->renderTitleLast($separator); |
|
272 | |||
273 | 276 | $output = Str::limit(strip_tags($output), $this->getMax()); |
|
274 | |||
275 | 276 | return '<title>' . e($output) . '</title>'; |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * Render the separator. |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | 48 | protected function renderSeparator() |
|
287 | |||
288 | /** |
||
289 | * Render the tag. |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | public function __toString() |
||
294 | { |
||
295 | return $this->render(); |
||
296 | } |
||
297 | |||
298 | /* ------------------------------------------------------------------------------------------------ |
||
299 | 540 | | Check Functions |
|
300 | | ------------------------------------------------------------------------------------------------ |
||
301 | 540 | */ |
|
302 | 12 | /** |
|
303 | * Check title. |
||
304 | 12 | * |
|
305 | 12 | * @param string $title |
|
306 | 9 | * |
|
307 | * @throws \Arcanedev\SeoHelper\Exceptions\InvalidArgumentException |
||
308 | */ |
||
309 | 540 | private function checkTitle(&$title) |
|
327 | 540 | ||
328 | 12 | /** |
|
329 | 3 | * Check title max length. |
|
330 | 9 | * |
|
331 | * @param int $max |
||
332 | * |
||
333 | 540 | * @throws \Arcanedev\SeoHelper\Exceptions\InvalidArgumentException |
|
334 | 12 | */ |
|
335 | 3 | private function checkMax($max) |
|
349 | |||
350 | /* ------------------------------------------------------------------------------------------------ |
||
351 | 276 | | Other Functions |
|
352 | | ------------------------------------------------------------------------------------------------ |
||
353 | 276 | */ |
|
354 | 276 | /** |
|
355 | * Render title first. |
||
356 | 276 | * |
|
357 | 60 | * @param string $separator |
|
358 | 60 | * |
|
359 | 45 | * @return string |
|
360 | */ |
||
361 | 276 | private function renderTitleFirst($separator) |
|
373 | 12 | ||
374 | /** |
||
375 | 12 | * Render title last. |
|
376 | 12 | * |
|
377 | 12 | * @param string $separator |
|
378 | 9 | * |
|
379 | * @return string |
||
380 | 12 | */ |
|
381 | private function renderTitleLast($separator) |
||
394 | |||
395 | /** |
||
396 | * Check if site name exists. |
||
397 | * |
||
398 | * @return bool |
||
399 | */ |
||
400 | private function hasSiteName() |
||
404 | } |
||
405 |