Skip to content

Commit

Permalink
Refactor avatar generation and improve type handling
Browse files Browse the repository at this point in the history
Reworked avatar type handling to separate InitialsAvatar and Identicon instances, deprecating mixed use of Avatar. Updated related methods and tests to reflect the refactored structure, ensuring stricter type safety and thorough test coverage.
  • Loading branch information
renfordt committed Jan 8, 2025
1 parent 31a4001 commit 9f0bbdc
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 184 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,6 @@ $larvatar = Larvatar::make(
);

// optional settings
$larvatar->avatar->setSymmetry(false);
$larvatar->avatar->setPixels(8);
$larvatar->identicon->setSymmetry(false);
$larvatar->identicon->setPixels(8);
```
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@
<directory suffix=".php">src</directory>
</include>
</source>
<coverage>
<report>
<html outputDirectory=".phpunit.cache/code-coverage" />
</report>
</coverage>
</phpunit>
134 changes: 9 additions & 125 deletions src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
namespace Renfordt\Larvatar;

use Exception;
use Renfordt\Larvatar\Enum\FormTypes;

abstract class Avatar
{
protected int $fontSize = 0;

protected string $fontFamily = '';
protected string $fontPath = '';
protected string $fontWeight = 'normal';
protected Name $name;
protected int $size = 100;
protected float $backgroundLightness = 0.8;
protected float $textLightness = 0.35;
protected float $foregroundLightness = 0.35;

/**
* Retrieves the HTML representation of the data.
Expand All @@ -35,66 +29,6 @@ abstract public function getHTML(bool $base64 = false): string;
*/
abstract public function getBase64(): string;

/**
* Get the font size
*
* @return int The font size value
*/
public function getFontSize(): int
{
return $this->fontSize;
}

/**
* Set the font size.
*
* @param int $fontSize The font size.
*/
public function setFontSize(int $fontSize): void
{
$this->fontSize = $fontSize;
}

/**
* Get the font family
*
* @return string The font family
*/
public function getFontFamily(): string
{
return $this->fontFamily;
}

/**
* Set the font family for the application
*
* @param string $fontFamily The font family to set
*/
public function setFontFamily(string $fontFamily): void
{
$this->fontFamily = $fontFamily;
}

/**
* Get the font path
*
* @return string The font path
*/
public function getFontPath(): string
{
return $this->fontPath;
}

/**
* Set the font path
*
* @param string $fontPath The path to the font
*/
public function setFontPath(string $fontPath): void
{
$this->fontPath = $fontPath;
}

/**
* Get the name of the object
*
Expand Down Expand Up @@ -139,43 +73,23 @@ public function setBackgroundLightness(float $backgroundLightness): void
}

/**
* Get the lightness value of the text
* Retrieves the lightness level of the foreground.
*
* @return float The lightness value of the text
* @return float The lightness value of the foreground
*/
public function getTextLightness(): float
public function getForegroundLightness(): float
{
return $this->textLightness;
return $this->foregroundLightness;
}

/**
* Set the text lightness value
* Sets the lightness value of the foreground within a specified range.
*
* @param float $textLightness The text lightness value to be set
* @param float $foregroundLightness The desired lightness value, a float between 0 and 1.
*/
public function setTextLightness(float $textLightness): void
public function setForegroundLightness(float $foregroundLightness): void
{
$this->textLightness = (float)clamp($textLightness, 0, 1);
}

/**
* Get the font weight
*
* @return string The font weight
*/
public function getFontWeight(): string
{
return $this->fontWeight;
}

/**
* Set the font weight for the application
*
* @param string $fontWeight The font weight to set
*/
public function setFontWeight(string $fontWeight): void
{
$this->fontWeight = $fontWeight;
$this->foregroundLightness = (float)clamp($foregroundLightness, 0, 1);
}

/**
Expand All @@ -197,34 +111,4 @@ public function setSize(int $size): void
{
$this->size = $size;
}

/**
* Sets the font family and path
*
* @param string $font The font family
* @param string $path The font path
*/
public function setFont(string $font, string $path): void
{
$this->setFontFamily($font);
$this->setFontPath($path);
}

public function setSymmetry(bool $symmetry): void
{
}

public function setPixels(int $pixels): void
{
}

public function setForm(string|FormTypes $form): void
{
}

public function setRotation(int $angle): void
{
}


}
118 changes: 117 additions & 1 deletion src/InitialsAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class InitialsAvatar extends Avatar
{
use ColorTrait;

protected int $fontSize = 0;
protected string $fontFamily = '';
protected string $fontPath = '';
protected string $fontWeight = 'normal';
private FormTypes $form = FormTypes::Circle;
private int $rotation = 0;
private int $offset = 0;
Expand Down Expand Up @@ -120,7 +124,7 @@ public function generate(): SVG
*/
[$darkColor, $lightColor] = $this->getColorSet(
$this->name,
$this->textLightness,
$this->foregroundLightness,
$this->backgroundLightness
);

Expand Down Expand Up @@ -249,4 +253,116 @@ public function getBase64(): string
{
return 'data:image/svg+xml;base64,' . base64_encode($this->generate()->__toString());
}

/**
* Get the font size
*
* @return int The font size value
*/
public function getFontSize(): int
{
return $this->fontSize;
}

/**
* Set the font size.
*
* @param int $fontSize The font size.
*/
public function setFontSize(int $fontSize): void
{
$this->fontSize = $fontSize;
}

/**
* Get the font family
*
* @return string The font family
*/
public function getFontFamily(): string
{
return $this->fontFamily;
}

/**
* Set the font family for the application
*
* @param string $fontFamily The font family to set
*/
public function setFontFamily(string $fontFamily): void
{
$this->fontFamily = $fontFamily;
}

/**
* Get the font path
*
* @return string The font path
*/
public function getFontPath(): string
{
return $this->fontPath;
}

/**
* Set the font path
*
* @param string $fontPath The path to the font
*/
public function setFontPath(string $fontPath): void
{
$this->fontPath = $fontPath;
}

/**
* Get the font weight
*
* @return string The font weight
*/
public function getFontWeight(): string
{
return $this->fontWeight;
}

/**
* Set the font weight for the application
*
* @param string $fontWeight The font weight to set
*/
public function setFontWeight(string $fontWeight): void
{
$this->fontWeight = $fontWeight;
}

/**
* Sets the font family and path
*
* @param string $font The font family
* @param string $path The font path
*/
public function setFont(string $font, string $path): void
{
$this->setFontFamily($font);
$this->setFontPath($path);
}

/**
* Get the lightness value of the text
*
* @return float The lightness value of the text
*/
public function getTextLightness(): float
{
return $this->foregroundLightness;
}

/**
* Set the text lightness value
*
* @param float $textLightness The text lightness value to be set
*/
public function setTextLightness(float $textLightness): void
{
$this->foregroundLightness = (float)clamp($textLightness, 0, 1);
}
}
Loading

0 comments on commit 9f0bbdc

Please sign in to comment.