thamtech/yii2-uuid

Yii 2 UUID Helper

Installs: 73 813

Dependents: 4

Suggesters: 0

Security: 0

Stars: 24

Watchers: 2

Forks: 1

Open Issues: 1

Type:yii2-extension

v1.2.0 2019-06-15 19:00 UTC

This package is auto-updated.

Last update: 2020-09-15 18:46:33 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