parfum/database/factories/VoucherFactory.php

67 lines
1.8 KiB
PHP

<?php
namespace Database\Factories;
use App\Enums\IsShow;
use App\Enums\VoucherType;
use App\Models\Voucher;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class VoucherFactory extends Factory
{
protected $model = Voucher::class;
public function definition(): array
{
$type = $this->faker->randomElement(VoucherType::values());
return [
'name' => $this->faker->words(2, true),
'code' => Str::upper(Str::random(8)),
'tags' => $this->faker->words(1, true),
'type' => $type,
'discount_amount' => $type === VoucherType::PERCENTAGE
? $this->faker->numberBetween(5, 50)
: $this->faker->numberBetween(10000, 100000),
'limit_per_user' => 1,
'summary' => $this->faker->sentence(),
'start_date' => $this->faker->dateTimeBetween('tomorrow', '+1 week')->format('Y-m-d'),
'is_show' => IsShow::SHOW,
];
}
public function withMinPurchase(int $amount): Factory
{
return $this->state(fn () => ['min_purchase' => $amount]);
}
public function withMaxDiscount(int $amount): Factory
{
return $this->state(fn () => ['max_discount' => $amount]);
}
public function withQuota(int $quota): Factory
{
return $this->state(fn () => [
'quota' => $quota,
'available_count' => $quota,
]);
}
public function withEndDate(string $date): Factory
{
return $this->state(fn () => ['end_date' => $date]);
}
public function show(): Factory
{
return $this->state(fn () => ['is_show' => IsShow::SHOW]);
}
public function hidden(): Factory
{
return $this->state(fn () => ['is_show' => IsShow::HIDDEN]);
}
}