diff --git a/readme.md b/readme.md index 8fe80a4c..b2259910 100644 --- a/readme.md +++ b/readme.md @@ -173,6 +173,10 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle opera // 'Opera/8.25 (Windows NT 5.1; en-US) Presto/2.9.188 Version/10.00' internetExplorer // 'Mozilla/5.0 (compatible; MSIE 7.0; Windows 98; Win 9x 4.90; Trident/3.0)' +### `Faker\Provider\Uuid` + + uuid // '7e57d004-2b97-0e7a-b45f-5387367791cd' + ## Localization `Faker\Factory` can take a locale as an argument, to return localized data. If no localized provider is found, the factory fallbacks to the default locale (en_EN). diff --git a/src/Faker/Factory.php b/src/Faker/Factory.php index ec19aa2d..6a9d4fc4 100644 --- a/src/Faker/Factory.php +++ b/src/Faker/Factory.php @@ -6,7 +6,7 @@ class Factory { const DEFAULT_LOCALE = 'en_US'; - protected static $defaultProviders = array('Person', 'Address', 'PhoneNumber', 'Company', 'Lorem', 'Internet', 'DateTime', 'Miscellaneous', 'UserAgent'); + protected static $defaultProviders = array('Person', 'Address', 'PhoneNumber', 'Company', 'Lorem', 'Internet', 'DateTime', 'Miscellaneous', 'UserAgent', 'Uuid'); public static function create($locale = self::DEFAULT_LOCALE) { diff --git a/src/Faker/Provider/Uuid.php b/src/Faker/Provider/Uuid.php new file mode 100644 index 00000000..eb6cf7b4 --- /dev/null +++ b/src/Faker/Provider/Uuid.php @@ -0,0 +1,48 @@ +> 8) | (($tLo & 0xff000000) >> 24); + $tMi = (($tMi & 0x00ff) << 8) | (($tMi & 0xff00) >> 8); + $tHi = (($tHi & 0x00ff) << 8) | (($tHi & 0xff00) >> 8); + } + + // apply version number + $tHi &= 0x0fff; + $tHi |= (3 << 12); + + // cast to string + $uuid = sprintf( + '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x', + $tLo, $tMi, $tHi, $csHi, $csLo, + $byte[10], $byte[11], $byte[12], $byte[13], $byte[14], $byte[15] + ); + + return $uuid; + } +} diff --git a/test/Faker/Provider/UuidTest.php b/test/Faker/Provider/UuidTest.php new file mode 100644 index 00000000..fceb8dfe --- /dev/null +++ b/test/Faker/Provider/UuidTest.php @@ -0,0 +1,26 @@ +assertTrue($this->isUuid($uuid)); + } + + public function testUuidExpectedSeed() + { + mt_srand(123); + $this->assertEquals("8e2e0c84-50dd-367c-9e66-f3ab455c78d6", BaseProvider::uuid()); + $this->assertEquals("073eb60a-902c-30ab-93d0-a94db371f6c8", BaseProvider::uuid()); + } + + protected function isUuid($uuid) + { + return is_string($uuid) && (bool) preg_match('/^[a-f0-9]{8,8}-(?:[a-f0-9]{4,4}-){3,3}[a-f0-9]{12,12}$/i', $uuid); + } +}