1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Jamosaur\Avtr; |
4
|
|
|
|
5
|
|
|
use Jamosaur\Avtr\Exceptions\InvalidFontException; |
6
|
|
|
use Jamosaur\Avtr\Exceptions\InvalidFormatException; |
7
|
|
|
use Jamosaur\Avtr\Exceptions\InvalidShapeException; |
8
|
|
|
use Jamosaur\Avtr\Exceptions\InvalidTextCaseException; |
9
|
|
|
use Jamosaur\Avtr\Exceptions\InvalidThemeException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Avtr |
13
|
|
|
* |
14
|
|
|
* @package Jamosaur\Avtr |
15
|
|
|
* @author James Wallen-Jones <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Avtr |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* URL For the API. |
21
|
|
|
*/ |
22
|
|
|
const URL = 'https://avtr.io/avtr.'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Config parameters. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $config = [ |
30
|
|
|
'initials' => null, |
31
|
|
|
'first_name' => null, |
32
|
|
|
'last_name' => null, |
33
|
|
|
'email' => null, |
34
|
|
|
'letter_count' => null, |
35
|
|
|
'background' => null, |
36
|
|
|
'size' => null, |
37
|
|
|
'rounded_corners' => null, |
38
|
|
|
'shape' => null, |
39
|
|
|
'theme' => null, |
40
|
|
|
'text_case' => null, |
41
|
|
|
'text_color' => null, |
42
|
|
|
'font_weight' => null, |
43
|
|
|
'font' => null, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Default Format that should be returned. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $format = 'png'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Avtr constructor. |
55
|
|
|
* |
56
|
|
|
* @param string $value |
57
|
|
|
*/ |
58
|
20 |
|
public function __construct(string $value = '') |
59
|
|
|
{ |
60
|
20 |
|
if (strpos($value, '@')) { |
61
|
1 |
|
$this->config['email'] = $value; |
62
|
19 |
|
} elseif (str_word_count($value) > 1) { |
63
|
1 |
|
$this->config['first_name'] = explode(' ', $value)[0]; |
64
|
1 |
|
$this->config['last_name'] = explode(' ', $value)[1]; |
65
|
|
|
} else { |
66
|
18 |
|
$this->config['initials'] = (($value === '') ? null : $value); |
67
|
|
|
} |
68
|
20 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the URL. |
72
|
|
|
* |
73
|
|
|
* @return String |
74
|
|
|
*/ |
75
|
19 |
|
public function toUrl(): String |
76
|
|
|
{ |
77
|
19 |
|
$values = count(array_filter($this->config)); |
78
|
|
|
|
79
|
19 |
|
return self::URL . $this->format . (($values > 0) ? '?' . http_build_query($this->config) : null); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets the format that should be returned. |
84
|
|
|
* |
85
|
|
|
* @param string $format |
86
|
|
|
* |
87
|
|
|
* @return Avtr |
88
|
|
|
* @throws InvalidFormatException |
89
|
|
|
*/ |
90
|
3 |
|
public function format(string $format): Avtr |
91
|
|
|
{ |
92
|
3 |
|
$validFormats = ['png', 'jpg', 'gif']; |
93
|
3 |
|
if (!in_array($format, $validFormats)) { |
94
|
1 |
|
throw new InvalidFormatException; |
95
|
|
|
} |
96
|
2 |
|
$this->format = $format; |
97
|
|
|
|
98
|
2 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Sets the first name. |
103
|
|
|
* |
104
|
|
|
* @param string $firstName |
105
|
|
|
* |
106
|
|
|
* @return Avtr |
107
|
|
|
*/ |
108
|
1 |
|
public function firstName(string $firstName): Avtr |
109
|
|
|
{ |
110
|
1 |
|
$this->config['first_name'] = $firstName; |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sets the last name. |
117
|
|
|
* |
118
|
|
|
* @param string $lastName |
119
|
|
|
* |
120
|
|
|
* @return Avtr |
121
|
|
|
*/ |
122
|
1 |
|
public function lastName(string $lastName): Avtr |
123
|
|
|
{ |
124
|
1 |
|
$this->config['last_name'] = $lastName; |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets the letter count. |
131
|
|
|
* |
132
|
|
|
* @param int $count |
133
|
|
|
* |
134
|
|
|
* @return Avtr |
135
|
|
|
*/ |
136
|
1 |
View Code Duplication |
public function letterCount(int $count): Avtr |
137
|
|
|
{ |
138
|
1 |
|
if ($count < 1 || $count > 2) { |
139
|
1 |
|
$count = 2; |
140
|
|
|
} |
141
|
1 |
|
$this->config['letter_count'] = $count; |
142
|
|
|
|
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets the background colour. |
148
|
|
|
* |
149
|
|
|
* @param int $r |
150
|
|
|
* @param int $g |
151
|
|
|
* @param int $b |
152
|
|
|
* @param float $a |
153
|
|
|
* |
154
|
|
|
* @return Avtr |
155
|
|
|
*/ |
156
|
2 |
|
public function background(int $r, int $g, int $b, float $a = 1): Avtr |
157
|
|
|
{ |
158
|
2 |
|
$this->setColour('background', $r, $g, $b, $a); |
159
|
|
|
|
160
|
2 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Sets the image size. |
165
|
|
|
* |
166
|
|
|
* @param int $size |
167
|
|
|
* |
168
|
|
|
* @return Avtr |
169
|
|
|
*/ |
170
|
2 |
View Code Duplication |
public function size(int $size): Avtr |
171
|
|
|
{ |
172
|
2 |
|
if ($size < 0) { |
173
|
1 |
|
$size = 100; |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
$this->config['size'] = $size; |
177
|
|
|
|
178
|
2 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Sets whether or not the square image should have rounded corners. |
183
|
|
|
* |
184
|
|
|
* @param bool $roundedCorners |
185
|
|
|
* |
186
|
|
|
* @return Avtr |
187
|
|
|
*/ |
188
|
2 |
|
public function roundedCorners(bool $roundedCorners): Avtr |
189
|
|
|
{ |
190
|
2 |
|
$this->config['rounded_corners'] = (int)$roundedCorners; |
191
|
|
|
|
192
|
2 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Sets the image shape. |
197
|
|
|
* |
198
|
|
|
* @param string $shape |
199
|
|
|
* |
200
|
|
|
* @return Avtr |
201
|
|
|
* @throws InvalidShapeException |
202
|
|
|
*/ |
203
|
2 |
|
public function shape(string $shape): Avtr |
204
|
|
|
{ |
205
|
2 |
|
if (!in_array($shape, ['square', 'circle'])) { |
206
|
1 |
|
throw new InvalidShapeException; |
207
|
|
|
} |
208
|
|
|
|
209
|
2 |
|
$this->config['shape'] = $shape; |
210
|
|
|
|
211
|
2 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Sets the theme. |
216
|
|
|
* |
217
|
|
|
* @param string $theme |
218
|
|
|
* |
219
|
|
|
* @return Avtr |
220
|
|
|
* @throws InvalidThemeException |
221
|
|
|
*/ |
222
|
2 |
|
public function theme(string $theme): Avtr |
223
|
|
|
{ |
224
|
2 |
|
if (!in_array($theme, ['material', 'flat'])) { |
225
|
1 |
|
throw new InvalidThemeException; |
226
|
|
|
} |
227
|
|
|
|
228
|
2 |
|
$this->config['theme'] = $theme; |
229
|
|
|
|
230
|
2 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Sets the text case. |
235
|
|
|
* |
236
|
|
|
* @param string $textCase |
237
|
|
|
* |
238
|
|
|
* @return Avtr |
239
|
|
|
* @throws InvalidTextCaseException |
240
|
|
|
*/ |
241
|
2 |
|
public function textCase(string $textCase): Avtr |
242
|
|
|
{ |
243
|
2 |
|
if (!in_array($textCase, ['lower', 'upper', 'title'])) { |
244
|
1 |
|
throw new InvalidTextCaseException; |
245
|
|
|
} |
246
|
|
|
|
247
|
2 |
|
$this->config['text_case'] = $textCase; |
248
|
|
|
|
249
|
2 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Sets the text colour. |
254
|
|
|
* |
255
|
|
|
* @param int $r |
256
|
|
|
* @param int $g |
257
|
|
|
* @param int $b |
258
|
|
|
* @param float $a |
259
|
|
|
* |
260
|
|
|
* @return Avtr |
261
|
|
|
*/ |
262
|
2 |
|
public function color(int $r, int $g, int $b, float $a = 1): Avtr |
263
|
|
|
{ |
264
|
2 |
|
$this->setColour('text_color', $r, $g, $b, $a); |
265
|
|
|
|
266
|
2 |
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Sets the font weight. |
271
|
|
|
* |
272
|
|
|
* @param int $fontWeight |
273
|
|
|
* |
274
|
|
|
* @return Avtr |
275
|
|
|
*/ |
276
|
2 |
|
public function fontWeight(int $fontWeight): Avtr |
277
|
|
|
{ |
278
|
2 |
|
$fontWeight = min(max($fontWeight, 100), 900); |
279
|
2 |
|
$this->config['font_weight'] = $fontWeight; |
280
|
|
|
|
281
|
2 |
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Sets the font. |
286
|
|
|
* |
287
|
|
|
* @param string $font |
288
|
|
|
* |
289
|
|
|
* @return Avtr |
290
|
|
|
* @throws InvalidFontException |
291
|
|
|
*/ |
292
|
2 |
|
public function font(string $font): Avtr |
293
|
|
|
{ |
294
|
2 |
|
$fonts = ['open-sans', 'source-sans-pro', 'roboto']; |
295
|
|
|
|
296
|
2 |
|
if (!in_array($font, $fonts)) { |
297
|
1 |
|
throw new InvalidFontException; |
298
|
|
|
} |
299
|
|
|
|
300
|
2 |
|
$this->config['font'] = $font; |
301
|
|
|
|
302
|
2 |
|
return $this; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Validates and sets the appropriate config value for colours. |
307
|
|
|
* |
308
|
|
|
* @param string $where |
309
|
|
|
* @param int $r |
310
|
|
|
* @param int $g |
311
|
|
|
* @param int $b |
312
|
|
|
* @param float $a |
313
|
|
|
*/ |
314
|
3 |
|
private function setColour(string $where, int $r, int $g, int $b, float $a = 1) |
315
|
|
|
{ |
316
|
3 |
|
$r = min(max($r, 0), 255); |
317
|
3 |
|
$g = min(max($g, 0), 255); |
318
|
3 |
|
$b = min(max($b, 0), 255); |
319
|
3 |
|
$a = min(max($a, 0), 1); |
320
|
|
|
|
321
|
3 |
|
$this->config[$where] = 'rgba(' . $r . ',' . $g . ',' . $b . ',' . $a . ')'; |
322
|
3 |
|
} |
323
|
|
|
} |
324
|
|
|
|