diff --git a/src/Avatar.php b/src/Avatar.php index a8c20f2..a1a90eb 100644 --- a/src/Avatar.php +++ b/src/Avatar.php @@ -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."); + } + + /** + * 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; diff --git a/src/Facade.php b/src/Facade.php index 8e5e40c..84e754d 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -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) + * @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 { /** diff --git a/tests/AvatarPhpTest.php b/tests/AvatarPhpTest.php index 213adfb..3db163c 100644 --- a/tests/AvatarPhpTest.php +++ b/tests/AvatarPhpTest.php @@ -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')); + } + + #[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('assertStringContainsString('JS', $svg); + } + protected function sampleBase64String() { if (version_compare(phpversion(), '7.2', '>=')) { diff --git a/tests/ImageExportTest.php b/tests/ImageExportTest.php index eb83d15..1c4c5f5 100644 --- a/tests/ImageExportTest.php +++ b/tests/ImageExportTest.php @@ -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() diff --git a/tests/StorageOptimizationTest.php b/tests/StorageOptimizationTest.php index 7d88fbb..5f06525 100644 --- a/tests/StorageOptimizationTest.php +++ b/tests/StorageOptimizationTest.php @@ -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()