View source
<?php
namespace Drupal\avatars_gravatar;
use Drupal\avatars\AvatarBase;
use Drupal\avatars\Exception\AvatarException;
class Gravatar extends AvatarBase implements GravatarInterface {
protected $fallbackType;
protected $fallbackURI;
protected $rating;
public function __construct() {
$this->fallbackType = '404';
$this
->setDimensionConstraints(GravatarInterface::DIMENSION_MINIMUM_WIDTH, GravatarInterface::DIMENSION_MAXIMUM_WIDTH);
}
public function getHostName() {
$hostname = parent::getHostName();
return isset($hostname) ? $hostname : ($this
->isSecure() ? static::GRAVATAR_HOSTNAME_SECURE : static::GRAVATAR_HOSTNAME);
}
public static function getTypes() {
return [
'gravatar' => 'Gravatar',
'mysteryman' => 'Mystery Man',
'identicon' => 'Identicon',
'monsterid' => 'Monsterid',
'wavatar' => 'Wavatar',
'retro' => 'Retro',
'blank' => 'Blank',
];
}
public static function getTypesMap() {
return [
'gravatar' => 'gravatar',
'mysteryman' => 'mysteryman',
'identicon' => 'identicon',
'monsterid' => 'monsterid',
'wavatar' => 'wavatar',
'retro' => 'retro',
'blank' => 'blank',
'404' => 404,
];
}
public static function getFallbackTypes() {
return array_diff(array_keys(Gravatar::getTypesMap()), [
'gravatar',
]);
}
public function getFallbackType() {
return $this->fallbackType;
}
public function setFallbackType($type = NULL) {
if (!in_array($type, $this
->getFallbackTypes())) {
throw new AvatarException(sprintf('%s is an invalid fallback type', $type));
}
$this->fallbackType = $type;
$this->fallbackURI = NULL;
return $this;
}
public function setFallbackUri($uri) {
$this->fallbackURI = $uri;
$this->fallbackType = NULL;
return $this;
}
public static function getRatings() {
return [
'g' => 'G',
'pg' => 'PG',
'r' => 'R',
'x' => 'X',
];
}
public function getRating() {
return $this->rating;
}
public function setRating($rating = NULL) {
if (isset($rating) && !array_key_exists($rating, $this
->getRatings())) {
throw new AvatarException(sprintf('%s is an invalid rating', $rating));
}
$this->rating = $rating;
return $this;
}
public function setIdentifier($identifier, $pre_hashed = FALSE) {
if ($pre_hashed && strlen($identifier) > 32) {
throw new AvatarException('API does not generate unique avatars after 32nd character.');
}
if (!$pre_hashed) {
$identifier = strtolower($identifier);
}
return parent::setIdentifier($identifier, $pre_hashed);
}
public static function hashIdentifier($identifier) {
return md5($identifier);
}
public function getUrl() {
$kv = [];
$url = ($this
->isSecure() ? 'https://' : 'http://') . $this
->getHostName() . '/avatar/';
$identifier = $this
->getIdentifier();
if (!strlen($identifier)) {
throw new AvatarException('Missing avatar identifier/hash');
}
$url .= $this
->identifierIsPreHashed() ? $identifier : $this
->hashIdentifier($identifier);
$type = $this
->getType();
if (!in_array($type, $this
->getFallbackTypes())) {
if (isset($this->fallbackType)) {
$kv['d'] = $this->fallbackType;
}
elseif (isset($this->fallbackURI)) {
$kv['d'] = $this->fallbackURI;
}
}
else {
$type_map = $this
->getTypesMap();
if (!empty($type)) {
$kv['d'] = $type_map[$type];
$kv['f'] = 'y';
}
}
if (is_numeric($this->width)) {
$kv['s'] = $this->width;
}
if (isset($this->rating)) {
$kv['r'] = $this->rating;
}
$query = http_build_query($kv);
return !empty($query) ? $url . '?' . $query : $url;
}
}