├── .gitignore
├── .idea
├── .gitignore
├── misc.xml
├── modules.xml
├── php.xml
├── think-generator.iml
└── vcs.xml
├── LICENSE
├── README.md
├── composer.json
└── src
└── generator
├── BaseUtil.php
├── Generator.php
├── Helper.php
├── data
├── cities.json
├── name.json
├── provinces.json
├── region.json
└── surname.json
└── util
├── Email.php
├── Identity.php
├── Mobile.php
└── Nickname.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | .idea
3 | composer.lock
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Datasource local storage ignored files
5 | /dataSources/
6 | /dataSources.local.xml
7 | # Editor-based HTTP Client requests
8 | /httpRequests/
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/php.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/think-generator.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Dashing.Li
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # think-generator
2 |
3 | **thinkphp6 随机生成手机号、身份证号、邮箱地址、昵称等测试数据**
4 |
5 | **特此申明:本类的初衷是生成测试数据,不可进行任何违法行为,不可进行任何违法行为,不可进行任何违法行为!!!**
6 | ## Installation
7 | 通过在composer.json中配置安装:
8 | ```php
9 | composer require dashingunique/think-generator
10 | ```
11 | or
12 | ```php
13 | // composer.json
14 | composer install
15 | // composer update
16 | composer update
17 | ```
18 | ## Usage
19 | **
20 |
21 | ## 生成手机号码
22 | **
23 | ```php
24 | $generator = (new Generator($this->app))->generator('mobile');
25 |
26 | //随机生成一个手机号
27 | $generator->get(); //13611111111
28 |
29 | //随机生成一个号段为182的手机号
30 | $generator->prefix(182)->get();//18211111111
31 |
32 | //允许13,14,15,17,18,19号段的手机号生成(从中生成一个号码段)
33 | $generator->allow([3, 4, 5, 7, 8, 9])->get();//13011111111
34 |
35 | //获取多个手机号
36 | $generator->limit(10)->get();
37 |
38 | //若同时存在prefix 和 allow prefix强制覆盖allow
39 | $generator->prefix(182)->allow([3, 4, 5, 7, 8, 9])->limit(10)->get();//18211111111
40 |
41 | ```
42 |
43 | ## 生成邮箱地址
44 | **
45 | ```php
46 | $generator = (new Generator($this->app))->generator('email');
47 |
48 | //随机生成一个邮箱地址
49 | $generator->get();//12345678@qq.com
50 |
51 | //随机生成一个前缀为123456789的邮箱
52 | $generator->prefix(123456789)->get();//123456789@163.com
53 |
54 | //随机生成一个后缀为@qq.com的邮箱
55 | $generator->suffix('@qq.com')->get();//12345678@qq.com
56 |
57 | //生成一个指定长度的邮箱地址
58 | $generator->length(6)->get();//123456@qq.com
59 |
60 | //获取多个邮箱
61 | $generator->limit(10)->get();
62 |
63 | ```
64 |
65 | ### 生成身份证号码
66 | ```php
67 | $generator = (new Generator($this->app))->generator('identity');
68 |
69 | //随机生成一个身份证号码
70 | $generator->get();
71 |
72 | //随机生成一个指定性别的身份证号码(1男性 2女性)
73 | $generator->sex(1)->get();
74 |
75 | //随机生成一个北京市的身份证号码
76 | $generator->province('北京市')->get();
77 |
78 | //随机生成一个省份城市的身份证号码
79 | $generator->city('成都市')->get();
80 |
81 | //随机生成一个地区的身份证号码
82 | $generator->city('高新区')->get();
83 |
84 | //随机生成一个指定出生日期的身份证号码
85 | $generator->birthday('2000-01-01')->get();
86 |
87 | //获取多个身份证号码
88 | $generator->limit(10)->get();
89 |
90 | ```
91 |
92 | ## 生成昵称
93 | ```php
94 | $generator = (new Generator($this->app))->generator('nickname');
95 |
96 | //随机生成一个用户昵称
97 | $generator->get();
98 |
99 | //随机生成一个指定姓氏的用户昵称
100 | $generator->surname('张')->get();//张三
101 |
102 | //随机生成一个指定姓氏类型的用户昵称(1单姓 2复姓)
103 | $generator->compound(2)->get();//上官三
104 |
105 | //随机生成一个指定性别的用户昵称(1男性 2女性)
106 | $generator->sex(2)->get();//张美
107 |
108 | //获取一个指定长度的用户昵称
109 | $generator->length(3)->get();//张大麻子
110 |
111 | //获取多个用户昵称
112 | $generator->limit(10)->get();
113 |
114 | ```
115 |
116 |
117 |
118 | ```
119 |
120 |
121 |
122 |
123 | ## 后续
124 | 增加银行卡号的生成
125 | 验证生成的数据为一性
126 | 增加数据驱动类型:本地文件、Redis、数据库
127 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "dashingunique/think-generator",
3 | "description": "thinkphp6 random generation of mobile phone number, ID number, mailbox address, nickname and other test data.",
4 | "type": "src",
5 | "license": "MIT",
6 | "authors": [
7 | {
8 | "name": "dashing unique",
9 | "email": "1107842285@qq.com"
10 | }
11 | ],
12 | "minimum-stability": "dev",
13 | "require": {
14 | "php": ">=7.0"
15 | },
16 | "autoload": {
17 | "files": [
18 | "src/generator/Helper.php"
19 | ],
20 | "psr-4": {
21 | "dashing\\": "src"
22 | }
23 | },
24 | "extra": {
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/generator/BaseUtil.php:
--------------------------------------------------------------------------------
1 | = self::MAX) ? self::MAX : $limit;
43 | $this->limit = $limit;
44 |
45 | return $this;
46 | }
47 |
48 | /**
49 | * @return mixed
50 | */
51 | public function getLimit()
52 | {
53 | return $this->limit;
54 | }
55 |
56 | /**
57 | * 获取生成的省份证号码
58 | * @return array|string
59 | */
60 | public function get()
61 | {
62 | $limit = $this->getLimit();
63 | if ($limit > 1) {
64 | $data = [];
65 | for ($i = 0; $i < $this->getLimit(); $i++) {
66 | $data[] = $this->generate();
67 | }
68 | return $data;
69 | } else {
70 | return $this->generate();
71 | }
72 | }
73 |
74 | /**
75 | * @return mixed 生成信息
76 | */
77 | public abstract function generate();
78 |
79 | /**
80 | * @param $name
81 | * @param $arguments
82 | * @return $this
83 | */
84 | public function __call($name, $arguments)
85 | {
86 | if (!is_null($arguments) && isset($arguments[0])) {
87 | $this->setAttributes($name, $arguments[0]);
88 | }
89 | return $this;
90 | }
91 |
92 | /**
93 | * @param $key
94 | * @param $value
95 | * @return $this
96 | */
97 | protected function setAttributes($key, $value)
98 | {
99 | if ($this->isPadding($key)) {
100 | $this->attributes[$key] = $value;
101 | }
102 | return $this;
103 | }
104 |
105 | /**
106 | * 是否是额外参数
107 | * @param $value
108 | * @return bool
109 | */
110 | public function isPadding($value): bool
111 | {
112 | if (in_array($value, $this->getPadding())) {
113 | return false;
114 | }
115 | return true;
116 | }
117 |
118 | /**
119 | * @return array
120 | */
121 | public function getPadding(): array
122 | {
123 | return $this->padding;
124 | }
125 |
126 | /**
127 | * 获取性别信息
128 | * @return float|int
129 | */
130 | public function getSex()
131 | {
132 | $this->setSex();
133 | return $this->sex;
134 | }
135 |
136 | /**
137 | * 设置性别
138 | * @return $this
139 | */
140 | protected function setSex()
141 | {
142 | // sex is null , random 1 - 8
143 | if (!isset($this->attributes['sex'])) {
144 | $sex = random_int(1, 8);
145 | } elseif ($this->attributes['sex'] == self::MALE) {
146 | // sex is male
147 | $sex = 2 * random_int(1, 4) - 1;
148 | } else {
149 | // sex is female
150 | $sex = 2 * random_int(1, 4);
151 | }
152 | $this->sex = $sex;
153 | return $this;
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/src/generator/Generator.php:
--------------------------------------------------------------------------------
1 |
4 | * +----------------------------------------------------------------------
5 | * | Description: 随机生成器
6 | * +----------------------------------------------------------------------
7 | */
8 |
9 | namespace dashing\generator;
10 |
11 | use think\Manager;
12 |
13 | class Generator extends Manager
14 | {
15 | protected $namespace = '\\dashing\\generator\\util\\';
16 |
17 | /**
18 | * 生成器
19 | * @param string $name
20 | * @return mixed
21 | */
22 | public function generator(string $name)
23 | {
24 | return $this->driver(ucfirst($name));
25 | }
26 |
27 | /**
28 | * 默认驱动
29 | * @return string|null
30 | */
31 | public function getDefaultDriver()
32 | {
33 | return null;
34 | }
35 | }
--------------------------------------------------------------------------------
/src/generator/Helper.php:
--------------------------------------------------------------------------------
1 |
4 | * +----------------------------------------------------------------------
5 | * | Description: 邮箱地址生成器
6 | * +----------------------------------------------------------------------
7 | */
8 |
9 | namespace dashing\generator\util;
10 |
11 | use dashing\generator\BaseUtil;
12 | use dashing\library\Collection;
13 |
14 | class Email extends BaseUtil
15 | {
16 | /**
17 | * @var string 邮箱前缀
18 | */
19 | protected $prefix;
20 |
21 | // 字符集合
22 | protected $codeSet = '0123456789abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
23 |
24 | // 位数
25 | protected $length = 8;
26 |
27 | /**
28 | * @var string[] 常用邮箱后缀
29 | */
30 | protected $mailboxSuffix = [
31 | '@qq.com',
32 | '@163.com',
33 | '@sina.com',
34 | '@sohu.com',
35 | '@gmail.com',
36 | '@yahoo.com',
37 | '@msn.com',
38 | '@hotmail.com',
39 | '@aol.com',
40 | '@ask.com',
41 | '@live.com',
42 | '@0355.net',
43 | '@163.net',
44 | '@263.net',
45 | '@3721.net',
46 | '@yeah.net',
47 | '@googlemail.com',
48 | '@mail.com',
49 | '@sina.com',
50 | '@21cn.com',
51 | '@yahoo.com.cn',
52 | '@etang.com',
53 | '@eyou.com',
54 | '@56.com',
55 | '@x.cn',
56 | '@chinaren.com',
57 | '@sogou.com',
58 | '@citiz.com'
59 | ];
60 |
61 | /**
62 | * @var string 邮箱后缀
63 | */
64 | protected $suffix;
65 |
66 | /**
67 | * 获取邮箱前缀
68 | * @return string
69 | */
70 | public function getPrefix()
71 | {
72 | $this->setPrefix();
73 | return $this->prefix;
74 | }
75 |
76 | /**
77 | * 设置邮箱前缀
78 | * @return $this
79 | */
80 | public function setPrefix()
81 | {
82 | $regexp = "/^[a-zA-Z0-9]*$/";
83 | if (isset($this->attributes['prefix']) && preg_match($regexp, $this->attributes['prefix'])) {
84 | $this->prefix = $this->attributes['prefix'];
85 | } else {
86 | $characters = str_split($this->codeSet);
87 | $prefix = '';
88 | for ($i = 0; $i < $this->getLength(); $i++) {
89 | $value = $characters[rand(0, count($characters) - 1)];
90 | if (empty($i) && empty($value)) {
91 | $prefix .= rand(1,9);
92 | } else {
93 | $prefix .= $value;
94 | }
95 | }
96 | $this->prefix = $prefix;
97 | }
98 | return $this;
99 | }
100 |
101 | /**
102 | * 获取邮箱前缀长度
103 | * @return mixed
104 | */
105 | public function getLength()
106 | {
107 | if (isset($this->attributes['length'])) {
108 | $this->length = $this->attributes['length'];
109 | }
110 | return $this->length;
111 | }
112 |
113 | /**
114 | * 获取邮箱后缀
115 | * @return string
116 | */
117 | public function getSuffix()
118 | {
119 | $this->setSuffix();
120 | return $this->suffix;
121 | }
122 |
123 | /**
124 | * 设置邮箱后缀
125 | * @return $this
126 | */
127 | public function setSuffix()
128 | {
129 | $mailboxSuffix = new Collection($this->mailboxSuffix);
130 | if (isset($this->attributes['suffix']) && in_array($this->attributes['suffix'], $mailboxSuffix->all())) {
131 | $this->suffix = $this->attributes['suffix'];
132 | } else {
133 | $this->suffix = $mailboxSuffix->random();
134 | }
135 | return $this;
136 | }
137 |
138 | /**
139 | * @return mixed 生成信息
140 | */
141 | public function generate()
142 | {
143 | return $this->getPrefix() . $this->getSuffix();
144 | }
145 | }
--------------------------------------------------------------------------------
/src/generator/util/Identity.php:
--------------------------------------------------------------------------------
1 |
4 | * +----------------------------------------------------------------------
5 | * | Description: 身份证号码生成器
6 | * +----------------------------------------------------------------------
7 | */
8 |
9 | namespace dashing\generator\util;
10 |
11 | use dashing\generator\BaseUtil;
12 | use dashing\library\Collection;
13 | use InvalidArgumentException;
14 |
15 | class Identity extends BaseUtil
16 | {
17 | /**
18 | * 省份集合
19 | * @var Collection
20 | */
21 | protected $region;
22 |
23 | /**
24 | * 城市集合
25 | * @var Collection
26 | */
27 | protected $cities;
28 |
29 | /**
30 | * @var mixed 性别编号
31 | */
32 | protected $sex;
33 |
34 | /**
35 | * @var string 生日编号
36 | */
37 | protected $birthday;
38 |
39 | /**
40 | * @var mixed 省份
41 | */
42 | protected $province;
43 |
44 | /**
45 | * @var mixed 市区
46 | */
47 | protected $city;
48 |
49 | /**
50 | * @var array|Collection|mixed
51 | */
52 | private $district;
53 |
54 | public function __construct()
55 | {
56 | parent::__construct();
57 | $this->loadRegion();
58 | }
59 |
60 | /**
61 | * 获取省份
62 | * @return array|bool|int|Collection|mixed
63 | */
64 | public function getProvince()
65 | {
66 | $this->setProvince();
67 | return $this->province;
68 | }
69 |
70 | /**
71 | * 获取城市
72 | * @return int|mixed
73 | * @throws \Exception
74 | */
75 | public function getCity()
76 | {
77 | $this->setCity();
78 | return $this->city;
79 | }
80 |
81 | /**
82 | * @return mixed
83 | * @throws \Exception
84 | */
85 | public function getDistrict()
86 | {
87 | $this->setDistrict();
88 | return $this->district;
89 | }
90 |
91 | /**
92 | * 获取生日日期编号
93 | * @return string
94 | */
95 | protected function getBirthday()
96 | {
97 | $this->setBirthday();
98 | return $this->birthday;
99 | }
100 |
101 | /**
102 | * 加载省份信息
103 | */
104 | protected function loadRegion()
105 | {
106 | if (empty($this->region)) {
107 | $file = file_get_contents(generatorPath().'/data/region.json');
108 | $this->region = uniqueCollection(json_decode($file,
109 | true));
110 | }
111 | return $this;
112 | }
113 |
114 | /**
115 | * @return $this
116 | */
117 | protected function setBirthday()
118 | {
119 | //random Datatime
120 | if (!isset($this->attributes['birthday'])) {
121 | $startDate = mktime(0, 0, 0, 1, 1, 1920);
122 | $year = date('Y');
123 | $month = date('m');
124 | $day = date('d');
125 | $endDate = mktime(0, 0, 0, $month, $day, $year);
126 | $birth = mt_rand($startDate, $endDate);
127 | $datetime = date('Ymd', $birth);
128 | } else {
129 | list($year, $month, $day) = explode('-', $this->attributes['birthday']);
130 | if (!checkdate($month, $day, $year)) {
131 | throw new InvalidArgumentException('Invalided datetime');
132 | }
133 | $datetime = $year.$month.$day;
134 | }
135 | $this->birthday = $datetime;
136 | return $this;
137 | }
138 |
139 | /**
140 | * 设置省份信息
141 | * @return $this
142 | */
143 | protected function setProvince()
144 | {
145 | if (isset($this->attributes['province'])) {
146 | $province = $this->region->where('name', $this->attributes['province'])->where('level', 1)->first();
147 | } else {
148 | $province = $this->region->where('level', 1)->random();
149 | }
150 | $this->province = $province;
151 | return $this;
152 | }
153 |
154 | /**
155 | * 设置城市信息
156 | * @return $this
157 | */
158 | protected function setCity()
159 | {
160 | $province = $this->getProvince();
161 | try {
162 | $list = $this->region->where('parent_id', $province['id'])->where('level', 2);
163 | if (isset($this->attributes['city'])) {
164 | $city = $list->where('name', $this->attributes['city'])->first();
165 | } else {
166 | $city = $list->random();
167 | }
168 | } catch (\Exception $exception) {
169 | $city = $province;
170 | }
171 | $this->city = $city;
172 | return $this;
173 | }
174 |
175 | /**
176 | * 设置区域信息
177 | * @return $this
178 | * @throws \Exception
179 | */
180 | protected function setDistrict()
181 | {
182 | $city = $this->getCity();
183 | try {
184 | $list = $this->region->where('parent_id', $city['id'])->where('level', 3);
185 | if (isset($this->attributes['district'])) {
186 | $district = $list->where('name', $this->attributes['district'])->first();
187 | } else {
188 | $district = $list->random();
189 | }
190 | } catch (\Exception $exception) {
191 | $district = $city;
192 | }
193 | $this->district = $district;
194 | return $this;
195 | }
196 |
197 | /**
198 | * 计算省份证最后一位
199 | * @param $base
200 | * @return string
201 | */
202 | protected function getSuffixD($base)
203 | {
204 | if (strlen($base) <> 17) {
205 | die('Invalid Length');
206 | }
207 | // 权重
208 | $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
209 | $sums = 0;
210 | for ($i = 0; $i < 17; $i++) {
211 | $sums += substr($base, $i, 1) * $factor[$i];
212 | }
213 |
214 | $mods = $sums % 11; //10X98765432
215 | $return = '';
216 | switch ($mods) {
217 | case 0:
218 | $return = '1';
219 | break;
220 | case 1:
221 | $return = '0';
222 | break;
223 | case 2:
224 | $return = 'X';
225 | break;
226 | case 3:
227 | $return = '9';
228 | break;
229 | case 4:
230 | $return = '8';
231 | break;
232 | case 5:
233 | $return = '7';
234 | break;
235 | case 6:
236 | $return = '6';
237 | break;
238 | case 7:
239 | $return = '5';
240 | break;
241 | case 8:
242 | $return = '4';
243 | break;
244 | case 9:
245 | $return = '3';
246 | break;
247 | case 10:
248 | $return = '2';
249 | break;
250 | }
251 | return $return;
252 | }
253 |
254 | /**
255 | * 生成身份证号码
256 | * @return string
257 | */
258 | public function generate()
259 | {
260 | $districtCode = $this->getDistrict()['adcode'];
261 | $baseCode = $districtCode.$this->getBirthday().mt_rand(0, 9).mt_rand(0, 9).$this->getSex();
262 | return $baseCode.$this->getSuffixD($baseCode);
263 | }
264 | }
--------------------------------------------------------------------------------
/src/generator/util/Mobile.php:
--------------------------------------------------------------------------------
1 |
4 | * +----------------------------------------------------------------------
5 | * | Description: 手机号码生成器
6 | * +----------------------------------------------------------------------
7 | */
8 |
9 | namespace dashing\generator\util;
10 |
11 | use dashing\generator\BaseUtil;
12 | use think\helper\Arr;
13 |
14 | class Mobile extends BaseUtil
15 | {
16 | /**
17 | * @var string 手机号前缀:例如136
18 | */
19 | protected $prefix = '';
20 |
21 | /**
22 | * @var array 允许的号码段
23 | */
24 | protected $allowPrefix = [];
25 |
26 | /**
27 | * 获取手机号前缀
28 | * @return string
29 | */
30 | public function getPrefix()
31 | {
32 | $this->setPrefix();
33 | return $this->prefix;
34 | }
35 |
36 | /**
37 | * 设置手机号前缀
38 | * @return $this
39 | */
40 | public function setPrefix()
41 | {
42 | if (isset($this->attributes['prefix'])) {
43 | $this->prefix = $this->attributes['prefix'];
44 | } else {
45 | $allowPrefix = $this->getAllowPrefix();
46 | $this->prefix = 1 . Arr::random($allowPrefix) . mt_rand(0, 9);
47 | }
48 | return $this;
49 | }
50 |
51 | /**
52 | * 获取允许的号码段(手机号第二位)
53 | * @return array
54 | */
55 | public function getAllowPrefix(): array
56 | {
57 | $this->setAllowPrefix();
58 | return $this->allowPrefix;
59 | }
60 |
61 | /**
62 | * 设置允许的号码段(手机号第二位)
63 | * @param array $allow
64 | * @return $this
65 | */
66 | public function setAllowPrefix(array $allow = [])
67 | {
68 | if (isset($this->attributes['allow'])) {
69 | if (is_string($this->attributes['allow']) && strpos($this->attributes['allow'], ',')) {
70 | $this->allowPrefix = explode(',', $this->attributes['allow']);
71 | } elseif (is_array($this->attributes['allow'])) {
72 | $this->allowPrefix = $this->attributes['allow'];
73 | }
74 | } else {
75 | if (empty($allow)) {
76 | $allow = [3, 4, 5, 7, 8, 9];
77 | }
78 | $this->allowPrefix = $allow;
79 | }
80 | return $this;
81 | }
82 |
83 | /**
84 | * 生成手机号码
85 | * @return string
86 | */
87 | public function generate()
88 | {
89 | return $this->getPrefix() . str_pad(rand(0,99999999),8,0,STR_PAD_LEFT);
90 | }
91 | }
--------------------------------------------------------------------------------
/src/generator/util/Nickname.php:
--------------------------------------------------------------------------------
1 |
4 | * +----------------------------------------------------------------------
5 | * | Description: 名称生成器
6 | * +----------------------------------------------------------------------
7 | */
8 |
9 | namespace dashing\generator\util;
10 |
11 | use dashing\generator\BaseUtil;
12 |
13 | class Nickname extends BaseUtil
14 | {
15 | /**
16 | * @var string 姓氏
17 | */
18 | protected $surname;
19 |
20 | /**
21 | * @var string 名字
22 | */
23 | protected $lastName;
24 |
25 | /**
26 | * @var int 姓氏类型:1单姓 2复姓
27 | */
28 | protected $compound;
29 |
30 | /**
31 | * @var int 名长
32 | */
33 | protected $nameLength;
34 |
35 | /**
36 | * 获取复姓长度
37 | * @return int
38 | */
39 | public function getCompound()
40 | {
41 | if (isset($this->attributes['compound']) && $this->attributes['compound'] <= 2) {
42 | $this->compound = $this->attributes['compound'];
43 | } else {
44 | $this->compound = mt_rand(1, 2);
45 | }
46 | return $this->compound;
47 | }
48 |
49 | /**
50 | * @return string
51 | */
52 | public function getSurname()
53 | {
54 | $this->setSurname();
55 | return $this->surname;
56 | }
57 |
58 | /**
59 | * @return $this
60 | */
61 | public function setSurname()
62 | {
63 | if (isset($this->attributes['surname'])) {
64 | $this->surname = $this->attributes['surname'];
65 | } else {
66 | $file = file_get_contents(generatorPath().'/data/surname.json');
67 | $surnameData = uniqueCollection(json_decode($file, true));
68 | $surname = $surnameData->where('length', $this->getCompound())->random();
69 | $this->surname = $surname['surname'];
70 | }
71 | return $this;
72 | }
73 |
74 | /**
75 | * 获取名字
76 | * @return string
77 | */
78 | public function getName()
79 | {
80 | $this->setName();
81 | return $this->lastName;
82 | }
83 |
84 | /**
85 | * @return Nickname
86 | */
87 | protected function setSex(): Nickname
88 | {
89 | if (isset($this->attributes['sex'])) {
90 | $this->sex = $this->attributes['sex'];
91 | } else {
92 | $this->sex = mt_rand(self::MALE, self::FEMALE);
93 | }
94 | return $this;
95 | }
96 |
97 | /**
98 | * 设置名字信息
99 | * @return $this
100 | */
101 | public function setName()
102 | {
103 | $file = file_get_contents(generatorPath().'/data/name.json');
104 | $nameData = uniqueCollection(json_decode($file, true));
105 | $nameCollect = $nameData->where('sex', $this->getSex());
106 | $name = '';
107 | for ($i = 0; $i < $this->getLength(); $i++) {
108 | $nameItem = $nameCollect->random();
109 | $name .= $nameItem['name'];
110 | }
111 | $this->lastName = $name;
112 | return $this;
113 | }
114 |
115 | /**
116 | * 获取名字长度
117 | * @return $this
118 | */
119 | public function getLength()
120 | {
121 | if (isset($this->attributes['length'])) {
122 | $this->nameLength = $this->attributes['length'];
123 | } else {
124 | $this->nameLength = mt_rand(1, 2);
125 | }
126 | return $this->nameLength;
127 | }
128 |
129 | /**
130 | * @return mixed 生成信息
131 | */
132 | public function generate()
133 | {
134 | return $this->getSurname().$this->getName();
135 | }
136 | }
--------------------------------------------------------------------------------