<?php
namespace app\helpers;

use yii\helpers\ArrayHelper;

class Uuid
{
    //43a17aae-aa70-4da0-a82e-6c258646a693
    //28fc1384-6132-4686-b200-a3c564f652c9

    public static function GetUUID():string
    {
		$bytes = random_bytes(16);
        $bytes[6] = chr((ord($bytes[6]) & 0x0f) | 0x40);
        $bytes[8] = chr((ord($bytes[8]) & 0x3f) | 0x80);
        $hex = bin2hex($bytes);
        $fields = [
            'time_low' => substr($hex, 0, 8),
            'time_mid' => substr($hex, 8, 4),
            'time_hi_and_version' => substr($hex, 12, 4),
            'clock_seq_hi_and_reserved' => substr($hex, 16, 2),
            'clock_seq_low' => substr($hex, 18, 2),
            'node' => substr($hex, 20, 12),
        ];
        return vsprintf(
            '%08s-%04s-%04s-%02s%02s-%012s',
            $fields
        );
    }

    public static function isvalid(string $string):string
    {
		if( preg_match('/^([a-z0-9]){8}\-([a-z0-9]){4}\-([a-z0-9]){4}\-([a-z0-9]){4}\-([a-z0-9]){12}$/', $string) ){
			return true;
		}
		return false;
    }

}
