1 | <?php |
||
2 | |||
3 | namespace Maestriam\Samurai\Entities; |
||
4 | |||
5 | use Maestriam\Samurai\Exceptions\InvalidAuthorException; |
||
6 | |||
7 | class Author extends Foundation |
||
8 | { |
||
9 | /** |
||
10 | * Nome do autor do tema |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | private string $name; |
||
15 | |||
16 | /** |
||
17 | * E-mail do autor do tema |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | private string $email; |
||
22 | |||
23 | /** |
||
24 | * Instância com as regras de negócios sobre o autor do tema. |
||
25 | * Na ausência de informações sobre o autor, irá pegar as informações |
||
26 | * definida nas configurações do projeto como padrão. |
||
27 | * |
||
28 | * @param string $author |
||
29 | */ |
||
30 | public function __construct(string $author = null) |
||
31 | { |
||
32 | ($author) ? $this->set($author) : $this->default(); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Retorna/Define o nome do autor do tema. |
||
37 | * Se passar uma string como parâmetro, assume a função de definição. |
||
38 | * Informações utilizados dentro arquivo composer.json. |
||
39 | * |
||
40 | * @param string $name |
||
41 | * @return Author|string |
||
42 | */ |
||
43 | public function name(string $name = null) : Author|string |
||
44 | { |
||
45 | if (! $name) { |
||
46 | return $this->getName(); |
||
47 | } |
||
48 | |||
49 | return $this->setName($name); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Retorna/Define o e-mail do autor do tema. |
||
54 | * Se passar uma string como parâmetro, assume a função de definição. |
||
55 | * Informações utilizados dentro arquivo composer.json |
||
56 | * |
||
57 | * @param string $email |
||
58 | * @return Author|string |
||
59 | */ |
||
60 | public function email(string $email = null) : Author|string |
||
61 | { |
||
62 | if (! $email) { |
||
63 | return $this->getEmail(); |
||
64 | } |
||
65 | |||
66 | return $this->setEmail($email); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Retorna a assinatura do autor. |
||
71 | * Ex: Joe Doe <[email protected]> |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function signature() : string |
||
76 | { |
||
77 | return sprintf("%s <%s>", $this->name(), $this->email()); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Define o nome e o autor do tema, de acordo com o padrão: |
||
82 | * Ex: Giu Sampaio <<[email protected]>> |
||
83 | * |
||
84 | * @param string $author |
||
85 | * @return Author |
||
86 | * @throws InvalidAuthorException |
||
87 | */ |
||
88 | public function set(string $author) : Author |
||
89 | { |
||
90 | if (! $this->valid()->author($author)) { |
||
91 | throw new InvalidAuthorException($author); |
||
92 | } |
||
93 | |||
94 | $author = $this->parser()->author($author); |
||
95 | |||
96 | return $this->load($author); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Carrega o nome, distribuidora e o e-mail do autor de um objeto específico |
||
101 | * |
||
102 | * @param object $author |
||
103 | * @return Author |
||
104 | */ |
||
105 | private function load(object $author) : Author |
||
106 | { |
||
107 | return $this->name($author->name)->email($author->email); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Define o nome do autor do tema |
||
112 | * Usado no arquivo composer.json |
||
113 | * |
||
114 | * @param string $name |
||
115 | * @return Author |
||
116 | */ |
||
117 | private function setName(string $name) : Author |
||
118 | { |
||
119 | $this->name = $name; |
||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Retorna o nome do autor do tema |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | private function getName() : string |
||
129 | { |
||
130 | return $this->name ?? $this->config()->author()->name; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Define o email do autor do tema |
||
135 | * |
||
136 | * @param string $email |
||
137 | * @return Author |
||
138 | */ |
||
139 | private function setEmail(string $email) : Author |
||
140 | { |
||
141 | $this->email = $email; |
||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Retorna o e-mail do autor do tema |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | private function getEmail() : string |
||
151 | { |
||
152 | return $this->email ?? $this->config()->author()->email; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Retorna as informações padrões do autor, definido no config |
||
157 | * |
||
158 | * @return Author |
||
159 | */ |
||
160 | private function default() : Author |
||
161 | { |
||
162 | $default = $this->config()->author(); |
||
163 | |||
164 | return $this->load($default); |
||
165 | } |
||
166 | } |
||
167 |