Completed
Push — master ( 2006e3...2912e5 )
by ARCANEDEV
9s
created

Title::hasSiteName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\SeoHelper\Entities;
2
3
use Arcanedev\SeoHelper\Contracts\Entities\TitleInterface;
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 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 = [])
71
    {
72 552
        $this->setConfigs($configs);
73
74 552
        if ( ! empty($configs)) {
75 552
            $this->init();
76 414
        }
77 552
    }
78
79
    /**
80
     * Start the engine.
81
     */
82 552
    private function init()
83
    {
84 552
        $this->set($this->getConfig('default', ''));
85 552
        $this->setSiteName($this->getConfig('site-name', ''));
86 552
        $this->setSeparator($this->getConfig('separator', '-'));
87 552
        $this->switchPosition($this->getConfig('first', true));
88 552
        $this->setMax($this->getConfig('max', 55));
89 552
    }
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()
101
    {
102 312
        return $this->title;
103
    }
104
105
    /**
106
     * Set title.
107
     *
108
     * @param  string  $title
109
     *
110
     * @return self
111
     */
112 552
    public function set($title)
113
    {
114 552
        $this->checkTitle($title);
115 552
        $this->title = $title;
116
117 552
        return $this;
118
    }
119
120
    /**
121
     * Get site name.
122
     *
123
     * @return string
124
     */
125 312
    public function getSiteName()
126
    {
127 312
        return $this->siteName;
128
    }
129
130
    /**
131
     * Set site name.
132
     *
133
     * @param  string  $siteName
134
     *
135
     * @return self
136
     */
137 552
    public function setSiteName($siteName)
138
    {
139 552
        $this->siteName = $siteName;
140
141 552
        return $this;
142
    }
143
144
    /**
145
     * Get title separator.
146
     *
147
     * @return string
148
     */
149 312
    public function getSeparator()
150
    {
151 312
        return $this->separator;
152
    }
153
154
    /**
155
     * Set title separator.
156
     *
157
     * @param  string  $separator
158
     *
159
     * @return self
160
     */
161 552
    public function setSeparator($separator)
162
    {
163 552
        $this->separator = trim($separator);
164
165 552
        return $this;
166
    }
167
168
    /**
169
     * Set title first.
170
     *
171
     * @return self
172
     */
173 24
    public function setFirst()
174
    {
175 24
        return $this->switchPosition(true);
176
    }
177
178
    /**
179
     * Set title last.
180
     *
181
     * @return self
182
     */
183 24
    public function setLast()
184
    {
185 24
        return $this->switchPosition(false);
186
    }
187
188
    /**
189
     * Switch title position.
190
     *
191
     * @param  bool  $first
192
     *
193
     * @return self
194
     */
195 552
    private function switchPosition($first)
196
    {
197 552
        $this->titleFirst = boolval($first);
198
199 552
        return $this;
200
    }
201
202
    /**
203
     * Check if title is first.
204
     *
205
     * @return bool
206
     */
207 312
    public function isTitleFirst()
208
    {
209 312
        return $this->titleFirst;
210
    }
211
212
    /**
213
     * Get title max length.
214
     *
215
     * @return int
216
     */
217 300
    public function getMax()
218
    {
219 300
        return $this->max;
220
    }
221
222
    /**
223
     * Set title max length.
224
     *
225
     * @param  int  $max
226
     *
227
     * @return self
228
     */
229 552
    public function setMax($max)
230
    {
231 552
        $this->checkMax($max);
232
233 552
        $this->max = $max;
234
235 552
        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
     */
251 12
    public static function make($title, $siteName = '', $separator = '-')
252
    {
253 12
        return new self([
254 12
            'default'   => $title,
255 12
            'site-name' => $siteName,
256 12
            'separator' => $separator,
257
            'first'     => true
258 9
        ]);
259
    }
260
261
    /**
262
     * Render the tag.
263
     *
264
     * @return string
265
     */
266 288
    public function render()
267
    {
268 288
        $separator = $this->renderSeparator();
269 288
        $output    = $this->isTitleFirst()
270 288
            ? $this->renderTitleFirst($separator)
271 288
            : $this->renderTitleLast($separator);
272
273 288
        $output    = Str::limit(strip_tags($output), $this->getMax());
274
275 288
        return '<title>' . e($output) . '</title>';
276
    }
277
278
    /**
279
     * Render the separator.
280
     *
281
     * @return string
282
     */
283 288
    protected function renderSeparator()
284
    {
285 288
        return empty($separator = $this->getSeparator()) ? ' ' : " $separator ";
286
    }
287
288
    /**
289
     * Render the tag.
290
     *
291
     * @return string
292
     */
293 48
    public function __toString()
294
    {
295 48
        return $this->render();
296
    }
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)
310
    {
311 552
        if ( ! is_string($title)) {
312 12
            $type = gettype($title);
313
314 12
            throw new InvalidArgumentException(
315 12
                "The title must be a string value, [$type] is given."
316 9
            );
317
        }
318
319 552
        $title = trim($title);
320
321 552
        if (empty($title)) {
322 12
            throw new InvalidArgumentException(
323 3
                'The title is required and must not be empty.'
324 9
            );
325
        }
326 552
    }
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)
336
    {
337 552
        if ( ! is_int($max)) {
338 12
            throw new InvalidArgumentException(
339 3
                'The title maximum lenght must be integer.'
340 9
            );
341
        }
342
343 552
        if ($max <= 0) {
344 12
            throw new InvalidArgumentException(
345 3
                'The title maximum lenght must be greater 0.'
346 9
            );
347
        }
348 552
    }
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)
362
    {
363 288
        $output   = [];
364 288
        $output[] = $this->getTitleOnly();
365
366 288
        if ($this->hasSiteName()) {
367 60
            $output[] = $separator;
368 60
            $output[] = $this->getSiteName();
369 45
        }
370
371 288
        return implode('', $output);
372
    }
373
374
    /**
375
     * Render title last.
376
     *
377
     * @param  string  $separator
378
     *
379
     * @return string
380
     */
381 12
    private function renderTitleLast($separator)
382
    {
383 12
        $output = [];
384
385 12
        if ($this->hasSiteName()) {
386 12
            $output[] = $this->getSiteName();
387 12
            $output[] = $separator;
388 9
        }
389
390 12
        $output[] = $this->getTitleOnly();
391
392 12
        return implode('', $output);
393
    }
394
395
    /**
396
     * Check if site name exists.
397
     *
398
     * @return bool
399
     */
400 288
    private function hasSiteName()
401
    {
402 288
        return ! empty($this->getSiteName());
403
    }
404
}
405