|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Shoperti. |
| 5 | + * |
| 6 | + * (c) Joseph Cohen <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Shoperti\Ciud; |
| 13 | + |
| 14 | +/** |
| 15 | + * This is the Ciud class. |
| 16 | + * |
| 17 | + * @author Joseph Cohen <[email protected]> |
| 18 | + */ |
| 19 | +class CiudFactory |
| 20 | +{ |
| 21 | + /** |
| 22 | + * The base string constant. |
| 23 | + * |
| 24 | + * @return int |
| 25 | + */ |
| 26 | + const BASE = 36; |
| 27 | + |
| 28 | + /** |
| 29 | + * The block size constant. |
| 30 | + * |
| 31 | + * @return int |
| 32 | + */ |
| 33 | + const BLOCK_SIZE = 4; |
| 34 | + |
| 35 | + /** |
| 36 | + * The counter. |
| 37 | + * |
| 38 | + * @return int |
| 39 | + */ |
| 40 | + protected $counter = 0; |
| 41 | + |
| 42 | + /** |
| 43 | + * Generates discrete values. |
| 44 | + * |
| 45 | + * @return string |
| 46 | + */ |
| 47 | + private $discreteValues; |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new ciud factory instance. |
| 51 | + * |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function __construct() |
| 55 | + { |
| 56 | + $this->discreteValues = pow(self::BASE, self::BLOCK_SIZE); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Pads the input string into specific size |
| 61 | + * |
| 62 | + * @param string $str |
| 63 | + * @param int $size |
| 64 | + * |
| 65 | + * @return string |
| 66 | + */ |
| 67 | + protected function pad($str, $size) |
| 68 | + { |
| 69 | + $str = '000000000'.$str; |
| 70 | + |
| 71 | + return substr($str, strlen($str) - $size); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Generates a random number. |
| 76 | + * |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + protected function random() |
| 80 | + { |
| 81 | + return mt_rand() / mt_getrandmax(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates a random block. |
| 86 | + * |
| 87 | + * @return string |
| 88 | + */ |
| 89 | + protected function randomBlock() |
| 90 | + { |
| 91 | + return $this->pad( |
| 92 | + base_convert((int) ($this->random() * $this->discreteValues << 0), 10, self::BASE), |
| 93 | + self::BLOCK_SIZE |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets a safe counter. |
| 99 | + * |
| 100 | + * @return int |
| 101 | + */ |
| 102 | + protected function safeCounter() |
| 103 | + { |
| 104 | + $this->counter = ($this->counter < $this->discreteValues) ? $this->counter : 0; |
| 105 | + ++$this->counter; |
| 106 | + return $this->counter - 1; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Gets a utf8 char code. |
| 111 | + * |
| 112 | + * @param string $str |
| 113 | + * |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + protected function charCodeAt($str) { |
| 117 | + list(, $ord) = unpack('N', mb_convert_encoding($str, 'UCS-4BE', 'UTF-8')); |
| 118 | + return $ord; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Generates a unique fingerprint based on the host. |
| 123 | + * |
| 124 | + * @return string |
| 125 | + */ |
| 126 | + protected function fingerprint() |
| 127 | + { |
| 128 | + $padding = 2; |
| 129 | + |
| 130 | + $pid = $this->pad( |
| 131 | + base_convert((int) getmypid(), 10, 36), $padding |
| 132 | + ); |
| 133 | + |
| 134 | + $hostname = gethostname(); |
| 135 | + $length = strlen($hostname); |
| 136 | + |
| 137 | + $hostId = $this->pad( |
| 138 | + base_convert((int) array_reduce(str_split($hostname), function ($prev, $char) { |
| 139 | + return +$prev + $this->charCodeAt($char); |
| 140 | + }, +$length + 36), 10, 36), |
| 141 | + $padding |
| 142 | + ); |
| 143 | + |
| 144 | + return $pid.$hostId; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Generates a new ciud. |
| 149 | + * |
| 150 | + * @param string|null $prefix |
| 151 | + * |
| 152 | + * @return string |
| 153 | + */ |
| 154 | + public function ciud($prefix = null) |
| 155 | + { |
| 156 | + // Starting with a lowercase letter makes |
| 157 | + // it HTML element ID friendly. |
| 158 | + $letter = $prefix ?: 'c'; // hard-coded allows for sequential access |
| 159 | + |
| 160 | + // timestamp |
| 161 | + // warning: this exposes the exact date and time |
| 162 | + // that the uid was created. |
| 163 | + $timestamp = base_convert(substr(microtime(true)*1000, 0, 13), 10, self::BASE); |
| 164 | + |
| 165 | + // A few chars to generate distinct ids for different |
| 166 | + // clients (so different computers are far less |
| 167 | + // likely to generate the same id) |
| 168 | + $fingerprint = $this->fingerprint(); |
| 169 | + |
| 170 | + // Grab some more chars from Math.random() |
| 171 | + $random = $this->randomBlock().$this->randomBlock(); |
| 172 | + |
| 173 | + // Prevent same-machine collisions. |
| 174 | + $counter = $this->pad(base_convert((int) $this->safeCounter(), 10, self::BASE), self::BLOCK_SIZE); |
| 175 | + |
| 176 | + return ($letter.$timestamp.$counter.$fingerprint.$random); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Generates a new slug string. |
| 181 | + * |
| 182 | + * @return string |
| 183 | + */ |
| 184 | + public function slug() |
| 185 | + { |
| 186 | + $timestamp = base_convert(substr(microtime(true)*1000, 0, 13), 10, self::BASE); |
| 187 | + $counter = substr(base_convert((int) $this->safeCounter(), 10, self::BASE), -4); |
| 188 | + $fingerprint = substr($this->fingerprint(), 0 ,1).substr($this->fingerprint(), -1); |
| 189 | + $random = substr($this->randomBlock(), -2); |
| 190 | + return (substr($timestamp, -2).$counter.$fingerprint.$random); |
| 191 | + } |
| 192 | +} |
0 commit comments