1 | <?php namespace Arcanedev\Gravatar; |
||
13 | class Gravatar implements Contracts\Gravatar |
||
14 | { |
||
15 | /* ----------------------------------------------------------------- |
||
16 | | Traits |
||
17 | | ----------------------------------------------------------------- |
||
18 | */ |
||
19 | |||
20 | use Concerns\HashEmail; |
||
21 | |||
22 | /* ----------------------------------------------------------------- |
||
23 | | Constants |
||
24 | | ----------------------------------------------------------------- |
||
25 | */ |
||
26 | |||
27 | const BASE_URL = 'http://www.gravatar.com/avatar/'; |
||
28 | const SECURE_URL = 'https://secure.gravatar.com/avatar/'; |
||
29 | |||
30 | /* ----------------------------------------------------------------- |
||
31 | | Properties |
||
32 | | ----------------------------------------------------------------- |
||
33 | */ |
||
34 | |||
35 | /** |
||
36 | * The default image to use (gravatar-recognized type, url or false = default gravatar image) |
||
37 | * |
||
38 | * @var mixed |
||
39 | */ |
||
40 | protected $defaultImage = false; |
||
41 | |||
42 | /** |
||
43 | * The maximum rating to allow for the avatar. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $rating = 'g'; |
||
48 | |||
49 | /** |
||
50 | * The avatar size. |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $size = 80; |
||
55 | |||
56 | /** |
||
57 | * Should we use the secure (HTTPS) URL base ? |
||
58 | * |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $secure = false; |
||
62 | |||
63 | /** |
||
64 | * A temporary internal cache of the URL parameters. |
||
65 | * |
||
66 | * @var array|null |
||
67 | */ |
||
68 | protected $cachedParams = null; |
||
69 | |||
70 | /** |
||
71 | * Supported image defaults. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | private $supportedImages = ['404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro']; |
||
76 | |||
77 | /** |
||
78 | * Supported image ratings. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | private $supportedRatings = ['g', 'pg', 'r', 'x']; |
||
83 | |||
84 | /* ----------------------------------------------------------------- |
||
85 | | Constructor |
||
86 | | ----------------------------------------------------------------- |
||
87 | */ |
||
88 | |||
89 | /** |
||
90 | * Make a gravatar instance. |
||
91 | * |
||
92 | * @param string $default |
||
93 | * @param int $size |
||
94 | * @param string $rating |
||
95 | */ |
||
96 | 68 | public function __construct($default = 'mm', $size = 80, $rating = 'g') |
|
103 | |||
104 | /* ----------------------------------------------------------------- |
||
105 | | Getters & Setters |
||
106 | | ----------------------------------------------------------------- |
||
107 | */ |
||
108 | |||
109 | /** |
||
110 | * Get the current default image setting. |
||
111 | * |
||
112 | * @return string|false |
||
113 | */ |
||
114 | 32 | public function getDefaultImage() |
|
118 | |||
119 | /** |
||
120 | * Set the default image to use for avatars. |
||
121 | * |
||
122 | * @param string|false $image |
||
123 | * |
||
124 | * @return \Arcanedev\Gravatar\Gravatar |
||
125 | * |
||
126 | * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageUrlException |
||
127 | */ |
||
128 | 68 | public function setDefaultImage($image) |
|
142 | |||
143 | /** |
||
144 | * Get the currently set avatar size. |
||
145 | * |
||
146 | * @return int |
||
147 | */ |
||
148 | 40 | public function getSize() |
|
152 | |||
153 | /** |
||
154 | * Set the avatar size to use. |
||
155 | * |
||
156 | * @param integer $size - The avatar size to use, must be less than 512 and greater than 0. |
||
157 | * |
||
158 | * @return \Arcanedev\Gravatar\Gravatar |
||
159 | * |
||
160 | * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageSizeException |
||
161 | */ |
||
162 | 68 | public function setSize($size) |
|
172 | |||
173 | /** |
||
174 | * Get the current maximum allowed rating for avatars. |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | 36 | public function getRating() |
|
182 | |||
183 | /** |
||
184 | * Set the maximum allowed rating for avatars. |
||
185 | * |
||
186 | * @param string $rating |
||
187 | * |
||
188 | * @return \Arcanedev\Gravatar\Gravatar |
||
189 | * |
||
190 | * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageRatingException |
||
191 | */ |
||
192 | 68 | public function setRating($rating) |
|
205 | |||
206 | /** |
||
207 | * Check if we are using the secure protocol for the image URLs. |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | 40 | public function isSecured() |
|
215 | |||
216 | /* ----------------------------------------------------------------- |
||
217 | | Main Methods |
||
218 | | ----------------------------------------------------------------- |
||
219 | */ |
||
220 | |||
221 | /** |
||
222 | * Get Gravatar image source. |
||
223 | * |
||
224 | * @param string $email |
||
225 | * @param int|null $size |
||
226 | * @param string|null $rating |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | 12 | public function src($email, $size = null, $rating = null) |
|
246 | |||
247 | /** |
||
248 | * Get the avatar URL based on the provided email address. |
||
249 | * |
||
250 | * @param string $email |
||
251 | * @param bool $hash |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | 32 | public function get($email, $hash = true) |
|
266 | |||
267 | /** |
||
268 | * Get Gravatar image tag. |
||
269 | * |
||
270 | * @param string $email |
||
271 | * @param string|null $alt |
||
272 | * @param array $attributes |
||
273 | * @param string|null $rating |
||
274 | * |
||
275 | * @return \Arcanedev\Html\Elements\Img |
||
276 | */ |
||
277 | 8 | public function image($email, $alt = null, array $attributes = [], $rating = null) |
|
292 | |||
293 | /** |
||
294 | * Get profile's data. |
||
295 | * |
||
296 | * @param string $email |
||
297 | * @param mixed|null $default |
||
298 | * |
||
299 | * @return array|mixed |
||
300 | */ |
||
301 | 4 | public function profile($email, $default = null) |
|
305 | |||
306 | /** |
||
307 | * Enable the use of the secure protocol for image URLs. |
||
308 | * |
||
309 | * @return \Arcanedev\Gravatar\Gravatar |
||
310 | */ |
||
311 | 68 | public function enableSecure() |
|
317 | |||
318 | /** |
||
319 | * Disable the use of the secure protocol for image URLs. |
||
320 | * |
||
321 | * @return \Arcanedev\Gravatar\Gravatar |
||
322 | */ |
||
323 | 4 | public function disableSecure() |
|
329 | |||
330 | /** |
||
331 | * Check if email has a gravatar. |
||
332 | * |
||
333 | * @param string $email |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | 4 | public function exists($email) |
|
345 | |||
346 | /* ----------------------------------------------------------------- |
||
347 | | Check Methods |
||
348 | | ----------------------------------------------------------------- |
||
349 | */ |
||
350 | |||
351 | /** |
||
352 | * Check image url. |
||
353 | * |
||
354 | * @param string $image |
||
355 | * |
||
356 | * @return string |
||
357 | * |
||
358 | * @throws \Arcanedev\Gravatar\Exceptions\InvalidImageUrlException |
||
359 | */ |
||
360 | 8 | private function checkImageUrl($image) |
|
368 | |||
369 | /* ----------------------------------------------------------------- |
||
370 | | Other Methods |
||
371 | | ----------------------------------------------------------------- |
||
372 | */ |
||
373 | |||
374 | /** |
||
375 | * Get params. |
||
376 | * |
||
377 | * @param string $email |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | 32 | private function getParams($email) |
|
402 | |||
403 | /** |
||
404 | * Check the image size. |
||
405 | * |
||
406 | * @param int $size |
||
407 | */ |
||
408 | 68 | private function checkSize(&$size) |
|
424 | } |
||
425 |