You are here

class Robohash in Avatar Kit 8

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

Implements the Robohash.org API.

Hierarchy

Expanded class hierarchy of Robohash

1 file declares its use of Robohash
Robohash.php in avatars_robohash/src/Plugin/AvatarGenerator/Robohash.php
2 string references to 'Robohash'
avatars_robohash.info.yml in avatars_robohash/avatars_robohash.info.yml
avatars_robohash/avatars_robohash.info.yml
avatars_robohash.schema.yml in avatars_robohash/config/schema/avatars_robohash.schema.yml
avatars_robohash/config/schema/avatars_robohash.schema.yml

File

avatars_robohash/src/Robohash.php, line 11

Namespace

Drupal\avatars_robohash
View source
class Robohash extends AvatarBase implements RobohashInterface {

  /**
   * The background to use, or NULL to use default.
   *
   * @var string|null
   */
  protected $background;

  /**
   * Constructs a new Robohash object.
   */
  public function __construct() {
    $this
      ->setDimensionConstraints(Robohash::DIMENSION_MINIMUM_WIDTH, Robohash::DIMENSION_MAXIMUM_WIDTH, Robohash::DIMENSION_MINIMUM_HEIGHT, Robohash::DIMENSION_MAXIMUM_HEIGHT);
  }

  /**
   * {@inheritdoc}
   */
  public function getHostName() {
    $hostname = parent::getHostName();
    return isset($hostname) ? $hostname : $this::ROBOHASH_HOSTNAME;
  }

  /**
   * {@inheritdoc}
   */
  public static function getBackgrounds() {
    return [
      'transparent' => 'Transparent',
      'places' => 'Places',
      'patterns' => 'Patterns',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function getBackgroundsMap() {
    return [
      'transparent' => '',
      'places' => 'bg1',
      'patterns' => 'bg2',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getBackground() {
    return $this->background;
  }

  /**
   * {@inheritdoc}
   */
  public function setBackground($background = NULL) {
    if (!array_key_exists($background, $this
      ->getBackgrounds())) {
      throw new AvatarException('Invalid background');
    }
    $this->background = $background;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function getTypes() {
    return [
      'robot' => 'Robot',
      'monster' => 'Monster',
      'robot_head' => 'Robot Head',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function getTypesMap() {
    return [
      'robot' => 1,
      'monster' => 2,
      'robot_head' => 3,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function setTypeRandom() {
    $this->type = 'any';
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

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::hashIdentifier public static function Prepare an identifier for transmission to a third party. Overrides AvatarBaseInterface::hashIdentifier 1
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::setIdentifier public function Sets a unique identifier to be passed to the API. Overrides AvatarBaseInterface::setIdentifier 1
AvatarBase::setIsSecure public function Sets the request to secure. Overrides AvatarBaseInterface::setIsSecure 1
AvatarBase::setType public function Sets the avatar type. Overrides AvatarBaseInterface::setType
Robohash::$background protected property The background to use, or NULL to use default.
Robohash::getBackground public function Gets the background. Overrides RobohashInterface::getBackground
Robohash::getBackgrounds public static function A list of valid backgrounds. Overrides RobohashInterface::getBackgrounds
Robohash::getBackgroundsMap public static function Backgrounds mapped to GET values. Overrides RobohashInterface::getBackgroundsMap
Robohash::getHostName public function Gets the request host name. Overrides AvatarBase::getHostName
Robohash::getTypes public static function Gets list of avatar types provided by this API. Overrides AvatarBaseInterface::getTypes
Robohash::getTypesMap public static function Avatar types mapped to GET values. Overrides RobohashInterface::getTypesMap
Robohash::getUrl public function Gets the URL for the avatar. Overrides AvatarBaseInterface::getUrl
Robohash::setBackground public function Set the background. Overrides RobohashInterface::setBackground
Robohash::setTypeRandom public function Sets type to random. Overrides RobohashInterface::setTypeRandom
Robohash::__construct public function Constructs a new Robohash object.
RobohashInterface::DIMENSION_MAXIMUM_HEIGHT constant
RobohashInterface::DIMENSION_MAXIMUM_WIDTH constant
RobohashInterface::DIMENSION_MINIMUM_HEIGHT constant
RobohashInterface::DIMENSION_MINIMUM_WIDTH constant
RobohashInterface::ROBOHASH_HOSTNAME constant