You are here

class Gravatar in Avatar Kit 8

Same name in this branch
  1. 8 avatars_gravatar/src/Gravatar.php \Drupal\avatars_gravatar\Gravatar
  2. 8 avatars_gravatar/src/Plugin/AvatarGenerator/Gravatar.php \Drupal\avatars_gravatar\Plugin\AvatarGenerator\Gravatar

Implements the Gravatar.com API.

Hierarchy

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_gravatar
View 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

Namesort descending Modifiers Type Description Overrides
AvatarBase::$dimension_height_maximum protected property Maximum height of the avatar.
AvatarBase::$dimension_height_minimum protected property Minimum height of the avatar.
AvatarBase::$dimension_width_maximum protected property Maximum width of the avatar.
AvatarBase::$dimension_width_minimum protected property Minimum width of the avatar.
AvatarBase::$height protected property Height.
AvatarBase::$hostname protected property Host name.
AvatarBase::$identifier protected property Identifier.
AvatarBase::$prehashed protected property Prehashed.
AvatarBase::$secure protected property Is Secure.
AvatarBase::$type protected property Type.
AvatarBase::$width protected property Width.
AvatarBase::getIdentifier public function Gets the identifier. Overrides AvatarBaseInterface::getIdentifier
AvatarBase::getType public function Gets the avatar type. Overrides AvatarBaseInterface::getType
AvatarBase::identifierIsPreHashed public function Determines if the set identifier was prehashed. Overrides AvatarBaseInterface::identifierIsPreHashed
AvatarBase::isSecure public function Whether the URL will be secure. Overrides AvatarBaseInterface::isSecure
AvatarBase::setDimensionConstraints protected function Sets constraints for avatar dimensions.
AvatarBase::setDimensions public function Sets dimensions to get form the endpoint. Overrides AvatarBaseInterface::setDimensions
AvatarBase::setHostName public function Sets the request host name. Overrides AvatarBaseInterface::setHostName
AvatarBase::setIsSecure public function Sets the request to secure. Overrides AvatarBaseInterface::setIsSecure 1
AvatarBase::setType public function Sets the avatar type. Overrides AvatarBaseInterface::setType
Gravatar::$fallbackType protected property The type that should be used if the main type is 'gravatar'.
Gravatar::$fallbackURI protected property The URI to an image that should be used if the main type is 'gravatar'.
Gravatar::$rating protected property Maximum censorship rating for the image when main type is 'gravatar'.
Gravatar::getFallbackType public function Get the fallback avatar type. Overrides GravatarInterface::getFallbackType
Gravatar::getFallbackTypes public static function Valid fallback types for when 'gravatar' is the primary type. Overrides GravatarInterface::getFallbackTypes
Gravatar::getHostName public function Gets the request host name. Overrides AvatarBase::getHostName
Gravatar::getRating public function Get the rating. Overrides GravatarInterface::getRating
Gravatar::getRatings public static function Get a list of valid ratings. Overrides GravatarInterface::getRatings
Gravatar::getTypes public static function Gets list of avatar types provided by this API. Overrides AvatarBaseInterface::getTypes
Gravatar::getTypesMap public static function Avatar types mapped to 'd' GET values. Overrides GravatarInterface::getTypesMap
Gravatar::getUrl public function Gets the URL for the avatar. Overrides AvatarBaseInterface::getUrl
Gravatar::hashIdentifier public static function Prepare an identifier for transmission to a third party. Overrides AvatarBase::hashIdentifier
Gravatar::setFallbackType public function The type used for when 'gravatar' type fails. Overrides GravatarInterface::setFallbackType
Gravatar::setFallbackUri public function The URI to an image used for when 'gravatar' type fails. Overrides GravatarInterface::setFallbackUri
Gravatar::setIdentifier public function Sets a unique identifier to be passed to the API. Overrides AvatarBase::setIdentifier
Gravatar::setRating public function Sets the maximum gravatar rating. Overrides GravatarInterface::setRating
Gravatar::__construct public function Constructs a new Gravatar object.
GravatarInterface::DIMENSION_MAXIMUM_WIDTH constant
GravatarInterface::DIMENSION_MINIMUM_WIDTH constant
GravatarInterface::GRAVATAR_HOSTNAME constant
GravatarInterface::GRAVATAR_HOSTNAME_SECURE constant