Skip to content

Commit

Permalink
apply strong type
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Sep 20, 2024
1 parent b9b1c20 commit f0534bd
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 95 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": "^10.5",
"mockery/mockery": "~1.3",
"php-coveralls/php-coveralls": "^2.1"
"php-coveralls/php-coveralls": "^2.1",
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-type-coverage": "^2.8"
},
"autoload": {
"psr-4": {
Expand All @@ -59,5 +61,10 @@
"Avatar": "Laravolt\\Avatar\\Facade"
}
}
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
129 changes: 64 additions & 65 deletions src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,61 @@ class Avatar
use AttributeGetter;
use AttributeSetter;

protected $name;
protected ?string $name = "";

protected $chars;
protected int $chars;

protected $shape;
protected string $shape;

protected $width;
protected int $width;

protected $height;
protected int $height;

protected $availableBackgrounds = [];
protected array $availableBackgrounds = [];

protected $availableForegrounds = [];
protected array $availableForegrounds = [];

protected $fonts = [];
protected array $fonts = [];

protected $fontSize;
protected float $fontSize;

protected $fontFamily;
protected ?string $fontFamily = null;

protected $borderSize = 0;
protected int $borderSize = 0;

protected $borderColor;
protected string $borderColor;

protected $borderRadius = 0;
protected int $borderRadius = 0;

protected $ascii = false;
protected bool $ascii = false;

protected $uppercase = false;
protected bool $uppercase = false;

protected $rtl = false;
protected bool $rtl = false;

protected \Intervention\Image\Image $image;

protected $font = null;
protected ?string $font;

protected $background = '#CCCCCC';
protected string $background = '#CCCCCC';

protected $foreground = '#FFFFFF';
protected string $foreground = '#FFFFFF';

protected $initials = '';
protected string $initials = '';

protected $cache;
protected Repository|ArrayStore $cache;

protected $driver;
protected mixed $driver;

protected $initialGenerator;
protected GeneratorInterface $initialGenerator;

protected $defaultFont = __DIR__.'/../fonts/OpenSans-Bold.ttf';
protected string $defaultFont = __DIR__.'/../fonts/OpenSans-Bold.ttf';

protected $themes = [];
protected array $themes = [];

protected $theme;
protected string|array|null $theme;

protected $defaultTheme = [];
protected array $defaultTheme = [];

/**
* Avatar constructor.
Expand All @@ -89,6 +89,7 @@ public function __construct(array $config = [], Repository $cache = null)
$this->theme = $config['theme'] ?? null;
$this->defaultTheme = $this->validateConfig($config);
$this->applyTheme($this->defaultTheme);
$this->initialGenerator = new DefaultGenerator();

// Add any additional themes for further use
$themes = $this->resolveTheme('*', $config['themes'] ?? []);
Expand All @@ -107,12 +108,12 @@ public function __toString()
return (string) $this->toBase64();
}

public function setGenerator(GeneratorInterface $generator)
public function setGenerator(GeneratorInterface $generator): void
{
$this->initialGenerator = $generator;
}

public function create($name)
public function create(string $name): static
{
$this->name = $name;

Expand All @@ -121,7 +122,7 @@ public function create($name)
return $this;
}

public function applyTheme(array $config)
public function applyTheme(array $config): void
{
$config = $this->validateConfig($config);
$this->shape = $config['shape'];
Expand All @@ -141,24 +142,24 @@ public function applyTheme(array $config)
$this->borderRadius = $config['border']['radius'];
}

public function addTheme(string $name, array $config)
public function addTheme(string $name, array $config): static
{
$this->themes[$name] = $this->validateConfig($config);

return $this;
}

protected function setRandomTheme()
protected function setRandomTheme(): void
{
$themes = $this->resolveTheme($this->theme, $this->themes);
if (!empty($themes)) {
$this->applyTheme($this->getRandomElement($themes, []));
}
}

protected function resolveTheme($theme, $config)
protected function resolveTheme(array|string|null $theme, array $cfg): array
{
$config = collect($config);
$config = collect($cfg);
$themes = [];

foreach ((array) $theme as $themeName) {
Expand All @@ -177,7 +178,7 @@ protected function resolveTheme($theme, $config)
return $themes;
}

public function toBase64()
public function toBase64(): string
{
$key = $this->cacheKey();
if ($base64 = $this->cache->get($key)) {
Expand All @@ -193,14 +194,14 @@ public function toBase64()
return $base64;
}

public function save($path, $quality = 90)
public function save(?string $path, int $quality = 90): \Intervention\Image\Interfaces\ImageInterface
{
$this->buildAvatar();

return $this->image->save($path, $quality);
}

public function toSvg()
public function toSvg(): string
{
$this->buildInitial();

Expand All @@ -211,15 +212,15 @@ public function toSvg()

$svg = '<svg xmlns="http://www.w3.org/2000/svg" width="'.$this->width.'" height="'.$this->height.'" viewBox="0 0 '.$this->width.' '.$this->height.'">';

if ($this->shape == 'square') {
if ($this->shape === 'square') {
$svg .= '<rect x="'.$x
.'" y="'.$y
.'" width="'.$width.'" height="'.$height
.'" stroke="'.$this->getBorderColor()
.'" stroke-width="'.$this->borderSize
.'" rx="'.$this->borderRadius
.'" fill="'.$this->background.'" />';
} elseif ($this->shape == 'circle') {
} elseif ($this->shape === 'circle') {
$svg .= '<circle cx="'.$center
.'" cy="'.$center
.'" r="'.$radius
Expand All @@ -243,7 +244,7 @@ public function toSvg()
return $svg;
}

public function toGravatar(array $param = null)
public function toGravatar(array $param = null): string
{
// Hash generation taken from https://en.gravatar.com/site/implement/images/php/
$hash = md5(strtolower(trim($this->name)));
Expand Down Expand Up @@ -271,34 +272,34 @@ public function toGravatar(array $param = null)
return $url;
}

public function getInitial()
public function getInitial(): string
{
return $this->initials;
}

public function getImageObject()
public function getImageObject(): \Intervention\Image\Image
{
$this->buildAvatar();

return $this->image;
}

protected function getRandomBackground()
protected function getRandomBackground(): string
{
return $this->getRandomElement($this->availableBackgrounds, $this->background);
}

protected function getRandomForeground()
protected function getRandomForeground(): string
{
return $this->getRandomElement($this->availableForegrounds, $this->foreground);
}

protected function getRandomFont()
protected function getRandomFont(): string
{
return $this->getRandomElement($this->fonts, $this->defaultFont);
}

protected function getBorderColor()
protected function getBorderColor(): string
{
if ($this->borderColor === 'foreground') {
return $this->foreground;
Expand All @@ -310,7 +311,7 @@ protected function getBorderColor()
return $this->borderColor;
}

public function buildAvatar()
public function buildAvatar(): static
{
$this->buildInitial();

Expand Down Expand Up @@ -343,17 +344,17 @@ function (FontFactory $font) {
return $this;
}

protected function createShape()
protected function createShape(): void
{
$method = 'create'.ucfirst($this->shape).'Shape';
if (method_exists($this, $method)) {
return $this->$method();
$this->$method();
} else {
throw new \InvalidArgumentException("Shape [$this->shape] currently not supported.");
}

throw new \InvalidArgumentException("Shape [$this->shape] currently not supported.");
}

protected function createCircleShape()
protected function createCircleShape(): void
{
$circleDiameter = (int) ($this->width - $this->borderSize);
$x = (int) ($this->width / 2);
Expand All @@ -370,7 +371,7 @@ function (CircleFactory $circle) use ($circleDiameter) {
);
}

protected function createSquareShape()
protected function createSquareShape(): void
{
$edge = (ceil($this->borderSize / 2));
$x = $y = $edge;
Expand All @@ -388,7 +389,7 @@ function (RectangleFactory $draw) use ($width, $height) {
);
}

protected function cacheKey()
protected function cacheKey(): string
{
$keys = [];
$attributes = [
Expand All @@ -410,17 +411,20 @@ protected function cacheKey()
return md5(implode('-', $keys));
}

protected function getRandomElement($array, $default)
/**
* @throws \Random\RandomException
*/
protected function getRandomElement(array $array, mixed $default): mixed
{
// Make it work for associative array
$array = array_values($array);

$name = $this->name;
if ($name === null || strlen($name) === 0) {
if ($name === null || $name === '') {
$name = chr(random_int(65, 90));
}

if (count($array) == 0) {
if (empty($array)) {
return $default;
}

Expand All @@ -435,17 +439,12 @@ protected function getRandomElement($array, $default)
return $array[$number % count($array)];
}

protected function buildInitial()
protected function buildInitial(): void
{
// fallback to default
if (!$this->initialGenerator) {
$this->initialGenerator = new DefaultGenerator();
}

$this->initials = $this->initialGenerator->make($this->name, $this->chars, $this->uppercase, $this->ascii, $this->rtl);
}

protected function validateConfig($config)
protected function validateConfig(array $config): array
{
$fallback = [
'shape' => 'circle',
Expand All @@ -472,7 +471,7 @@ protected function validateConfig($config)
return $config + $this->defaultTheme + $fallback;
}

protected function initTheme()
protected function initTheme(): void
{
$this->setRandomTheme();
$this->setForeground($this->getRandomForeground());
Expand Down
Loading

0 comments on commit f0534bd

Please sign in to comment.