Last active
December 19, 2017 15:53
-
-
Save florianajir/ca1f631682c9dfe91ff5 to your computer and use it in GitHub Desktop.
Generate random identifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| const SKU_LENGTH = 24; | |
| const PREFIX_SEPARATOR = '-'; | |
| public function createSku($prefix) | |
| { | |
| $prefix .= self::PREFIX_SEPARATOR; | |
| $randLength = self::SKU_LENGTH - count($prefix); | |
| $sku = $prefix . $this->generateRandom($missingLength); | |
| return $sku; | |
| } | |
| private function generateRandom($length) | |
| { | |
| if (!function_exists( 'openssl_random_pseudo_bytes' )) { | |
| throw new Exception('missing OpenSSL extension'); | |
| } | |
| $bytes = openssl_random_pseudo_bytes($length); | |
| $hex = bin2hex($bytes); | |
| return $hex; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment