class Robohash in Avatar Kit 8
Same name in this branch
- 8 avatars_robohash/src/Robohash.php \Drupal\avatars_robohash\Robohash
- 8 avatars_robohash/src/Plugin/AvatarGenerator/Robohash.php \Drupal\avatars_robohash\Plugin\AvatarGenerator\Robohash
Implements the Robohash.org API.
Hierarchy
- class \Drupal\avatars\AvatarBase implements AvatarBaseInterface
- class \Drupal\avatars_robohash\Robohash implements RobohashInterface
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_robohashView 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
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 static | function |
Prepare an identifier for transmission to a third party. Overrides AvatarBaseInterface:: |
1 |
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 a unique identifier to be passed to the API. Overrides AvatarBaseInterface:: |
1 |
AvatarBase:: |
public | function |
Sets the request to secure. Overrides AvatarBaseInterface:: |
1 |
AvatarBase:: |
public | function |
Sets the avatar type. Overrides AvatarBaseInterface:: |
|
Robohash:: |
protected | property | The background to use, or NULL to use default. | |
Robohash:: |
public | function |
Gets the background. Overrides RobohashInterface:: |
|
Robohash:: |
public static | function |
A list of valid backgrounds. Overrides RobohashInterface:: |
|
Robohash:: |
public static | function |
Backgrounds mapped to GET values. Overrides RobohashInterface:: |
|
Robohash:: |
public | function |
Gets the request host name. Overrides AvatarBase:: |
|
Robohash:: |
public static | function |
Gets list of avatar types provided by this API. Overrides AvatarBaseInterface:: |
|
Robohash:: |
public static | function |
Avatar types mapped to GET values. Overrides RobohashInterface:: |
|
Robohash:: |
public | function |
Gets the URL for the avatar. Overrides AvatarBaseInterface:: |
|
Robohash:: |
public | function |
Set the background. Overrides RobohashInterface:: |
|
Robohash:: |
public | function |
Sets type to random. Overrides RobohashInterface:: |
|
Robohash:: |
public | function | Constructs a new Robohash object. | |
RobohashInterface:: |
constant | |||
RobohashInterface:: |
constant | |||
RobohashInterface:: |
constant | |||
RobohashInterface:: |
constant | |||
RobohashInterface:: |
constant |