Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,61 @@ public function __toString()
return (string) $this->toBase64();
}

/**
* Handle dynamic method calls on the instance.
*
* @return mixed
*/
public function __call(string $method, array $args): mixed
{
if ($method === 'create') {
$name = $args['name'] ?? ($args[0] ?? null);

if (! is_string($name)) {
throw new \InvalidArgumentException('The create() method requires a string name argument.');
}

return $this->createAvatar($name);
}

throw new \BadMethodCallException("Method {$method} does not exist.");
}

throw new \BadMethodCallException("Method {$method} does not exist.");
}
Comment on lines +125 to +146

/**
* Handle static method calls to allow static usage of the Avatar class.
* This enables calling Avatar::create('Name') without a facade.
*
* @return mixed
*/
public static function __callStatic(string $method, array $args): mixed
{
if ($method === 'create') {
$name = $args['name'] ?? ($args[0] ?? null);

if (! is_string($name)) {
throw new \InvalidArgumentException('The create() method requires a string name argument.');
}

$instance = new static;

return $instance->createAvatar($name);
}

throw new \BadMethodCallException("Method {$method} does not exist.");
}

throw new \BadMethodCallException("Method {$method} does not exist.");
}

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

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

Expand Down
21 changes: 21 additions & 0 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

namespace Laravolt\Avatar;

/**
* @method static \Laravolt\Avatar\Avatar create(string $name)
* @method static \Laravolt\Avatar\Avatar setBackground(string $color)
* @method static \Laravolt\Avatar\Avatar setForeground(string $color)
* @method static \Laravolt\Avatar\Avatar setDimension(int $width, ?int $height = null)
* @method static \Laravolt\Avatar\Avatar setFontSize(float $size)
* @method static \Laravolt\Avatar\Avatar setFont(string $font)
* @method static \Laravolt\Avatar\Avatar setShape(string $shape)
* @method static \Laravolt\Avatar\Avatar setChars(int $chars)
* @method static \Laravolt\Avatar\Avatar setBorder(int $size, string $color, int $radius = 0)
* @method static \Laravolt\Avatar\Avatar setBorderRadius(int $radius)
* @method static \Laravolt\Avatar\Avatar setTheme(array|string $theme)
Comment thread
Copilot marked this conversation as resolved.
* @method static \Laravolt\Avatar\Avatar setResponsive(bool $responsive)
* @method static \Laravolt\Avatar\Avatar buildAvatar()
* @method static string toBase64()
* @method static string toSvg()
* @method static string toGravatar()
* @method static string getInitial()
*
* @see \Laravolt\Avatar\Avatar
*/
class Facade extends \Illuminate\Support\Facades\Facade
{
/**
Expand Down
21 changes: 21 additions & 0 deletions tests/AvatarPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,27 @@ public function it_can_set_custom_generator()
$this->assertEquals('foo', $avatar->buildAvatar()->getInitial());
}

#[Test]
public function it_can_be_called_statically()
{
$avatar = \Laravolt\Avatar\Avatar::create('John Doe');

$this->assertInstanceOf(\Laravolt\Avatar\Avatar::class, $avatar);
$this->assertEquals('John Doe', $avatar->getAttribute('name'));
}
Comment on lines +586 to +593

#[Test]
public function it_can_chain_methods_after_static_create()
{
$svg = \Laravolt\Avatar\Avatar::create('Jane Smith')
->setDimension(100, 100)
->setShape('circle')
->toSvg();

$this->assertStringContainsString('<svg', $svg);
$this->assertStringContainsString('JS', $svg);
}

protected function sampleBase64String()
{
if (version_compare(phpversion(), '7.2', '>=')) {
Expand Down
3 changes: 1 addition & 2 deletions tests/ImageExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ protected function setUp(): void
parent::setUp();

// Create a test avatar class with ImageExport trait
$this->avatar = new class extends Avatar
{
$this->avatar = new class extends Avatar {
use ImageExport;

public function __construct()
Expand Down
3 changes: 1 addition & 2 deletions tests/StorageOptimizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ protected function setUp(): void
$this->mockCache();

// Create a test avatar class with StorageOptimization trait
$this->avatar = new class extends Avatar
{
$this->avatar = new class extends Avatar {
use StorageOptimization;

public function __construct()
Expand Down
Loading