Convert Figma logo to code with AI

ramsey logouuid

:snowflake: A PHP library for generating universally unique identifiers (UUIDs).

12,433
500
12,433
23

Top Related Projects

Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

PHP 5.x support for random_bytes() and random_int()

Quick Overview

Ramsey/uuid is a PHP library for generating and working with universally unique identifiers (UUIDs). It provides a robust implementation of RFC 4122 UUIDs, supporting all five UUID versions (1, 2, 3, 4, and 5) and offering additional utility functions for UUID manipulation and validation.

Pros

  • Comprehensive UUID support, including all five versions
  • Easy-to-use API with clear documentation
  • Follows PHP Standards Recommendations (PSR) for coding style and autoloading
  • Actively maintained with regular updates and improvements

Cons

  • Requires PHP 7.4 or later, which may not be available on older systems
  • Some users may find the library overkill for simple UUID generation needs
  • Performance may be a concern for high-volume UUID generation scenarios

Code Examples

Generating a version 4 (random) UUID:

use Ramsey\Uuid\Uuid;

$uuid = Uuid::uuid4();
echo $uuid->toString(); // e.g., f47ac10b-58cc-4372-a567-0e02b2c3d479

Creating a version 5 (name-based) UUID using SHA-1:

use Ramsey\Uuid\Uuid;

$namespace = Uuid::NAMESPACE_DNS;
$name = 'example.com';
$uuid = Uuid::uuid5($namespace, $name);
echo $uuid->toString(); // e.g., 2ed6657d-e927-568b-95e1-2665a8aea6a2

Parsing and validating a UUID string:

use Ramsey\Uuid\Uuid;

$uuidString = 'f47ac10b-58cc-4372-a567-0e02b2c3d479';
if (Uuid::isValid($uuidString)) {
    $uuid = Uuid::fromString($uuidString);
    echo "UUID version: " . $uuid->getVersion();
}

Getting Started

To use ramsey/uuid in your PHP project, follow these steps:

  1. Install the library using Composer:

    composer require ramsey/uuid
    
  2. Include the Composer autoloader in your PHP script:

    require 'vendor/autoload.php';
    
  3. Use the library in your code:

    use Ramsey\Uuid\Uuid;
    
    $uuid = Uuid::uuid4();
    echo $uuid->toString();
    

That's it! You can now start generating and working with UUIDs in your PHP application.

Competitor Comparisons

Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

Pros of laravel-uuid

  • Specifically designed for Laravel, offering seamless integration
  • Provides Laravel-specific features like model traits and database migrations
  • Lightweight and focused on Laravel use cases

Cons of laravel-uuid

  • Less actively maintained compared to uuid
  • Limited functionality outside of Laravel ecosystem
  • Smaller community and fewer contributors

Code Comparison

laravel-uuid:

use Webpatser\Uuid\Uuid;

$uuid = Uuid::generate();
$model->uuid = $uuid;
$model->save();

uuid:

use Ramsey\Uuid\Uuid;

$uuid = Uuid::uuid4();
$model->uuid = $uuid->toString();
$model->save();

Summary

laravel-uuid is tailored for Laravel applications, offering easy integration and Laravel-specific features. However, it has a narrower scope and less active development compared to uuid.

uuid is a more comprehensive and widely-used UUID library with broader application support and a larger community. It provides more extensive functionality but may require additional setup for Laravel-specific use cases.

Choose laravel-uuid for quick Laravel integration and simplicity, or uuid for a more robust and versatile UUID solution across different PHP projects.

PHP 5.x support for random_bytes() and random_int()

Pros of random_compat

  • Focused specifically on providing a secure random number generator for older PHP versions
  • Lightweight and easy to integrate into existing projects
  • Actively maintained with regular updates and security patches

Cons of random_compat

  • Limited in scope compared to uuid, which offers more comprehensive UUID functionality
  • May require additional dependencies or configuration for certain features
  • Not as widely adopted as uuid in the PHP ecosystem

Code Comparison

random_compat:

$bytes = random_bytes(16);
$hex = bin2hex($bytes);

uuid:

use Ramsey\Uuid\Uuid;

$uuid = Uuid::uuid4();
$string = $uuid->toString();

Summary

While random_compat focuses on providing secure random number generation for older PHP versions, uuid offers a more comprehensive solution for generating and working with UUIDs. random_compat is lightweight and easy to integrate, but uuid provides a wider range of UUID-related functionality and is more widely adopted in the PHP community. The choice between the two depends on the specific requirements of your project and whether you need full UUID support or just secure random number generation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

ramsey/uuid

A PHP library for generating and working with UUIDs.

Source Code Download Package PHP Programming Language Read License Build Status Codecov Code Coverage Psalm Type Coverage

ramsey/uuid is a PHP library for generating and working with universally unique identifiers (UUIDs).

This project adheres to a code of conduct. By participating in this project and its community, you are expected to uphold this code.

Much inspiration for this library came from the Java and Python UUID libraries.

Installation

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require ramsey/uuid

Upgrading to Version 4

See the documentation for a thorough upgrade guide:

Documentation

Please see https://uuid.ramsey.dev for documentation, tips, examples, and frequently asked questions.

Contributing

Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.

Coordinated Disclosure

Keeping user information safe and secure is a top priority, and we welcome the contribution of external security researchers. If you believe you've found a security issue in software that is maintained in this repository, please read SECURITY.md for instructions on submitting a vulnerability report.

ramsey/uuid for Enterprise

Available as part of the Tidelift Subscription.

The maintainers of ramsey/uuid and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.

Copyright and License

The ramsey/uuid library is copyright © Ben Ramsey and licensed for use under the MIT License (MIT). Please see LICENSE for more information.