From 965367c8c44fb2c47027d0ffe6d5eed08c6613bd Mon Sep 17 00:00:00 2001 From: Maik Penz Date: Tue, 5 Feb 2013 17:13:54 +0100 Subject: [PATCH 1/2] uuid provider and test --- src/Faker/Provider/Uuid.php | 50 ++++++++++++++++++++++++++++++++ test/Faker/Provider/UuidTest.php | 26 +++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/Faker/Provider/Uuid.php create mode 100644 test/Faker/Provider/UuidTest.php diff --git a/src/Faker/Provider/Uuid.php b/src/Faker/Provider/Uuid.php new file mode 100644 index 00000000..9fbf019e --- /dev/null +++ b/src/Faker/Provider/Uuid.php @@ -0,0 +1,50 @@ + $tmpVal) { + $byte[$tmpKey-1] = $tmpVal; + } + + // b2f + $tLo = ($byte[0] << 24) | ($byte[1] << 16) | ($byte[2] << 8) | $byte[3]; + $tMi = ($byte[4] << 8) | $byte[5]; + $tHi = ($byte[6] << 8) | $byte[7]; + $csLo = $byte[9]; + $csHi = $byte[8] & 0x3f | (1 << 7); + + // if needed to make it big endian + if (pack('L', 0x6162797A) == pack('N', 0x6162797A)) { + $tLo = (($tLo & 0x000000ff) << 24) | (($tLo & 0x0000ff00) << 8) + | (($tLo & 0x00ff0000) >> 8) | (($tLo & 0xff000000) >> 24); + $tMi = (($tMi & 0x00ff) << 8) | (($tMi & 0xff00) >> 8); + $tHi = (($tHi & 0x00ff) << 8) | (($tHi & 0xff00) >> 8); + } + + // apply version + $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..a4b6a95d --- /dev/null +++ b/test/Faker/Provider/UuidTest.php @@ -0,0 +1,26 @@ +assertTrue($this->isUuid($uuid)); + } + + public function testUuidExpectedSeed() + { + mt_srand(123); + $this->assertEquals("8eea2b29-832f-3633-985c-b2a257eabb5a", BaseProvider::uuid()); + $this->assertEquals("9424cf48-58b3-3b7d-b89f-c049e9f5d31f", 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); + } +} From dc94119d7b50aff8ab6f4d671ee67b667f488fcf Mon Sep 17 00:00:00 2001 From: Maik Penz Date: Tue, 5 Feb 2013 17:44:26 +0100 Subject: [PATCH 2/2] simplified uuid generation, fixed comments --- src/Faker/Provider/Uuid.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Faker/Provider/Uuid.php b/src/Faker/Provider/Uuid.php index 9fbf019e..d96f40f2 100644 --- a/src/Faker/Provider/Uuid.php +++ b/src/Faker/Provider/Uuid.php @@ -14,12 +14,9 @@ class Uuid extends \Faker\Provider\Base // Hash the seed and convert to a byte array $val = md5($seed, true); - $byte = array(); - foreach (unpack('C16', $val) as $tmpKey => $tmpVal) { - $byte[$tmpKey-1] = $tmpVal; - } + $byte = array_values(unpack('C16', $val)); - // b2f + // extract fields from byte array $tLo = ($byte[0] << 24) | ($byte[1] << 16) | ($byte[2] << 8) | $byte[3]; $tMi = ($byte[4] << 8) | $byte[5]; $tHi = ($byte[6] << 8) | $byte[7]; @@ -34,7 +31,7 @@ class Uuid extends \Faker\Provider\Base $tHi = (($tHi & 0x00ff) << 8) | (($tHi & 0xff00) >> 8); } - // apply version + // apply version number $tHi &= 0x0fff; $tHi |= (3 << 12);