You are here

public function Robohash::getUrl in Avatar Kit 8

Gets the URL for the avatar.

Return value

string A URL for an avatar.

Throws

\Drupal\avatars\Exception\AvatarException Thrown if missing parameters.

Overrides AvatarBaseInterface::getUrl

File

avatars_robohash/src/Robohash.php, line 113

Class

Robohash
Implements the Robohash.org API.

Namespace

Drupal\avatars_robohash

Code

public function getUrl() {
  $kv = [];
  $url = ($this
    ->isSecure() ? 'https://' : 'http://') . $this
    ->getHostName() . '/';
  $identifier = $this
    ->getIdentifier();
  if (!strlen($identifier)) {
    throw new AvatarException('Robohash missing identifier/hash');
  }
  $url .= $this
    ->identifierIsPreHashed() ? $identifier : $this
    ->hashIdentifier($identifier);
  $background = $this
    ->getBackground();
  $background_map = $this
    ->getBackgroundsMap();
  if (!empty($background) && $background != key($background_map)) {
    $kv['bgset'] = $background_map[$background];
  }
  $type_map = $this
    ->getTypesMap();
  $type = $this
    ->getType();
  if ($type == 'any') {
    $kv['set'] = 'any';
  }
  elseif (!empty($type) && $type != key($type_map)) {
    $kv['set'] = 'set' . $type_map[$type];
  }
  $width = $this->width;
  $height = $this->height;

  // Robohash requires width AND height to be set.
  // Robohash dimensions do not have to be square, although the rendered image
  // will be distorted.
  // Validation is done in setDimensions method.
  if (is_numeric($width) && is_numeric($height)) {
    $kv['size'] = $width . 'x' . $height;
  }
  $query = http_build_query($kv);
  return !empty($query) ? $url . '?' . $query : $url;
}