├── .github └── FUNDING.yml ├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── composer.json ├── phpunit.xml ├── readme.md ├── src └── jeremykenedy │ └── Uuid │ ├── Uuid.php │ └── UuidServiceProvider.php └── tests ├── .gitkeep ├── bootstrap.php └── jeremykenedy └── Uuid └── UuidTest.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [jeremykenedy] 4 | patreon: jeremykenedy 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### OSX BOOGERS ### 2 | .DS_Store 3 | *._DS_Store 4 | ._.DS_Store 5 | *._ 6 | ._* 7 | ._.* 8 | 9 | ### WINDOWS BOOGERS ### 10 | Thumbs.db 11 | 12 | ### Sass ### 13 | #bower_components/* 14 | /.sass-cache/* 15 | .sass-cache 16 | 17 | ### SUBLIMETEXT BOOGERS ### 18 | *.sublime-workspace 19 | .phpintel/* 20 | /.phpintel/* 21 | *.php-temp 22 | 23 | ### PHPSTORM BOOGERS ### 24 | .idea/* 25 | /.idea/* 26 | 27 | ### DIFFERENT TYPE OF MASTER CONFIGS ### 28 | .env 29 | .env-dev 30 | .env-live 31 | .env.dev 32 | .env.production 33 | .env-* 34 | /config/blog.php 35 | /sql 36 | composer.lock 37 | 38 | ### ASSET EXCLUSIONS ### 39 | /ref 40 | /node_modules 41 | /public/css/** 42 | /public/js/** 43 | /public/build/** 44 | /public/docs/** 45 | /public/build/rev-manifest.json 46 | /public/js/app.js 47 | /storage/** 48 | /vendor 49 | Homestead.json 50 | Homestead.yaml 51 | 52 | ### Ignore Token Generationg Certificates 53 | /storage/oauth-private.key 54 | /storage/oauth-public.key 55 | storage/oauth-private.key 56 | storage/oauth-public.key 57 | **/storage/oauth-private.key 58 | **/storage/oauth-public.key -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | sudo: required 3 | dist: trusty 4 | group: edge 5 | 6 | php: 7 | - '7.2.5' 8 | - '7.3' 9 | 10 | sudo: false 11 | 12 | services: 13 | - mysql 14 | 15 | before_script: 16 | - curl -s http://getcomposer.org/installer | php 17 | - php composer.phar install 18 | 19 | before_install: 20 | - travis_retry composer self-update 21 | 22 | install: 23 | - travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest 24 | 25 | script: vendor/bin/phpunit --coverage-text 26 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at jeremykenedy@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 - 2023 Jeremy Kenedy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "jeremykenedy/uuid", 3 | "description" : "Laravel package to generate a UUID according to the RFC 4122 standard. UUID Versions 1, 3, 4 and 5 are supported. With MIT license.", 4 | "authors" : [ 5 | { 6 | "name" : "Christoph Kempen", 7 | "email" : "christoph@downsized.nl" 8 | }, 9 | { 10 | "name" : "Jeremy Kenedy", 11 | "email" : "jeremykenedy@gmail.com" 12 | } 13 | ], 14 | "keywords" : [ 15 | "UUID RFC4122" 16 | ], 17 | "homepage" : "https://github.com/jeremykenedy/laravel-uuid", 18 | "license" : [ 19 | "MIT" 20 | ], 21 | "require" : { 22 | "php" : ">=7.0.0" 23 | }, 24 | "require-dev" : { 25 | "phpunit/phpunit": "~6.0", 26 | "fzaninotto/faker": "~1.4" 27 | }, 28 | "autoload" : { 29 | "psr-0" : { 30 | "jeremykenedy\\Uuid" : "src/" 31 | } 32 | }, 33 | "extra": { 34 | "laravel": { 35 | "providers": [ 36 | "jeremykenedy\\Uuid\\UuidServiceProvider" 37 | ], 38 | "aliases": { 39 | "Uuid": "jeremykenedy\\Uuid\\Uuid" 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | ./src 14 | 15 | 16 | 17 | 18 | ./tests/ 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Laravel Uuid (Universally unique identifier) 2 | 3 | [![Total Downloads](https://poser.pugx.org/jeremykenedy/uuid/d/total.svg)](https://packagist.org/packages/jeremykenedy/uuid) 4 | [![StyleCI](https://github.styleci.io/repos/97753869/shield?branch=master)](https://github.styleci.io/repos/97753869) 5 | [![Build Status](https://travis-ci.org/jeremykenedy/laravel-uuid.svg?branch=master)](http://travis-ci.org/jeremykenedy/laravel-uuid) 6 | [![Latest Stable Version](https://poser.pugx.org/jeremykenedy/uuid/v/stable.svg)](https://packagist.org/packages/jeremykenedy/uuid) 7 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 8 | 9 | Laravel package to generate a UUID according to the RFC 4122 standard. UUID Versions 1, 3, 4 and 5 are supported. With MIT license. 10 | 11 | - [About](#about) 12 | - [Requirements](#requirements) 13 | - [Installation](#installation) 14 | - [Laravel 5.5 and above](#laravel-55-and-above) 15 | - [Laravel 5.4 and below](#laravel-54-and-below) 16 | - [Basic Usage](#basic-usage) 17 | - [Advanced Usage](#advanced-usage) 18 | - [UUID creation](#uuid-creation) 19 | - [UUID V1](#uuid-v1) 20 | - [UUID V3](#uuid-v3) 21 | - [UUID V4](#uuid-v4) 22 | - [UUID V5](#uuid-v5) 23 | - [Additional Features](#additional-features) 24 | - [Import UUID](#import-uuid) 25 | - [Extract time](#extract-time) 26 | - [Extract Version](#extract-version) 27 | - [Changelog](#changelog) 28 | - [2.*](#2.*) 29 | - [License](#license) 30 | - [Credits](#credits) 31 | 32 | ### About 33 | Since Laravel `4.*` and `5.*` both rely on either `OpenSSL` or `Mcrypt`, the pseudo random byte generator now tries to use one of them. If both cannot be used (not a Laravel project?), the 'less random' `mt_rand()` function is used. 34 | 35 | ### Requirements 36 | * [Laravel 5.3 - 10](https://laravel.com/docs/installation) 37 | 38 | ### Installation 39 | 40 | ##### Laravel 5.5 and above 41 | 1. From your projects root folder in terminal run: 42 | 43 | ```bash 44 | composer require jeremykenedy/uuid 45 | ``` 46 | 47 | * Uses package auto discovery feature, no need to edit the `config/app.php` file. 48 | 49 | ##### Laravel 5.4 and below 50 | 1. From your projects root folder in terminal run: 51 | 52 | ```bash 53 | composer require jeremykenedy/uuid:2.1.2 54 | ``` 55 | 56 | 2. Register the package with laravel in `config/app.php` under `aliases` with the following: 57 | 58 | ```php 59 | 'aliases' => [ 60 | 'Uuid' => jeremykenedy\Uuid\Uuid::class, 61 | ]; 62 | ``` 63 | 64 | ### Basic Usage 65 | 66 | To quickly generate a UUID just do 67 | 68 | ```php 69 | Uuid::generate() 70 | ``` 71 | * This will generate a version 1 with a random ganerated MAC address. 72 | 73 | ### Advanced Usage 74 | 75 | #### UUID creation 76 | 77 | ##### UUID V1 78 | Generate a version 1, time-based, UUID. You can set the optional node to the MAC address. If not supplied it will generate a random MAC address. 79 | 80 | ```php 81 | Uuid::generate(1,'00:11:22:33:44:55'); 82 | ``` 83 | 84 | ##### UUID V3 85 | Generate a version 3, name-based using MD5 hashing, UUID 86 | 87 | ```php 88 | Uuid::generate(3,'test', Uuid::NS_DNS); 89 | ``` 90 | 91 | ##### UUID V4 92 | Generate a version 4, truly random, UUID 93 | 94 | ```php 95 | Uuid::generate(4); 96 | ``` 97 | 98 | ##### UUID V5 99 | Generate a version 5, name-based using SHA-1 hashing, UUID 100 | 101 | ```php 102 | Uuid::generate(5,'test', Uuid::NS_DNS); 103 | ``` 104 | 105 | ### Additional Features 106 | ###### Import UUID 107 | * To import a UUID 108 | 109 | ```php 110 | $uuid = Uuid::import('d3d29d70-1d25-11e3-8591-034165a3a613'); 111 | ``` 112 | 113 | ###### Extract Time 114 | * Extract the time for a time-based UUID (version 1) 115 | 116 | ```php 117 | $uuid = Uuid::generate(1); 118 | dd($uuid->time); 119 | ``` 120 | 121 | ###### Extract Version 122 | * Extract the version of an UUID 123 | 124 | ```php 125 | $uuid = Uuid::generate(4); 126 | dd($uuid->version); 127 | ```` 128 | 129 | ### Changelog 130 | ##### 2.* 131 | * Laravel Uuid is now fully PSR-2, just like Laravel 5.1. 132 | * Not that much has changed except for UPPERCASING the constants used in Laravel Uuid. 133 | * Meaning `Uuid::nsDNS` is now `Uuid::NS_DNS` etc. Should be an easy fix. 134 | 135 | ### Credits 136 | * Full development credit must go to [webpatser](https://github.com/webpatser). This package was forked and modified to be compliant with [MIT](https://opensource.org/licenses/MIT) licensing standards for production use. 137 | 138 | ## License 139 | Laravel UUID is licensed under the MIT license for both personal and commercial products. Enjoy! 140 | -------------------------------------------------------------------------------- /src/jeremykenedy/Uuid/Uuid.php: -------------------------------------------------------------------------------- 1 | bytes = $uuid; 137 | 138 | // Optimize the most common use 139 | $this->string = bin2hex(substr($uuid, 0, 4)).'-'. 140 | bin2hex(substr($uuid, 4, 2)).'-'. 141 | bin2hex(substr($uuid, 6, 2)).'-'. 142 | bin2hex(substr($uuid, 8, 2)).'-'. 143 | bin2hex(substr($uuid, 10, 6)); 144 | } 145 | 146 | /** 147 | * @param int $ver 148 | * @param string $node 149 | * @param string $ns 150 | * 151 | * @throws Exception 152 | * 153 | * @return Uuid 154 | */ 155 | public static function generate($ver = 1, $node = null, $ns = null) 156 | { 157 | /* Create a new UUID based on provided data. */ 158 | switch ((int) $ver) { 159 | case 1: 160 | return new static(static::mintTime($node)); 161 | case 2: 162 | // Version 2 is not supported 163 | throw new Exception('Version 2 is unsupported.'); 164 | case 3: 165 | return new static(static::mintName(static::MD5, $node, $ns)); 166 | case 4: 167 | return new static(static::mintRand()); 168 | case 5: 169 | return new static(static::mintName(static::SHA1, $node, $ns)); 170 | default: 171 | throw new Exception('Selected version is invalid or unsupported.'); 172 | } 173 | } 174 | 175 | /** 176 | * Generates a Version 1 UUID. 177 | * These are derived from the time at which they were generated. 178 | * 179 | * @param string $node 180 | * 181 | * @return string 182 | */ 183 | protected static function mintTime($node = null) 184 | { 185 | 186 | /** Get time since Gregorian calendar reform in 100ns intervals 187 | * This is exceedingly difficult because of PHP's (and pack()'s) 188 | * integer size limits. 189 | * Note that this will never be more accurate than to the microsecond. 190 | */ 191 | $time = microtime(1) * 10000000 + static::INTERVAL; 192 | 193 | // Convert to a string representation 194 | $time = sprintf('%F', $time); 195 | 196 | //strip decimal point 197 | preg_match("/^\d+/", $time, $time); 198 | 199 | // And now to a 64-bit binary representation 200 | $time = base_convert($time[0], 10, 16); 201 | $time = pack('H*', str_pad($time, 16, '0', STR_PAD_LEFT)); 202 | 203 | // Reorder bytes to their proper locations in the UUID 204 | $uuid = $time[4].$time[5].$time[6].$time[7].$time[2].$time[3].$time[0].$time[1]; 205 | 206 | // Generate a random clock sequence 207 | $uuid .= static::randomBytes(2); 208 | 209 | // set variant 210 | $uuid[8] = chr(ord($uuid[8]) & static::CLEAR_VAR | static::VAR_RFC); 211 | 212 | // set version 213 | $uuid[6] = chr(ord($uuid[6]) & static::CLEAR_VER | static::VERSION_1); 214 | 215 | // Set the final 'node' parameter, a MAC address 216 | if (!is_null($node)) { 217 | $node = static::makeBin($node, 6); 218 | } 219 | 220 | // If no node was provided or if the node was invalid, 221 | // generate a random MAC address and set the multicast bit 222 | if (is_null($node)) { 223 | $node = static::randomBytes(6); 224 | $node[0] = pack('C', ord($node[0]) | 1); 225 | } 226 | 227 | $uuid .= $node; 228 | 229 | return $uuid; 230 | } 231 | 232 | /** 233 | * Randomness is returned as a string of bytes. 234 | * 235 | * @param $bytes 236 | * 237 | * @return string 238 | */ 239 | public static function randomBytes($bytes) 240 | { 241 | return random_bytes($bytes); 242 | } 243 | 244 | /** 245 | * Insure that an input string is either binary or hexadecimal. 246 | * Returns binary representation, or false on failure. 247 | * 248 | * @param string $str 249 | * @param int $len 250 | * 251 | * @return string|null 252 | */ 253 | protected static function makeBin($str, $len) 254 | { 255 | if ($str instanceof self) { 256 | return $str->bytes; 257 | } 258 | if (strlen($str) === $len) { 259 | return $str; 260 | } else { 261 | // strip URN scheme and namespace 262 | $str = preg_replace('/^urn:uuid:/is', '', $str); 263 | } 264 | // strip non-hex characters 265 | $str = preg_replace('/[^a-f0-9]/is', '', $str); 266 | if (strlen($str) !== ($len * 2)) { 267 | return null; 268 | } else { 269 | return pack('H*', $str); 270 | } 271 | } 272 | 273 | /** 274 | * Generates a Version 3 or Version 5 UUID. 275 | * These are derived from a hash of a name and its namespace, in binary form. 276 | * 277 | * @param string $ver 278 | * @param string $node 279 | * @param string $ns 280 | * 281 | * @throws Exception 282 | * 283 | * @return string 284 | */ 285 | protected static function mintName($ver, $node, $ns) 286 | { 287 | if (empty($node)) { 288 | throw new Exception('A name-string is required for Version 3 or 5 UUIDs.'); 289 | } 290 | 291 | // if the namespace UUID isn't binary, make it so 292 | $ns = static::makeBin($ns, 16); 293 | if (is_null($ns)) { 294 | throw new Exception('A binary namespace is required for Version 3 or 5 UUIDs.'); 295 | } 296 | 297 | $version = null; 298 | $uuid = null; 299 | 300 | switch ($ver) { 301 | case static::MD5: 302 | $version = static::VERSION_3; 303 | $uuid = md5($ns.$node, 1); 304 | break; 305 | case static::SHA1: 306 | $version = static::VERSION_5; 307 | $uuid = substr(sha1($ns.$node, 1), 0, 16); 308 | break; 309 | default: 310 | // no default really required here 311 | } 312 | 313 | // set variant 314 | $uuid[8] = chr(ord($uuid[8]) & static::CLEAR_VAR | static::VAR_RFC); 315 | 316 | // set version 317 | $uuid[6] = chr(ord($uuid[6]) & static::CLEAR_VER | $version); 318 | 319 | return $uuid; 320 | } 321 | 322 | /** 323 | * Generate a Version 4 UUID. 324 | * These are derived solely from random numbers. 325 | * generate random fields. 326 | * 327 | * @return string 328 | */ 329 | protected static function mintRand() 330 | { 331 | $uuid = static::randomBytes(16); 332 | // set variant 333 | $uuid[8] = chr(ord($uuid[8]) & static::CLEAR_VAR | static::VAR_RFC); 334 | // set version 335 | $uuid[6] = chr(ord($uuid[6]) & static::CLEAR_VER | static::VERSION_4); 336 | 337 | return $uuid; 338 | } 339 | 340 | /** 341 | * Import an existing UUID. 342 | * 343 | * @param string $uuid 344 | * 345 | * @return Uuid 346 | */ 347 | public static function import($uuid) 348 | { 349 | return new static(static::makeBin($uuid, 16)); 350 | } 351 | 352 | /** 353 | * Compares the binary representations of two UUIDs. 354 | * The comparison will return true if they are bit-exact, 355 | * or if neither is valid. 356 | * 357 | * @param string $a 358 | * @param string $b 359 | * 360 | * @return string|string 361 | */ 362 | public static function compare($a, $b) 363 | { 364 | if (static::makeBin($a, 16) == static::makeBin($b, 16)) { 365 | return true; 366 | } else { 367 | return false; 368 | } 369 | } 370 | 371 | /** 372 | * @param string $var 373 | * 374 | * @return string|string|number|number|number|number|number|null|number|null|null 375 | */ 376 | public function __get($var) 377 | { 378 | switch ($var) { 379 | case 'bytes': 380 | return $this->bytes; 381 | break; 382 | case 'hex': 383 | return bin2hex($this->bytes); 384 | break; 385 | case 'node': 386 | if (ord($this->bytes[6]) >> 4 == 1) { 387 | return bin2hex(substr($this->bytes, 10)); 388 | } else { 389 | return null; 390 | } 391 | break; 392 | case 'string': 393 | return $this->__toString(); 394 | break; 395 | case 'time': 396 | if (ord($this->bytes[6]) >> 4 == 1) { 397 | // Restore contiguous big-endian byte order 398 | $time = bin2hex($this->bytes[6].$this->bytes[7].$this->bytes[4].$this->bytes[5]. 399 | $this->bytes[0].$this->bytes[1].$this->bytes[2].$this->bytes[3]); 400 | // Clear version flag 401 | $time[0] = '0'; 402 | 403 | // Do some reverse arithmetic to get a Unix timestamp 404 | return (hexdec($time) - static::INTERVAL) / 10000000; 405 | } else { 406 | return null; 407 | } 408 | break; 409 | case 'urn': 410 | return 'urn:uuid:'.$this->__toString(); 411 | break; 412 | case 'variant': 413 | $byte = ord($this->bytes[8]); 414 | if ($byte >= static::VAR_RES) { 415 | return 3; 416 | } elseif ($byte >= static::VAR_MS) { 417 | return 2; 418 | } elseif ($byte >= static::VAR_RFC) { 419 | return 1; 420 | } else { 421 | return 0; 422 | } 423 | break; 424 | case 'version': 425 | return ord($this->bytes[6]) >> 4; 426 | break; 427 | default: 428 | return null; 429 | break; 430 | } 431 | } 432 | 433 | /** 434 | * Return the UUID. 435 | * 436 | * @return string 437 | */ 438 | public function __toString() 439 | { 440 | return $this->string; 441 | } 442 | 443 | /** 444 | * Import and validate an UUID. 445 | * 446 | * @param Uuid|string $uuid 447 | * 448 | * @return bool 449 | */ 450 | public static function validate($uuid) 451 | { 452 | return (bool) preg_match('~'.static::VALID_UUID_REGEX.'~', static::import($uuid)->string); 453 | } 454 | } 455 | -------------------------------------------------------------------------------- /src/jeremykenedy/Uuid/UuidServiceProvider.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('jeremykenedy\Uuid\Uuid', $uuid); 12 | 13 | $uuid = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 14 | $this->assertInstanceOf('jeremykenedy\Uuid\Uuid', $uuid); 15 | 16 | $uuid = Uuid::generate(4); 17 | $this->assertInstanceOf('jeremykenedy\Uuid\Uuid', $uuid); 18 | 19 | $uuid = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 20 | $this->assertInstanceOf('jeremykenedy\Uuid\Uuid', $uuid); 21 | } 22 | 23 | public function testImportAllZeroUuid() 24 | { 25 | $uuid = Uuid::import('00000000-0000-0000-0000-000000000000'); 26 | $this->assertInstanceOf('jeremykenedy\Uuid\Uuid', $uuid); 27 | $this->assertEquals('00000000-0000-0000-0000-000000000000', (string) $uuid); 28 | } 29 | 30 | public function testGenerationOfValidUuidViaRegex() 31 | { 32 | $uuid = Uuid::generate(1); 33 | $this->assertRegExp('~'.Uuid::VALID_UUID_REGEX.'~', (string) $uuid); 34 | 35 | $uuid = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 36 | $this->assertRegExp('~'.Uuid::VALID_UUID_REGEX.'~', (string) $uuid); 37 | 38 | $uuid = Uuid::generate(4); 39 | $this->assertRegExp('~'.Uuid::VALID_UUID_REGEX.'~', (string) $uuid); 40 | 41 | $uuid = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 42 | $this->assertRegExp('~'.Uuid::VALID_UUID_REGEX.'~', (string) $uuid); 43 | } 44 | 45 | public function testGenerationOfValidUuidViaValidator() 46 | { 47 | $uuid = Uuid::generate(1); 48 | $this->assertTrue(Uuid::validate($uuid->string)); 49 | 50 | $uuid = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 51 | $this->assertTrue(Uuid::validate($uuid->string)); 52 | 53 | $uuid = Uuid::generate(4); 54 | $this->assertTrue(Uuid::validate($uuid->string)); 55 | 56 | $uuid = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 57 | $this->assertTrue(Uuid::validate($uuid->string)); 58 | 59 | $uuid = Uuid::generate(1); 60 | $this->assertTrue(Uuid::validate($uuid->bytes)); 61 | 62 | $uuid = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 63 | $this->assertTrue(Uuid::validate($uuid->bytes)); 64 | 65 | $uuid = Uuid::generate(4); 66 | $this->assertTrue(Uuid::validate($uuid->bytes)); 67 | 68 | $uuid = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 69 | $this->assertTrue(Uuid::validate($uuid->bytes)); 70 | 71 | $uuid = Uuid::generate(1); 72 | $this->assertTrue(Uuid::validate($uuid->urn)); 73 | 74 | $uuid = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 75 | $this->assertTrue(Uuid::validate($uuid->urn)); 76 | 77 | $uuid = Uuid::generate(4); 78 | $this->assertTrue(Uuid::validate($uuid->urn)); 79 | 80 | $uuid = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 81 | $this->assertTrue(Uuid::validate($uuid->urn)); 82 | 83 | $this->assertTrue(Uuid::validate(Uuid::generate(1))); 84 | 85 | $this->assertTrue(Uuid::validate(Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS))); 86 | 87 | $this->assertTrue(Uuid::validate(Uuid::generate(4))); 88 | 89 | $this->assertTrue(Uuid::validate(Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS))); 90 | } 91 | 92 | public function testCorrectVersionUuid() 93 | { 94 | $uuidOne = Uuid::generate(1); 95 | $this->assertEquals(1, $uuidOne->version); 96 | 97 | $uuidThree = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 98 | $this->assertEquals(3, $uuidThree->version); 99 | 100 | $uuidFour = Uuid::generate(4); 101 | $this->assertEquals(4, $uuidFour->version); 102 | 103 | $uuidFive = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 104 | $this->assertEquals(5, $uuidFive->version); 105 | } 106 | 107 | public function testCorrectVariantUuid() 108 | { 109 | $uuidOne = Uuid::generate(1); 110 | $this->assertEquals(1, $uuidOne->variant); 111 | 112 | $uuidThree = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 113 | $this->assertEquals(1, $uuidThree->variant); 114 | 115 | $uuidFour = Uuid::generate(4); 116 | $this->assertEquals(1, $uuidFour->variant); 117 | 118 | $uuidFive = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 119 | $this->assertEquals(1, $uuidFive->variant); 120 | } 121 | 122 | public function testCorrectVersionOfImportedUuid() 123 | { 124 | $uuidOne = Uuid::generate(1); 125 | $importedOne = Uuid::import((string) $uuidOne); 126 | $this->assertEquals($uuidOne->version, $importedOne->version); 127 | 128 | $uuidThree = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 129 | $importedThree = Uuid::import((string) $uuidThree); 130 | $this->assertEquals($uuidThree->version, $importedThree->version); 131 | 132 | $uuidFour = Uuid::generate(4); 133 | $importedFour = Uuid::import((string) $uuidFour); 134 | $this->assertEquals($uuidFour->version, $importedFour->version); 135 | 136 | $uuidFive = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 137 | $importedFive = Uuid::import((string) $uuidFive); 138 | $this->assertEquals($uuidFive->version, $importedFive->version); 139 | } 140 | 141 | public function testCorrectNodeOfGeneratedUuid() 142 | { 143 | $macAdress = Faker\Provider\Internet::macAddress(); 144 | $uuidThree = Uuid::generate(1, $macAdress); 145 | $this->assertEquals(strtolower(str_replace(':', '', $macAdress)), $uuidThree->node); 146 | 147 | $uuidThree = Uuid::generate(3, $macAdress, Uuid::NS_DNS); 148 | $this->assertNull($uuidThree->node); 149 | 150 | $uuidThree = Uuid::generate(4, $macAdress); 151 | $this->assertNull($uuidThree->node); 152 | 153 | $uuidThree = Uuid::generate(5, $macAdress, Uuid::NS_DNS); 154 | $this->assertNull($uuidThree->node); 155 | } 156 | 157 | public function testCorrectTimeOfImportedUuid() 158 | { 159 | $uuidOne = Uuid::generate(1); 160 | $importedOne = Uuid::import((string) $uuidOne); 161 | $this->assertEquals($uuidOne->time, $importedOne->time); 162 | 163 | $uuidThree = Uuid::generate(3, 'jeremykenedy.com', Uuid::NS_DNS); 164 | $importedThree = Uuid::import((string) $uuidThree); 165 | $this->assertEmpty($importedThree->time); 166 | 167 | $uuidFour = Uuid::generate(4); 168 | $importedFour = Uuid::import((string) $uuidFour); 169 | $this->assertEmpty($importedFour->time); 170 | 171 | $uuidFive = Uuid::generate(5, 'jeremykenedy.com', Uuid::NS_DNS); 172 | $importedFive = Uuid::import((string) $uuidFive); 173 | $this->assertEmpty($importedFive->time); 174 | } 175 | 176 | public function testUuidCompare() 177 | { 178 | $uuid1 = (string) Uuid::generate(1); 179 | $uuid2 = (string) Uuid::generate(1); 180 | 181 | $this->assertTrue(Uuid::compare($uuid1, $uuid1)); 182 | $this->assertFalse(Uuid::compare($uuid1, $uuid2)); 183 | } 184 | } 185 | --------------------------------------------------------------------------------