vSouza

CustomerFactory

Dec 5th, 2021
985
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. use App\Customer;
  4. use Faker\Generator as Faker;
  5. use Illuminate\Database\Eloquent\Factory;
  6.  
  7. /** @var Factory $factory */
  8. $factory->define(Customer::class, function (Faker $faker) {
  9.     $firstName = $faker->firstName();
  10.     $lastName = $faker->lastName();
  11.     $fullName = "${firstName} ${lastName}";
  12.     $email = makeEmail($fullName, $faker);
  13.     $city = $faker->city();
  14.     $postalCode = "{$faker->randomNumber(5, true)}-{$faker->randomNumber(3, true)}";
  15.  
  16.     return [
  17.         'name' => $fullName,
  18.         'birthdate' => $faker->date('Y-m-d', '-30 years'),
  19.         'gender' => $faker->randomElement(['Female', 'Male', 'Non-binary', 'Transgender', 'Intersex', 'Prefer not say']),
  20.         'city' => $city,
  21.         'address' => $faker->streetName() . ' Nº ' . $faker->buildingNumber() . ', ' .$city . ' - ' . $faker->state() . ' - CEP ' . $faker->postcode(),
  22.         'postal_code' => $postalCode,
  23.         'email' => $email,
  24.         'phone_number' => $faker->e164PhoneNumber(),
  25.     ];
  26. });
  27.  
  28. function makeEmail(string $fullName, Faker $faker): string
  29. {
  30.     return (string) Str::of($fullName)
  31.         ->replace(' ', '.')
  32.         ->ascii()
  33.         ->lower()
  34.         ->replaceMatches('/[^a-zA-Z0-9_.]+/', '')
  35.         ->replaceMatches('/\.d[aeiou]\.+/', '.')
  36.         ->append(rand(1, 100))
  37.         ->append('@' . $faker->safeEmailDomain());
  38. }
  39.  
RAW Paste Data