Skip to content

Commit

Permalink
Refactor and add documentation for InitialsAvatar class
Browse files Browse the repository at this point in the history
The functionality of the InitialsAvatar class has been divided into smaller methods to enhance readability and maintainability. Documentation was added to all methods, offering a clear and precise description of their function and parameters. This new design makes the code more modular and easier to understand.
  • Loading branch information
renfordt committed Mar 31, 2024
1 parent a59c731 commit b8b4697
Showing 1 changed file with 97 additions and 39 deletions.
136 changes: 97 additions & 39 deletions src/InitialsAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,35 @@ public function __construct(string $name = '')
$this->setName($name);
}

/**
* Sets the names for the avatar
* @param string $name Names seperated by a space for generating the avatar
* @return void
*/
public function setName(string $name): void
{
$this->names = explode(' ', $name);
}

/**
* Generates the InitialsAvatar as an SVG
* @param array $names Array of Names which shall be shortend for the initials
* @return string Returns a SVG code of the Initials
*/
public function generate(array $names = []): string
{
if (empty($names)) {
$names = $this->names;
}
$names = $this->getNames($names);
$larvatar = new SVG($this->size, $this->size);
$doc = $larvatar->getDocument();
if ($this->fontPath != '' && $this->fontFamily != '') {
SVG::addFont(__DIR__.$this->fontPath);
}

$this->addFontIfNotEmpty();
$halfSize = $this->size / 2;

$color = new Color(ColorType::Hex, $this->generateHexColor($names));
$color = $this->getColor($names);
list($darkColor, $lightColor) = $color->getColorSet();

$circle = new SVGCircle($halfSize, $halfSize, $halfSize);
$circle->setStyle('fill', $lightColor->getHex());

$initials = '';
foreach ($names as $name) {
$initials .= substr($name, 0, 1);
}

$initials = new SVGText($initials, '50%', '55%');
$initials->setStyle('fill', $darkColor->getHex());
$initials->setStyle('text-anchor', 'middle');
$initials->setStyle('dominant-baseline', 'middle');
$initials->setFontFamily($this->fontFamily);
$initials->setFontSize($this->size * 0.5 .'px');
$circle = $this->getCircle($halfSize, $lightColor);
$initials = $this->getInitials($names, $darkColor);

$doc->addChild($circle);
$doc->addChild($initials);
Expand All @@ -65,41 +60,44 @@ public function generate(array $names = []): string
}

/**
* Sets the size of the avatar
* @param int $size Size in pixel
* @return void
* Retrieves the names array
* If the provided $names array is empty,
* it returns the default names array
*
* @param array $names The names array
* @return array The names array to be returned
*/
public function setSize(int $size): void
private function getNames(array $names): array
{
$this->size = $size;
return empty($names) ? $this->names : $names;
}

/**
* Sets the names for the avatar
* @param string $name Names seperated by a space for generating the avatar
* Adds a font if the font path and font family are not empty
* @return void
*/
public function setName(string $name): void
private function addFontIfNotEmpty(): void
{
$this->names = explode(' ', $name);
if ($this->fontPath != '' && $this->fontFamily != '') {
SVG::addFont(__DIR__.$this->fontPath);
}
}

/**
* Sets the font which shall be used for the avatar
* @param string $fontFamily Font Family, e.g. 'Roboto'
* @param string $path Relative path to the true type font with a leading slash, e.g. '/font/Roboto-Bold.ttf'
* @return void
* Retrieves the color based on the given array of names
*
* @param array $names An array of names
* @return Color The color object with the generated hex color
*/
public function setFont(string $fontFamily, string $path): void
private function getColor(array $names): Color
{
$this->fontFamily = $fontFamily;
$this->fontPath = $path;
return new Color(ColorType::Hex, $this->generateHexColor($names));
}

/**
* Generates a hex color code based on the names
* @param array|null $names Array of names used to generate the hex color code.
* @param int $offset Offset of the hash, similar to a seed
* @param array|null $names Array of names used to generate the hex color code.
* @param int $offset Offset of the hash, similar to a seed
* @return string Returns a color hash code, e.g. '#123456'
*/
public function generateHexColor(array $names = null, int $offset = 0): string
Expand All @@ -111,4 +109,64 @@ public function generateHexColor(array $names = null, int $offset = 0): string
$hash = md5($name);
return '#'.substr($hash, $offset, 6);
}

/**
* Get a circle SVG element
*
* @param float $halfSize Half of the size of the circle
* @param Color $lightColor The light color to fill the circle with
* @return SVGCircle The circle SVG element
*/
private function getCircle(float $halfSize, Color $lightColor): SVGCircle
{
$circle = new SVGCircle($halfSize, $halfSize, $halfSize);
$circle->setStyle('fill', $lightColor->getHex());

return $circle;
}

/**
* Generates initials for the given names and returns SVGText object
* @param array $names List of names
* @param Color $darkColor Dark color object
* @return SVGText SVGText object containing the initials
*/
private function getInitials(array $names, Color $darkColor): SVGText
{
$initialsText = '';
foreach ($names as $name) {
$initialsText .= substr($name, 0, 1);
}

$initials = new SVGText($initialsText, '50%', '55%');
$initials->setStyle('fill', $darkColor->getHex());
$initials->setStyle('text-anchor', 'middle');
$initials->setStyle('dominant-baseline', 'middle');
$initials->setFontFamily($this->fontFamily);
$initials->setFontSize($this->size * 0.5 .'px');

return $initials;
}

/**
* Sets the size of the avatar
* @param int $size Size in pixel
* @return void
*/
public function setSize(int $size): void
{
$this->size = $size;
}

/**
* Sets the font which shall be used for the avatar
* @param string $fontFamily Font Family, e.g. 'Roboto'
* @param string $path Relative path to the true type font with a leading slash, e.g. '/font/Roboto-Bold.ttf'
* @return void
*/
public function setFont(string $fontFamily, string $path): void
{
$this->fontFamily = $fontFamily;
$this->fontPath = $path;
}
}

0 comments on commit b8b4697

Please sign in to comment.