Skip to content

Commit

Permalink
Change how custom generators are resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk committed Mar 6, 2024
1 parent 0b1a186 commit 25dde40
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Support/GeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ public static function make(string $generator): self

public function generator(): Generator
{
if (class_exists($this->generator) && in_array(Generator::class, class_implements($this->generator), true)) {
return new $this->generator;
}

return match ($this->generator) {
'ksuid' => new KsuidGenerator,
'snowflake' => new SnowflakeGenerator,
'uuid' => new UuidGenerator,
default => throw InvalidGenerator::invalid($this->generator),
default => $this->makeCustomGenerator($this->generator),
};
}

protected function makeCustomGenerator(string $generator): Generator
{
throw_unless(
class_exists($generator) && in_array(Generator::class, class_implements($generator), true),
InvalidGenerator::invalid($generator)
);

return new $generator;
}
}

0 comments on commit 25dde40

Please sign in to comment.