/** * 指定のプロパティにユニークコードをセットする * auto_code_addアノテーションで呼ばれる * * @param string $prop_name * @param integer $size * @return string 生成されたユニークコード */ public function set_unique_code($prop_name, $size = null) { /** * ユニークコードで利用する文字 * * @param string $base ex. ABCDEFGHJKLMNPQRSTUWXY0123456789 */ $base = $this->prop_anon($prop_name, 'base'); if (empty($base)) { /** * ユニークコードで利用する文字パターン * unique_code_baseが未定義の場合のみ有効 * * 0: 数字 0123456789 * a: 小文字 abcdefghjkmnprstuvwxy * A: 大文字 ABCDEFGHJKLMNPQRSTUVWXY * * @param string $unique_code_ctype 0aAの組み合わせ */ $ctype = $this->prop_anon($prop_name, 'ctype', '0a'); if (strpos($ctype, 'A') !== false) { $base .= 'ABCDEFGHJKLMNPQRSTUVWXY'; } if (strpos($ctype, 'a') !== false) { $base .= 'abcdefghjkmnprstuvwxy'; } if (strpos($ctype, '0') !== false) { $base .= '0123456789'; } if (empty($base)) { throw new \ebi\exception\IllegalDataTypeException('unexpected ctype'); } } $code = ''; $challenge = 0; $challenge_max = 10; $bool = true; $vefify_func = method_exists($this, '__verify_' . $prop_name . '__'); $prefix = ''; $length = !empty($size) ? $size : $this->prop_anon($prop_name, 'length'); if (empty($length)) { $length = $this->prop_anon($prop_name, 'max', 32); } if (method_exists($this, '__prefix_' . $prop_name . '__')) { $prefix = call_user_func_array([$this, '__prefix_' . $prop_name . '__'], [$base]); $length = $length - strlen($prefix); } while ($code == '') { for ($i = 0; $i <= $challenge_max; $i++) { $code = $prefix . \ebi\Code::rand($base, $length); call_user_func_array([$this, $prop_name], [$code]); if ((!$vefify_func || call_user_func([$this, '__verify_' . $prop_name . '__']) !== false) && static::find_count(Q::eq($prop_name, $code)) === 0) { break 2; } } if ($challenge++ > $challenge_max) { throw new \ebi\exception\GenerateUniqueCodeRetryLimitOverException($prop_name . ': generate unique code retry limit over'); } usleep(1000); $code = ''; } return $code; }
public function __prefix_code__($codebase) { $time = time(); return \ebi\Code::encode($codebase, date('Y', $time) - 1) . \ebi\Code::encode($codebase, date('m', $time) - 1) . \ebi\Code::encode($codebase, date('d', $time) - 1) . \ebi\Code::encode($codebase, date('H', $time)); }
<?php $codebase = '0123456789ABC'; $max = \ebi\Code::max($codebase, 5); $maxcode = \ebi\Code::encode($codebase, $max); eq('CCCCC', $maxcode); eq($max, \ebi\Code::decode($codebase, $maxcode)); $min = \ebi\Code::min($codebase, 5); $mincode = \ebi\Code::encode($codebase, $min); eq('10000', $mincode); eq($min, \ebi\Code::decode($codebase, $mincode)); eq(3, strlen(\ebi\Code::rand($codebase, 3))); eq(40, strlen(\ebi\Code::rand($codebase, 40))); eq('100110', \ebi\Code::encode('0123456789', 100110)); eq(100110, \ebi\Code::decode('0123456789', '100110'));
<?php \test\db\AutoCodePrefix::create_table(); \test\db\AutoCodePrefix::find_delete(); $codebase = 'abcdefghjkmnprstuvwxy0123456789'; $time = time(); $code = \ebi\Code::encode($codebase, date('Y', $time) - 1) . \ebi\Code::encode($codebase, date('m', $time) - 1) . \ebi\Code::encode($codebase, date('d', $time) - 1) . \ebi\Code::encode($codebase, date('H', $time)); $obj = new \test\db\AutoCodePrefix(); $obj->save(); eq($code, substr($obj->code(), 0, 6));