@@ -28,7 +28,12 @@ public function run() {
2828 "myNum|1-100 " => 1 ,
2929 "myFloat|1-100.1-10 " => 1 ,
3030 "myFa|123.1-10 " => 1 ,
31- "myFb|123.3 " => 1
31+ "myFb|123.3 " => 1 ,
32+ "object|2 " => [
33+ "name|1-3 " => ['myName ' , 123123 , '1231541asdasd ' , 'jjjjsssss ' , '2345123afasgvawe ' ],
34+ "name2|2 " => ['myName ' , 123123 , '1231541asdasd ' , 'jjjjsssss ' , '2345123afasgvawe ' ],
35+ 'age|25-68 ' => 1
36+ ]
3237 ];
3338 $ data = $ this ->buildData ($ config );
3439
@@ -54,6 +59,7 @@ private function buildData($config) {
5459 $ data [$ name ] = $ this ->buildInt ($ rule );
5560 break ;
5661 case 'array ' :
62+ $ data [$ name ] = $ this ->buildArray ($ rule , $ value );
5763 break ;
5864 case 'string ' :
5965 $ data [$ name ] = $ this ->buildString ($ rule );
@@ -113,6 +119,7 @@ private function buildInt($rule = '') {
113119 $ hasVertical = strstr ($ rule , '- ' );
114120 if ($ hasVertical ) {
115121 list ($ min , $ max ) = explode ('- ' , $ rule );
122+
116123 return mt_rand ($ min , $ max );
117124 } else {
118125 return intval ($ rule );
@@ -137,12 +144,81 @@ private function buildString($rule = '') {
137144 return Strs::randString ($ len );
138145 }
139146
140- private function buildArray ($ rule = '' ) {
147+ /**
148+ * 构建随机的数组列表数据
149+ * @param string $rule
150+ * @param array $value
151+ * @return array
152+ * @author zhaoxiang <zhaoxiang051405@gmail.com>
153+ */
154+ private function buildArray ($ rule = '' , $ value = []) {
155+ $ isAssoc = $ this ->isAssoc ($ value );
156+ if ($ isAssoc ) {
157+ $ has = strstr ($ rule , '- ' );
158+ if ($ has ) {
159+ list ($ min , $ max ) = explode ('- ' , $ rule );
160+ $ num = mt_rand ($ min , $ max );
161+ } else {
162+ $ num = intval ($ rule );
163+ }
164+
165+ $ res = [];
166+ for ($ i = 0 ; $ i < $ num ; $ i ++) {
167+ $ new = [];
168+ foreach ($ value as $ vKey => $ item ) {
169+ $ hasVertical = strstr ($ vKey , '| ' );
170+ if ($ hasVertical ) {
171+ $ new = array_merge ($ new , $ this ->buildData ([$ vKey => $ item ]));
172+ } else {
173+ $ new [$ vKey ] = $ item ;
174+ }
175+ }
176+ $ res [] = $ new ;
177+ }
141178
179+ return $ res ;
180+ } else {
181+ $ hasVertical = strstr ($ rule , '- ' );
182+ if ($ hasVertical ) {
183+ $ new = [];
184+ list ($ min , $ max ) = explode ('- ' , $ rule );
185+ $ num = mt_rand ($ min , $ max );
186+ for ($ i = 0 ; $ i < $ num ; $ i ++) {
187+ $ new = array_merge ($ new , $ value );
188+ }
189+
190+ return $ new ;
191+ } else {
192+ $ rule = intval ($ rule );
193+ if (count ($ value ) <= $ rule ) {
194+ return $ value ;
195+ } else {
196+ $ new = [];
197+ shuffle ($ value );
198+ for ($ i = 0 ; $ i < $ rule ; $ i ++) {
199+ $ new [] = $ value [$ i ];
200+ }
201+
202+ return $ new ;
203+ }
204+ }
205+ }
142206 }
143207
144- private function buildObject ($ rule = '' ) {
208+ /**
209+ * 判断是否是关联数组
210+ * @param $array
211+ * @return bool true 是关联数组 false 是索引数组
212+ * @author zhaoxiang <zhaoxiang051405@gmail.com>
213+ */
214+ private function isAssoc ($ array ) {
215+ if (is_array ($ array )) {
216+ $ keys = array_keys ($ array );
217+
218+ return $ keys !== array_keys ($ keys );
219+ }
145220
221+ return false ;
146222 }
147223
148224}
0 commit comments