UlidFactory example

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Uid\Factory\UlidFactory;

#[AsCommand(name: 'ulid:generate', description: 'Generate a ULID')] class GenerateUlidCommand extends Command
{
    private UlidFactory $factory;

    public function __construct(UlidFactory $factory = null)
    {
        $this->factory = $factory ?? new UlidFactory();

        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setDefinition([
                new InputOption('time', null, InputOption::VALUE_REQUIRED, 'The ULID timestamp: a parsable date/time string'),
                new InputOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of ULID to generate', 1),
                new InputOption('format', 'f', InputOption::VALUE_REQUIRED, sprintf('The ULID output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'base32'),
            ])


namespace Symfony\Component\Uid\Tests\Factory;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Factory\UlidFactory;

final class UlidFactoryTest extends TestCase
{
    public function testCreate()
    {
        $ulidFactory = new UlidFactory();

        $ulidFactory->create();

        $ulid1 = $ulidFactory->create(new \DateTimeImmutable('@999999.123000'));
        $this->assertSame('999999.123000', $ulid1->getDateTime()->format('U.u'));
        $ulid2 = $ulidFactory->create(new \DateTimeImmutable('@999999.123000'));
        $this->assertSame('999999.123000', $ulid2->getDateTime()->format('U.u'));

        $this->assertFalse($ulid1->equals($ulid2));
        $this->assertSame(-1, $ulid1->compare($ulid2));

        
Home | Imprint | This part of the site doesn't use cookies.