thamtech/yii2-uuid

Yii 2 UUID Helper

Fund package maintenance!
Liberapay

Installs: 258 709

Dependents: 7

Suggesters: 0

Security: 0

Stars: 33

Watchers: 3

Forks: 0

Open Issues: 2

Type:yii2-extension

v1.2.2 2021-01-20 03:05 UTC

This package is auto-updated.

Last update: 2024-11-30 01:47:42 UTC


README

UUID Helper and validator for Yii 2.

This library interfaces with ramsey/uuid to generate universally unique identifiers.

For license information check the LICENSE-file.

Latest Stable Version Build Status Scrutinizer Code Quality Code Coverage

Installation

The preferred way to install this extensions is through composer.

Either run

php composer.phar require --prefer-dist thamtech/yii2-uuid

or add

"thamtech/yii2-uuid": "*"

to the require section of your composer.json file.

Usage

New UUID

Generate a new UUID (version 4 by default):

$uuid = \thamtech\uuid\helpers\UuidHelper::uuid();

Ad-Hoc Validation

Validate that a string is formatted in the canonical format using hexadecimal text with inserted hyphen characters (case insensitive):

$uuid = 'de305d54-75b4-431b-adb2-eb6b9e546014';
$isValid = \thamtech\uuid\helpers\UuidHelper::isValid($uuid); // true

$uuid = 'not-a-uuid';
$isValid = \thamtech\uuid\helpers\UuidHelper::isValid($uuid); // false

// or using the Validator class directly
$validator = new \thamtech\uuid\validators\UuidValidator();
if ($validator->validate($uuid, $error)) {
    // valid
} else {
    // not valid
    echo $error
}

Or you can include the use lines, especially if you will be making multiple uuid calls within a file:

use thamtech\uuid\helpers\UuidHelper;
use thamtech\uuid\helpers\UuidValidator;

// ...

$uuid = 'de305d54-75b4-431b-adb2-eb6b9e546014';
$isValid = UuidHelper::isValid($uuid); // true

$uuid = 'not-a-uuid';
$isValid = UuidHelper::isValid($uuid); // false

// or using the Validator class directly
$validator = new UuidValidator();
if ($validator->validate($uuid, $error)) {
    // valid
} else {
    // not valid
    echo $error
}

Field Validation

Incorporate this same validation into your model:

public function rules()
{
    return [
        [['uuid'], 'thamtech\uuid\validators\UuidValidator'],
    ];
}

See Also