antikirra/uuid4

Installs: 14

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/antikirra/uuid4

3.1.1 2025-10-19 05:48 UTC

This package is auto-updated.

Last update: 2025-10-19 05:49:16 UTC


README

Packagist Dependency Version Packagist Version

Ultra-fast, lightweight PHP library for generating and validating RFC 4122 compliant UUIDv4 identifiers. Perfect for distributed systems, database primary keys, and scenarios requiring cryptographically secure universally unique identifiers without external dependencies.

Install

composer require antikirra/uuid4:^3.0

Why UUID4?

  • โœจ Blazing Fast Performance - Optimized implementation with minimal overhead
  • ๐Ÿ”ง Universal Compatibility - Works seamlessly from PHP 5.6 to PHP 8.4+
  • ๐Ÿ”’ Cryptographically Secure - Uses secure random number generation (random_bytes)
  • ๐Ÿ“œ RFC 4122 Compliant - Strictly follows official UUID version 4 specification
  • ๐Ÿ“ฆ Lightweight - Pure PHP implementation with minimal dependencies
  • โœ… Built-in Validation - Comprehensive UUID format and variant validation
  • ๐Ÿš€ Production Ready - Battle-tested with comprehensive test coverage
  • ๐Ÿงช Fully Tested - 100% code coverage with PHPUnit/Pest test suite

Features

  • RFC 4122 Compliant Generation: Creates properly formatted UUIDv4 with correct version (4) and variant bits (8, 9, a, b)
  • Cryptographic Security: Leverages PHP's random_bytes() for cryptographically secure randomness
  • Strict Validation: Validates UUID format, version, variant, and character set
  • Case-Insensitive: Accepts both uppercase and lowercase UUID strings
  • Type Safety: Throws exceptions for invalid input types with clear error messages
  • Legacy Support: Compatible with ancient PHP versions (5.6+) through modern PHP 8.4
  • Zero Configuration: Works out of the box, no setup required
  • Memory Efficient: Minimal memory footprint, perfect for high-load applications

Perfect for

  • Database Primary Keys: Alternative to auto-increment IDs in distributed databases
  • Distributed Systems: Unique ID generation across multiple servers without coordination
  • API Development: Request IDs, transaction tracking, resource identifiers
  • Microservices: Service-independent unique identifiers
  • Event Sourcing: Event IDs, aggregate identifiers
  • File Systems: Unique file names, temporary file generation
  • Session Management: Session tokens, correlation IDs
  • Legacy Systems: Drop-in UUID solution for old PHP applications

Requirements

  • PHP: 5.6 or higher (tested up to PHP 8.4)
  • Extensions: None (uses only core PHP functions)
  • Dependencies:
    • antikirra/random-bytes for PHP < 7.0 (provides random_bytes() polyfill)
    • Zero dependencies for PHP 7.0+

Basic usage

<?php

use function Antikirra\uuid4;
use function Antikirra\uuid4_validate;

require __DIR__ . '/vendor/autoload.php';

// Generate a new UUID v4
echo uuid4(); // 19f16271-84eb-449c-d22b-0bbf7fcc1f63

// Validate UUID format
uuid4_validate('74331057-8c1a-40f0-1e6f-a3073c3f56fc'); // true
uuid4_validate('not-a-valid-uuid'); // false
uuid4_validate('550e8400-e29b-31d4-a716-446655440000'); // false (version 3, not 4)

// Type safety
try {
    uuid4_validate(123);
} catch (InvalidArgumentException $e) {
    echo $e->getMessage(); // UUID must be a string, got integer
}

Testing

This library is thoroughly tested with comprehensive test coverage:

  • UUID Generation: Validates format, version bits, variant bits, and randomness
  • UUID Validation: Tests valid/invalid formats, version detection, variant validation
  • Edge Cases: Handles uppercase/lowercase, malformed UUIDs, type errors
  • PHP Compatibility: Tested across PHP 5.6 through PHP 8.4

Performance

Blazing fast performance benchmarks on Apple M4 with PHP 8.4:

Operation Speed Details
UUID Generation 1,626,197 ops/sec 100,000 iterations in 0.0615 seconds
UUID Validation 8,338,825 ops/sec 1,000,000 iterations in 0.1199 seconds

Run php benchmark.php to test performance on your own system.

Keywords

uuid4, uuid-generator, rfc4122, unique-identifier, php-uuid, cryptographically-secure, distributed-systems, php-5.6-compatible, zero-dependencies, lightweight-library, database-primary-keys, microservices