3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper
;
12 use Zend\View\Exception
;
15 * Helper for retrieving avatars from gravatar.com
17 class Gravatar
extends AbstractHtmlElement
20 * URL to gravatar service
22 const GRAVATAR_URL
= 'http://www.gravatar.com/avatar';
24 * Secure URL to gravatar service
26 const GRAVATAR_URL_SECURE
= 'https://secure.gravatar.com/avatar';
32 const RATING_PG
= 'pg';
37 * Default gravatar image value constants
39 const DEFAULT_404
= '404';
40 const DEFAULT_MM
= 'mm';
41 const DEFAULT_IDENTICON
= 'identicon';
42 const DEFAULT_MONSTERID
= 'monsterid';
43 const DEFAULT_WAVATAR
= 'wavatar';
46 * Attributes for HTML image tag
60 * True or false if the email address passed is already an MD5 hash
64 protected $emailIsHashed;
71 protected $options = array(
73 'default_img' => self
::DEFAULT_MM
,
74 'rating' => self
::RATING_G
,
79 * Returns an avatar from gravatar's service.
81 * $options may include the following:
82 * - 'img_size' int height of img to return
83 * - 'default_img' string img to return if email address has not found
84 * - 'rating' string rating parameter for avatar
85 * - 'secure' bool load from the SSL or Non-SSL location
87 * @see http://pl.gravatar.com/site/implement/url
88 * @see http://pl.gravatar.com/site/implement/url More information about gravatar's service.
89 * @param string|null $email Email address.
90 * @param null|array $options Options
91 * @param array $attribs Attributes for image tag (title, alt etc.)
94 public function __invoke($email = "", $options = array(), $attribs = array())
97 $this->setEmail($email);
99 if (!empty($options)) {
100 $this->setOptions($options);
102 if (!empty($attribs)) {
103 $this->setAttribs($attribs);
110 * Return valid image tag
114 public function __toString()
116 return $this->getImgTag();
122 * @param array $options
125 public function setOptions(array $options)
127 foreach ($options as $key => $value) {
128 $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
129 if (method_exists($this, $method)) {
130 $this->{$method}($value);
138 * Get avatar url (including size, rating and default image options)
142 protected function getAvatarUrl()
144 $src = $this->getGravatarUrl()
145 . '/' . ($this->emailIsHashed ?
$this->getEmail() : md5($this->getEmail()))
146 . '?s=' . $this->getImgSize()
147 . '&d=' . $this->getDefaultImg()
148 . '&r=' . $this->getRating();
153 * Get URL to gravatar's service.
157 protected function getGravatarUrl()
159 return ($this->getSecure() === false) ? self
::GRAVATAR_URL
: self
::GRAVATAR_URL_SECURE
;
163 * Return valid image tag
167 public function getImgTag()
169 $this->setSrcAttribForImg();
171 . $this->htmlAttribs($this->getAttribs())
172 . $this->getClosingBracket();
178 * Set attribs for image tag
180 * Warning! You shouldn't set src attrib for image tag.
181 * This attrib is overwritten in protected method setSrcAttribForImg().
182 * This method(_setSrcAttribForImg) is called in public method getImgTag().
184 * @param array $attribs
187 public function setAttribs(array $attribs)
189 $this->attribs
= $attribs;
194 * Get attribs of image
197 * If you set src attrib, you get it, but this value will be overwritten in
198 * protected method setSrcAttribForImg(). And finally your get other src
203 public function getAttribs()
205 return $this->attribs
;
211 * Can be either an absolute URL to an image, or one of the DEFAULT_* constants
213 * @link http://pl.gravatar.com/site/implement/url More information about default image.
214 * @param string $defaultImg
217 public function setDefaultImg($defaultImg)
219 $this->options
['default_img'] = urlencode($defaultImg);
228 public function getDefaultImg()
230 return $this->options
['default_img'];
236 * @param string $email
239 public function setEmail($email)
241 $this->emailIsHashed
= (bool) preg_match('/^[A-Za-z0-9]{32}$/', $email);
242 $this->email
= strtolower(trim($email));
251 public function getEmail()
257 * Set img size in pixels
259 * @param int $imgSize Size of img must be between 1 and 512
262 public function setImgSize($imgSize)
264 $this->options
['img_size'] = (int) $imgSize;
271 * @return int The img size
273 public function getImgSize()
275 return $this->options
['img_size'];
281 * Must be one of the RATING_* constants
283 * @link http://pl.gravatar.com/site/implement/url More information about rating.
284 * @param string $rating Value for rating. Allowed values are: g, px, r,x
286 * @throws Exception\DomainException
288 public function setRating($rating)
292 case self
::RATING_PG
:
295 $this->options
['rating'] = $rating;
298 throw new Exception\
DomainException(sprintf(
299 'The rating value "%s" is not allowed',
312 public function getRating()
314 return $this->options
['rating'];
318 * Load from an SSL or No-SSL location?
323 public function setSecure($flag)
325 $this->options
['secure'] = ($flag === null) ?
null : (bool) $flag;
330 * Get an SSL or a No-SSL location
334 public function getSecure()
336 if ($this->options
['secure'] === null) {
337 return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
340 return $this->options
['secure'];
344 * Set src attrib for image.
346 * You shouldn't set an own url value!
347 * It sets value, uses protected method getAvatarUrl.
349 * If already exists, it will be overwritten.
353 protected function setSrcAttribForImg()
355 $attribs = $this->getAttribs();
356 $attribs['src'] = $this->getAvatarUrl();
357 $this->setAttribs($attribs);