/src/PHPCR/Util/UUIDHelper.php
PHP | 75 lines | 22 code | 9 blank | 44 comment | 1 complexity | f165539bb7b1ad021ba5e395257b7263 MD5 | raw file
- <?php
- /**
- * This file is part of the PHPCR API and was originally ported from the Java
- * JCR API to PHP by Karsten Dambekalns for the FLOW3 project.
- *
- * Copyright 2008-2011 Karsten Dambekalns <karsten@typo3.org>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0
- * @link http://phpcr.github.com/
- */
- namespace PHPCR\Util;
- /**
- * static helper functions to deal with UUID's
- */
- class UUIDHelper
- {
- /**
- * Checks if the string could be a uuid.
- *
- * @param string $id Possible uuid
- * @return boolean True if the test was passed, else false.
- */
- public static function isUUID($id)
- {
- // UUID is HEX_CHAR{8}-HEX_CHAR{4}-HEX_CHAR{4}-HEX_CHAR{4}-HEX_CHAR{12}
- if (1 === preg_match('/^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}$/', $id)) {
- return true;
- }
- return false;
- }
- /**
- * Generates a UUID.
- *
- * @return string
- */
- public static function generateUUID()
- {
- return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- // 32 bits for "time_low"
- mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
- // 16 bits for "time_mid"
- mt_rand( 0, 0xffff ),
- // 16 bits for "time_hi_and_version",
- // four most significant bits holds version number 4
- mt_rand( 0, 0x0fff ) | 0x4000,
- // 16 bits, 8 bits for "clk_seq_hi_res",
- // 8 bits for "clk_seq_low",
- // two most significant bits holds zero and one for variant DCE1.1
- mt_rand( 0, 0x3fff ) | 0x8000,
- // 48 bits for "node"
- mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
- );
- }
- }