thamtech / yii2-uuid
Yii 2 UUID Helper
Fund package maintenance!
Liberapay
Installs: 254 507
Dependents: 7
Suggesters: 0
Security: 0
Stars: 32
Watchers: 3
Forks: 0
Open Issues: 2
Type:yii2-extension
Requires
- php: >=5.6.0
- ramsey/uuid: ^3 || ^4
- yiisoft/yii2: >=2.0.14 <2.1
Requires (Dev)
- phpunit/phpunit: ^4.1 || ^5.0
This package is auto-updated.
Last update: 2024-10-30 01:31:46 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.
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'], ]; }