1 | <?php |
||
17 | class Gravatar implements GravatarContract |
||
18 | { |
||
19 | /* ----------------------------------------------------------------- |
||
20 | | Traits |
||
21 | | ----------------------------------------------------------------- |
||
22 | */ |
||
23 | |||
24 | 1 | use Concerns\HashEmail; |
|
25 | |||
26 | /* ----------------------------------------------------------------- |
||
27 | | Constants |
||
28 | | ----------------------------------------------------------------- |
||
29 | */ |
||
30 | |||
31 | const BASE_URL = 'http://www.gravatar.com/avatar/'; |
||
32 | const SECURE_URL = 'https://secure.gravatar.com/avatar/'; |
||
33 | |||
34 | /* ----------------------------------------------------------------- |
||
35 | | Properties |
||
36 | | ----------------------------------------------------------------- |
||
37 | */ |
||
38 | |||
39 | /** |
||
40 | * The default image to use (gravatar-recognized type, url or false = default gravatar image) |
||
41 | * |
||
42 | * @var mixed |
||
43 | */ |
||
44 | protected $defaultImage = false; |
||
45 | |||
46 | /** |
||
47 | * The maximum rating to allow for the avatar. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $rating = 'g'; |
||
52 | |||
53 | /** |
||
54 | * The avatar size. |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | protected $size = 80; |
||
59 | |||
60 | /** |
||
61 | * Should we use the secure (HTTPS) URL base ? |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $secure = false; |
||
66 | |||
67 | /** |
||
68 | * A temporary internal cache of the URL parameters. |
||
69 | * |
||
70 | * @var array|null |
||
71 | */ |
||
72 | protected $cachedParams = null; |
||
73 | |||
74 | /** |
||
75 | * Supported image defaults. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | private $supportedImages = [ |
||
80 | '404', 'mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank', |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * Supported image ratings. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | private $supportedRatings = [ |
||
89 | 'g', 'pg', 'r', 'x' |
||
90 | ]; |
||
91 | |||
92 | /* ----------------------------------------------------------------- |
||
93 | | Constructor |
||
94 | | ----------------------------------------------------------------- |
||
95 | */ |
||
96 | |||
97 | /** |
||
98 | * Make a gravatar instance. |
||
99 | * |
||
100 | * @param string $default |
||
101 | * @param int $size |
||
102 | * @param string $rating |
||
103 | */ |
||
104 | 76 | public function __construct($default = 'identicon', $size = 80, $rating = 'g') |
|
111 | |||
112 | /* ----------------------------------------------------------------- |
||
113 | | Getters & Setters |
||
114 | | ----------------------------------------------------------------- |
||
115 | */ |
||
116 | |||
117 | /** |
||
118 | * Get the current default image setting. |
||
119 | * |
||
120 | * @return string|false |
||
121 | */ |
||
122 | 36 | public function getDefaultImage() |
|
126 | |||
127 | /** |
||
128 | * Set the default image to use for avatars. |
||
129 | * |
||
130 | * @param string|false $image |
||
131 | * |
||
132 | * @return $this |
||
133 | * |
||
134 | * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageUrlException |
||
135 | */ |
||
136 | 76 | public function setDefaultImage($image) |
|
150 | |||
151 | /** |
||
152 | * Get the currently set avatar size. |
||
153 | * |
||
154 | * @return int |
||
155 | */ |
||
156 | 40 | public function getSize() |
|
160 | |||
161 | /** |
||
162 | * Set the avatar size to use. |
||
163 | * |
||
164 | * @param int $size - The avatar size to use, must be less than 512 and greater than 0. |
||
165 | * |
||
166 | * @return $this |
||
167 | * |
||
168 | * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageSizeException |
||
169 | */ |
||
170 | 76 | public function setSize($size) |
|
180 | |||
181 | /** |
||
182 | * Get the current maximum allowed rating for avatars. |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 36 | public function getRating() |
|
190 | |||
191 | /** |
||
192 | * Set the maximum allowed rating for avatars. |
||
193 | * |
||
194 | * @param string $rating |
||
195 | * |
||
196 | * @return $this |
||
197 | * |
||
198 | * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageRatingException |
||
199 | */ |
||
200 | 76 | public function setRating($rating) |
|
213 | |||
214 | /** |
||
215 | * Check if we are using the secure protocol for the image URLs. |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | 40 | public function isSecured(): bool |
|
223 | |||
224 | /* ----------------------------------------------------------------- |
||
225 | | Main Methods |
||
226 | | ----------------------------------------------------------------- |
||
227 | */ |
||
228 | |||
229 | /** |
||
230 | * Get Gravatar image source. |
||
231 | * |
||
232 | * @param string $email |
||
233 | * @param int|null $size |
||
234 | * @param string|null $rating |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 12 | public function src($email, $size = null, $rating = null): string |
|
254 | |||
255 | /** |
||
256 | * Get the avatar URL based on the provided email address. |
||
257 | * |
||
258 | * @param string $email |
||
259 | * @param bool $hash |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | 32 | public function get($email, $hash = true): string |
|
274 | |||
275 | /** |
||
276 | * Get Gravatar image tag. |
||
277 | * |
||
278 | * @param string $email |
||
279 | * @param string|null $alt |
||
280 | * @param array $attributes |
||
281 | * @param string|null $rating |
||
282 | * |
||
283 | * @return \Arcanedev\Html\Elements\Img |
||
284 | */ |
||
285 | 8 | public function image($email, $alt = null, array $attributes = [], $rating = null) |
|
300 | |||
301 | /** |
||
302 | * Get profile's data. |
||
303 | * |
||
304 | * @param string $email |
||
305 | * @param mixed|null $default |
||
306 | * |
||
307 | * @return array|mixed |
||
308 | */ |
||
309 | 4 | public function profile($email, $default = null) |
|
313 | |||
314 | /** |
||
315 | * Enable the use of the secure protocol for image URLs. |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | 76 | public function enableSecure() |
|
325 | |||
326 | /** |
||
327 | * Disable the use of the secure protocol for image URLs. |
||
328 | * |
||
329 | * @return $this |
||
330 | */ |
||
331 | 4 | public function disableSecure() |
|
337 | |||
338 | /** |
||
339 | * Check if email has a gravatar. |
||
340 | * |
||
341 | * @param string $email |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | 4 | public function exists($email): bool |
|
353 | |||
354 | /* ----------------------------------------------------------------- |
||
355 | | Other Methods |
||
356 | | ----------------------------------------------------------------- |
||
357 | */ |
||
358 | |||
359 | /** |
||
360 | * Get params. |
||
361 | * |
||
362 | * @param string $email |
||
363 | * |
||
364 | * @return array |
||
365 | */ |
||
366 | 32 | private function getParams(string $email): array |
|
387 | |||
388 | /** |
||
389 | * Check image url. |
||
390 | * |
||
391 | * @param string $image |
||
392 | * |
||
393 | * @return string |
||
394 | * |
||
395 | * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageUrlException |
||
396 | */ |
||
397 | 12 | private function checkImageUrl(string $image): string |
|
405 | |||
406 | /** |
||
407 | * Check the image size. |
||
408 | * |
||
409 | * @param int $size |
||
410 | */ |
||
411 | 76 | private function checkSize(&$size): void |
|
427 | } |
||
428 |