class Gravatar in Avatar Kit 8
Same name in this branch
- 8 avatars_gravatar/src/Gravatar.php \Drupal\avatars_gravatar\Gravatar
- 8 avatars_gravatar/src/Plugin/AvatarGenerator/Gravatar.php \Drupal\avatars_gravatar\Plugin\AvatarGenerator\Gravatar
Implements the Gravatar.com API.
Hierarchy
- class \Drupal\avatars\AvatarBase implements AvatarBaseInterface
- class \Drupal\avatars_gravatar\Gravatar implements GravatarInterface
Expanded class hierarchy of Gravatar
2 files declare their use of Gravatar
- Gravatar.php in avatars_gravatar/
src/ Plugin/ AvatarGenerator/ Gravatar.php - GravatarGenerator.php in avatars_gravatar/
src/ Plugin/ AvatarGenerator/ GravatarGenerator.php
3 string references to 'Gravatar'
- avatars_gravatar.info.yml in avatars_gravatar/
avatars_gravatar.info.yml - avatars_gravatar/avatars_gravatar.info.yml
- avatars_gravatar.schema.yml in avatars_gravatar/
config/ schema/ avatars_gravatar.schema.yml - avatars_gravatar/config/schema/avatars_gravatar.schema.yml
- Gravatar::getTypes in avatars_gravatar/
src/ Gravatar.php - Gets list of avatar types provided by this API.
File
- avatars_gravatar/
src/ Gravatar.php, line 11
Namespace
Drupal\avatars_gravatarView source
class Gravatar extends AvatarBase implements GravatarInterface {
/**
* The type that should be used if the main type is 'gravatar'.
*
* Only if there is no Gravatar for the hash.
*
* @var string|null
*/
protected $fallbackType;
/**
* The URI to an image that should be used if the main type is 'gravatar'.
*
* Only if there is no Gravatar for the hash.
*
* @var string|null
*/
protected $fallbackURI;
/**
* Maximum censorship rating for the image when main type is 'gravatar'.
*
* Endpoint will use fallback image if the gravatar exceeds this rating.
*
* Set to NULL if no rating is required.
*
* @var string|null
*/
protected $rating;
/**
* Constructs a new Gravatar object.
*/
public function __construct() {
$this->fallbackType = '404';
$this
->setDimensionConstraints(GravatarInterface::DIMENSION_MINIMUM_WIDTH, GravatarInterface::DIMENSION_MAXIMUM_WIDTH);
}
/**
* {@inheritdoc}
*/
public function getHostName() {
$hostname = parent::getHostName();
return isset($hostname) ? $hostname : ($this
->isSecure() ? static::GRAVATAR_HOSTNAME_SECURE : static::GRAVATAR_HOSTNAME);
}
/**
* {@inheritdoc}
*/
public static function getTypes() {
return [
'gravatar' => 'Gravatar',
'mysteryman' => 'Mystery Man',
'identicon' => 'Identicon',
'monsterid' => 'Monsterid',
'wavatar' => 'Wavatar',
'retro' => 'Retro',
'blank' => 'Blank',
];
}
/**
* {@inheritdoc}
*/
public static function getTypesMap() {
return [
'gravatar' => 'gravatar',
'mysteryman' => 'mysteryman',
'identicon' => 'identicon',
'monsterid' => 'monsterid',
'wavatar' => 'wavatar',
'retro' => 'retro',
'blank' => 'blank',
'404' => 404,
];
}
/**
* {@inheritdoc}
*/
public static function getFallbackTypes() {
return array_diff(array_keys(Gravatar::getTypesMap()), [
'gravatar',
]);
}
/**
* {@inheritdoc}
*/
public function getFallbackType() {
return $this->fallbackType;
}
/**
* {@inheritdoc}
*/
public function setFallbackType($type = NULL) {
// Fallback type only applies when primary type is set to 'gravatar'.
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;
}
/**
* {@inheritdoc}
*/
public function setFallbackUri($uri) {
$this->fallbackURI = $uri;
$this->fallbackType = NULL;
return $this;
}
/**
* {@inheritdoc}
*/
public static function getRatings() {
return [
'g' => 'G',
'pg' => 'PG',
'r' => 'R',
'x' => 'X',
];
}
/**
* {@inheritdoc}
*/
public function getRating() {
return $this->rating;
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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.');
}
// Gravatar expects lower case email address.
if (!$pre_hashed) {
$identifier = strtolower($identifier);
}
return parent::setIdentifier($identifier, $pre_hashed);
}
/**
* {@inheritdoc}
*/
public static function hashIdentifier($identifier) {
// Gravatar expects a md5 hash, and must have a length <= 32.
// Override in case base class changes.
return md5($identifier);
}
/**
* {@inheritdoc}
*/
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)) {
// Fallback URI is already urlencode'd by http_build_query().
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AvatarBase:: |
protected | property | Maximum height of the avatar. | |
AvatarBase:: |
protected | property | Minimum height of the avatar. | |
AvatarBase:: |
protected | property | Maximum width of the avatar. | |
AvatarBase:: |
protected | property | Minimum width of the avatar. | |
AvatarBase:: |
protected | property | Height. | |
AvatarBase:: |
protected | property | Host name. | |
AvatarBase:: |
protected | property | Identifier. | |
AvatarBase:: |
protected | property | Prehashed. | |
AvatarBase:: |
protected | property | Is Secure. | |
AvatarBase:: |
protected | property | Type. | |
AvatarBase:: |
protected | property | Width. | |
AvatarBase:: |
public | function |
Gets the identifier. Overrides AvatarBaseInterface:: |
|
AvatarBase:: |
public | function |
Gets the avatar type. Overrides AvatarBaseInterface:: |
|
AvatarBase:: |
public | function |
Determines if the set identifier was prehashed. Overrides AvatarBaseInterface:: |
|
AvatarBase:: |
public | function |
Whether the URL will be secure. Overrides AvatarBaseInterface:: |
|
AvatarBase:: |
protected | function | Sets constraints for avatar dimensions. | |
AvatarBase:: |
public | function |
Sets dimensions to get form the endpoint. Overrides AvatarBaseInterface:: |
|
AvatarBase:: |
public | function |
Sets the request host name. Overrides AvatarBaseInterface:: |
|
AvatarBase:: |
public | function |
Sets the request to secure. Overrides AvatarBaseInterface:: |
1 |
AvatarBase:: |
public | function |
Sets the avatar type. Overrides AvatarBaseInterface:: |
|
Gravatar:: |
protected | property | The type that should be used if the main type is 'gravatar'. | |
Gravatar:: |
protected | property | The URI to an image that should be used if the main type is 'gravatar'. | |
Gravatar:: |
protected | property | Maximum censorship rating for the image when main type is 'gravatar'. | |
Gravatar:: |
public | function |
Get the fallback avatar type. Overrides GravatarInterface:: |
|
Gravatar:: |
public static | function |
Valid fallback types for when 'gravatar' is the primary type. Overrides GravatarInterface:: |
|
Gravatar:: |
public | function |
Gets the request host name. Overrides AvatarBase:: |
|
Gravatar:: |
public | function |
Get the rating. Overrides GravatarInterface:: |
|
Gravatar:: |
public static | function |
Get a list of valid ratings. Overrides GravatarInterface:: |
|
Gravatar:: |
public static | function |
Gets list of avatar types provided by this API. Overrides AvatarBaseInterface:: |
|
Gravatar:: |
public static | function |
Avatar types mapped to 'd' GET values. Overrides GravatarInterface:: |
|
Gravatar:: |
public | function |
Gets the URL for the avatar. Overrides AvatarBaseInterface:: |
|
Gravatar:: |
public static | function |
Prepare an identifier for transmission to a third party. Overrides AvatarBase:: |
|
Gravatar:: |
public | function |
The type used for when 'gravatar' type fails. Overrides GravatarInterface:: |
|
Gravatar:: |
public | function |
The URI to an image used for when 'gravatar' type fails. Overrides GravatarInterface:: |
|
Gravatar:: |
public | function |
Sets a unique identifier to be passed to the API. Overrides AvatarBase:: |
|
Gravatar:: |
public | function |
Sets the maximum gravatar rating. Overrides GravatarInterface:: |
|
Gravatar:: |
public | function | Constructs a new Gravatar object. | |
GravatarInterface:: |
constant | |||
GravatarInterface:: |
constant | |||
GravatarInterface:: |
constant | |||
GravatarInterface:: |
constant |