1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Common\Utils; |
4
|
|
|
|
5
|
|
|
use Win\Common\Utils\Str; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Auxilia a criar o título otimizado para SEO |
9
|
|
|
*/ |
10
|
|
|
class Seo |
11
|
|
|
{ |
12
|
|
|
public static $titlePrefix = ''; |
13
|
|
|
public static $titleSuffix = ''; |
14
|
|
|
public static $keywords = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Descrição padrão. |
18
|
|
|
* Usadas quando a descrição informada não tenha o tamanho suficiente |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public static $description = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Retorna o título com o nome da aplicação no final |
25
|
|
|
* Mantendo o máximo de caracteres |
26
|
|
|
* @param string $title |
27
|
|
|
* @param string $maxLength |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public static function title($title, $maxLength = 70) |
31
|
|
|
{ |
32
|
|
|
$staticLength = Str::length(static::$titlePrefix) + Str::length(static::$titleSuffix); |
33
|
|
|
$maxLength = $maxLength - $staticLength; |
34
|
|
|
|
35
|
|
|
return static::$titlePrefix . Str::truncate($title, $maxLength) . static::$titleSuffix; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/* Chaves padrão. |
39
|
|
|
* Usadas quando as chaves informadas não tenham o tamanho suficiente |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Retorna uma string em minúscula, separada por virgula |
45
|
|
|
* @param string[] $keys |
46
|
|
|
* @param int $maxLength |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public static function keywords($keys, $maxLength = 100) |
50
|
|
|
{ |
51
|
|
|
$keys = array_merge($keys, static::$keywords); |
52
|
|
|
$keys = Str::truncate(implode(', ', array_filter($keys)), $maxLength); |
53
|
|
|
|
54
|
|
|
return Str::lower(str_replace([',...', '...'], '', $keys)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retorna a 'description' com tamanho ideal |
59
|
|
|
* @param string $description |
60
|
|
|
* @param int $maxLength |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public static function description($description, $maxLength = 150) |
64
|
|
|
{ |
65
|
|
|
if (Str::length($description) > 0) { |
66
|
|
|
return Str::truncate($description, $maxLength); |
67
|
|
|
} else { |
68
|
|
|
return static::$description; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|