ConfigFactory.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /* @var $factory \Illuminate\Database\Eloquent\Factory */
  3. use App\Models\Config;
  4. use Faker\Generator as Faker;
  5. $factory->define(Config::class, function (Faker $faker) {
  6. list($type, $options) = _make_type_and_options($faker);
  7. return [
  8. 'category_id' => 0,
  9. 'type' => $type,
  10. 'name' => 'name_'.$faker->unique()->word,
  11. 'slug' => 'slug_'.$faker->unique()->word,
  12. 'desc' => (mt_rand(0, 9) > 8) ? ('desc '.$faker->paragraph) : null,
  13. 'options' => $options,
  14. 'value' => null,
  15. 'validation_rules' => $faker->randomElement(['required', 'max:5', 'min:5']),
  16. ];
  17. });
  18. if (!function_exists('_make_type_and_options')) {
  19. function _make_type_and_options(Faker $faker)
  20. {
  21. $type = $faker->randomElement(array_keys(Config::$typeMap));
  22. $options = null;
  23. switch ($type) {
  24. case Config::TYPE_INPUT:
  25. case Config::TYPE_TEXTAREA:
  26. case Config::TYPE_OTHER:
  27. $options = null;
  28. break;
  29. case Config::TYPE_FILE:
  30. $options = [
  31. 'max' => mt_rand(1, 99),
  32. 'ext' => null,
  33. ];
  34. break;
  35. case Config::TYPE_SINGLE_SELECT:
  36. case Config::TYPE_MULTIPLE_SELECT:
  37. $values = $faker->randomElements(['', 1, 2, 3, 4, 5], mt_rand(1, 6));
  38. $pairs = array_map(function ($i) {
  39. $label = $i ? ('值'.$i) : '无';
  40. return $i.'=>'.$label;
  41. }, $values);
  42. $options = [
  43. 'options' => implode("\n", $pairs),
  44. 'type' => $faker->randomElement(['input', 'select']),
  45. ];
  46. break;
  47. default:
  48. // do nothing
  49. }
  50. return [$type, $options];
  51. }
  52. }