Skip to content

Commit 87a7365

Browse files
committed
Merge pull request #11 from ericthelin/feature-is-valid
Merging in pull request to create an isValid method
2 parents 901febf + 77d2aa5 commit 87a7365

File tree

2 files changed

+129
-4
lines changed

2 files changed

+129
-4
lines changed

src/Rhumsaa/Uuid/Uuid.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,12 @@ public function getUrn()
719719
*/
720720
public function getVariant()
721721
{
722-
if (0 === ($this->getClockSeqHiAndReserved() & 0x80)) {
722+
$clockSeq = $this->getClockSeqHiAndReserved();
723+
if (0 === ($clockSeq & 0x80)) {
723724
$variant = self::RESERVED_NCS;
724-
} elseif (0 === ($this->getClockSeqHiAndReserved() & 0x40)) {
725+
} elseif (0 === ($clockSeq & 0x40)) {
725726
$variant = self::RFC_4122;
726-
} elseif (0 === ($this->getClockSeqHiAndReserved() & 0x20)) {
727+
} elseif (0 === ($clockSeq & 0x20)) {
727728
$variant = self::RESERVED_MICROSOFT;
728729
} else {
729730
$variant = self::RESERVED_FUTURE;
@@ -778,7 +779,7 @@ public static function fromString($name)
778779
$name = str_replace(array('urn:', 'uuid:', '{', '}'), '', $name);
779780
$components = explode('-', $name);
780781

781-
if (count($components) != 5) {
782+
if (!self::isValid($name)) {
782783
throw new \InvalidArgumentException('Invalid UUID string: ' . $name);
783784
}
784785

@@ -794,6 +795,22 @@ public static function fromString($name)
794795
return new self($fields);
795796
}
796797

798+
/**
799+
* Check if a string is a valid uuid
800+
*
801+
* @param string $uuid The uuid to test
802+
* @return boolean
803+
*/
804+
public static function isValid($uuid)
805+
{
806+
$uuid = str_replace(array('urn:', 'uuid:', '{', '}'), '', $uuid);
807+
808+
if (!preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[1-5][0-9A-Fa-f]{3}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $uuid)) {
809+
return false;
810+
}
811+
return true;
812+
}
813+
797814
/**
798815
* Generate a version 1 UUID from a host ID, sequence number, and the current time.
799816
* If $node is not given, getMacAddress() is used to obtain the hardware

tests/Rhumsaa/Uuid/UuidTest.php

+108
Original file line numberDiff line numberDiff line change
@@ -1226,4 +1226,112 @@ public function testIs64BitSystem()
12261226
Uuid::$force32Bit = true;
12271227
$this->assertFalse($is64BitSystem->invoke($uuid));
12281228
}
1229+
1230+
/**
1231+
* @covers Rhumsaa\Uuid\Uuid::isValid
1232+
*/
1233+
public function testIsValidGoodVersion1()
1234+
{
1235+
$valid = Uuid::isValid('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
1236+
$this->assertTrue($valid);
1237+
}
1238+
1239+
/**
1240+
* @covers Rhumsaa\Uuid\Uuid::isValid
1241+
*/
1242+
public function testIsValidGoodVersion2()
1243+
{
1244+
$valid = Uuid::isValid('ff6f8cb0-c57d-21e1-9b21-0800200c9a66');
1245+
$this->assertTrue($valid);
1246+
}
1247+
1248+
/**
1249+
* @covers Rhumsaa\Uuid\Uuid::isValid
1250+
*/
1251+
public function testIsValidGoodVersion3()
1252+
{
1253+
$valid = Uuid::isValid('ff6f8cb0-c57d-31e1-9b21-0800200c9a66');
1254+
$this->assertTrue($valid);
1255+
}
1256+
1257+
/**
1258+
* @covers Rhumsaa\Uuid\Uuid::isValid
1259+
*/
1260+
public function testIsValidGoodVersion4()
1261+
{
1262+
$valid = Uuid::isValid('ff6f8cb0-c57d-41e1-9b21-0800200c9a66');
1263+
$this->assertTrue($valid);
1264+
}
1265+
1266+
/**
1267+
* @covers Rhumsaa\Uuid\Uuid::isValid
1268+
*/
1269+
public function testIsValidGoodVersion5()
1270+
{
1271+
$valid = Uuid::isValid('ff6f8cb0-c57d-51e1-9b21-0800200c9a66');
1272+
$this->assertTrue($valid);
1273+
}
1274+
1275+
/**
1276+
* @covers Rhumsaa\Uuid\Uuid::isValid
1277+
*/
1278+
public function testIsValidBadVersion6()
1279+
{
1280+
$valid = Uuid::isValid('ff6f8cb0-c57d-61e1-9b21-0800200c9a66');
1281+
$this->assertFalse($valid);
1282+
}
1283+
1284+
/**
1285+
* @covers Rhumsaa\Uuid\Uuid::isValid
1286+
*/
1287+
public function testIsValidGoodUpperCase()
1288+
{
1289+
$valid = Uuid::isValid('FF6F8CB0-C57D-11E1-9B21-0800200C9A66');
1290+
$this->assertTrue($valid);
1291+
}
1292+
1293+
/**
1294+
* @covers Rhumsaa\Uuid\Uuid::isValid
1295+
*/
1296+
public function testIsValidBadHex()
1297+
{
1298+
$valid = Uuid::isValid('zf6f8cb0-c57d-11e1-9b21-0800200c9a66');
1299+
$this->assertFalse($valid);
1300+
}
1301+
1302+
/**
1303+
* @covers Rhumsaa\Uuid\Uuid::isValid
1304+
*/
1305+
public function testIsValidTooShort1()
1306+
{
1307+
$valid = Uuid::isValid('3f6f8cb0-c57d-11e1-9b21-0800200c9a6');
1308+
$this->assertFalse($valid);
1309+
}
1310+
1311+
/**
1312+
* @covers Rhumsaa\Uuid\Uuid::isValid
1313+
*/
1314+
public function testIsValidTooShort2()
1315+
{
1316+
$valid = Uuid::isValid('af6f8cb-c57d-11e1-9b21-0800200c9a66');
1317+
$this->assertFalse($valid);
1318+
}
1319+
1320+
/**
1321+
* @covers Rhumsaa\Uuid\Uuid::isValid
1322+
*/
1323+
public function testIsValidNoDashes()
1324+
{
1325+
$valid = Uuid::isValid('af6f8cb0c57d11e19b210800200c9a66');
1326+
$this->assertFalse($valid);
1327+
}
1328+
1329+
/**
1330+
* @covers Rhumsaa\Uuid\Uuid::isValid
1331+
*/
1332+
public function testIsValidTooLong()
1333+
{
1334+
$valid = Uuid::isValid('ff6f8cb0-c57da-51e1-9b21-0800200c9a66');
1335+
$this->assertFalse($valid);
1336+
}
12291337
}

0 commit comments

Comments
 (0)