pixielity / laravel-uuid
UUID generation and management utilities
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/pixielity/laravel-uuid
Requires
- php: ^8.5
- pixielity/laravel-support: *
Requires (Dev)
- mockery/mockery: ^1.6
- orchestra/testbench: ^10.0
- phpunit/phpunit: ^11.0
README
The UUID Module provides a robust and flexible API for generating and managing Universally Unique Identifiers (UUIDs). It supports various UUID versions (1, 3, 4, and 5) and includes utilities for validation, namespace-based generation, and lexicographically sortable UUIDs.
Features • Generate UUIDs for versions 1, 3, 4, and 5. • Supports namespace-based UUID generation. • Generate lexicographically sortable UUIDs. • Validate UUID strings for correctness. • Custom exceptions for error handling.
Installation
To use the UUID Module, follow these steps:
- Install the package (if applicable):
composer require pixielity/laravel-framework-uuid
- Configure it in your application or register the service provider (if required).
Usage
Importing the Facade
The Uuid facade offers a convenient way to interact with the UUID Manager:
use Pixielity\Uuid\Facades\Uuid;
Available Methods
- generate(int $version, ?string $namespace = null, ?string $name = null)
Generates a UUID for the specified version. Supports versions 1, 3, 4, and 5.
// Generate a version 4 UUID $uuidV4 = Uuid::generate(4);
// Generate a version 5 UUID using a namespace and name $uuidV5 = Uuid::generate(5, 'namespace-uuid', 'resource-name');
- orderedUuid()
Generates a lexicographically sortable UUID, ideal for database indexing.
$orderedUuid = Uuid::orderedUuid();
- uuid1()
Generates a version 1 UUID based on timestamp and node.
$uuidV1 = Uuid::uuid1();
- uuid3(string $namespace, string $name)
Generates a version 3 UUID using a namespace and name (MD5 hashing).
$uuidV3 = Uuid::uuid3('namespace-uuid', 'resource-name');
- uuid4()
Generates a random version 4 UUID.
$uuidV4 = Uuid::uuid4();
- uuid5(string $namespace, string $name)
Generates a version 5 UUID using a namespace and name (SHA-1 hashing).
$uuidV5 = Uuid::uuid5('namespace-uuid', 'resource-name');
- namespaceUuid(string $namespace, string $name)
Generates a namespace-based UUID, equivalent to uuid5.
$namespaceUuid = Uuid::namespaceUuid('namespace-uuid', 'resource-name');
- isValid(string $uuid)
Validates whether a given string is a valid UUID.
$isValid = Uuid::isValid('550e8400-e29b-41d4-a716-446655440000');
Examples
Generating and Validating a UUID
use Pixielity\Uuid\Facades\Uuid;
// Generate a UUID version 4 $uuid = Uuid::uuid4(); echo "Generated UUID: $uuid";
// Validate the UUID if (Uuid::isValid($uuid)) { echo "The UUID is valid."; } else { echo "The UUID is invalid."; }
Generating a Namespace-Based UUID
$namespaceUuid = Uuid::namespaceUuid('my-namespace-uuid', 'my-resource-name'); echo "Namespace-based UUID: $namespaceUuid";
Constants
The module defines constants for common use cases: • UUID Field Name:
const UUID = 'uuid';
You can reference this constant for database schema fields or application logic.
Exceptions
All methods throw the Pixielity\Foundation\Exceptions\Exception in cases where UUID generation or validation fails. Ensure you wrap calls in try-catch blocks for error handling.
try { $uuid = Uuid::generate(6); // Invalid version } catch (Exception $e) { echo "Error: " . $e->getMessage(); }
Contributing
- Fork the repository.
- Create a feature branch: git checkout -b feature-name.
- Commit your changes: git commit -m "Add feature description".
- Push to the branch: git push origin feature-name.
- Open a pull request for review.
License
This module is open-source software licensed under the MIT license.
Support
If you encounter any issues or have questions, feel free to open an issue in the repository or contact the support team.