buckets/database/factories/BucketFactory.php

123 lines
3.4 KiB
PHP
Raw Normal View History

2025-12-29 23:32:05 +01:00
<?php
namespace Database\Factories;
use App\Models\Bucket;
use App\Models\Scenario;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Bucket>
*/
class BucketFactory extends Factory
{
public function definition(): array
{
$allocationType = $this->faker->randomElement([
Bucket::TYPE_FIXED_LIMIT,
Bucket::TYPE_PERCENTAGE,
Bucket::TYPE_UNLIMITED,
]);
return [
'scenario_id' => Scenario::factory(),
'name' => $this->faker->randomElement([
'Monthly Expenses',
'Emergency Fund',
'Investments',
'Vacation Fund',
'Car Maintenance',
'Home Repairs',
'Health & Medical',
'Education',
'Entertainment',
'Groceries',
'Clothing',
'Gifts',
]),
'priority' => $this->faker->numberBetween(1, 10),
'sort_order' => $this->faker->numberBetween(0, 10),
'allocation_type' => $allocationType,
'allocation_value' => $this->getAllocationValueForType($allocationType),
];
}
/**
* Create a fixed limit bucket.
*/
public function fixedLimit($amount = null): Factory
{
$amount = $amount ?? $this->faker->numberBetween(500, 5000);
return $this->state([
'allocation_type' => Bucket::TYPE_FIXED_LIMIT,
'allocation_value' => $amount,
]);
}
/**
* Create a percentage bucket.
*/
public function percentage($percentage = null): Factory
{
$percentage = $percentage ?? $this->faker->numberBetween(10, 50);
return $this->state([
'allocation_type' => Bucket::TYPE_PERCENTAGE,
'allocation_value' => $percentage,
]);
}
/**
* Create an unlimited bucket.
*/
public function unlimited(): Factory
{
return $this->state([
'allocation_type' => Bucket::TYPE_UNLIMITED,
'allocation_value' => null,
]);
}
/**
* Create default buckets set (Monthly Expenses, Emergency Fund, Investments).
*/
public function defaultSet(): array
{
return [
$this->state([
'name' => 'Monthly Expenses',
'priority' => 1,
'sort_order' => 1,
'allocation_type' => Bucket::TYPE_FIXED_LIMIT,
'allocation_value' => 0,
]),
$this->state([
'name' => 'Emergency Fund',
'priority' => 2,
'sort_order' => 2,
'allocation_type' => Bucket::TYPE_FIXED_LIMIT,
'allocation_value' => 0,
]),
$this->state([
'name' => 'Investments',
'priority' => 3,
'sort_order' => 3,
'allocation_type' => Bucket::TYPE_UNLIMITED,
'allocation_value' => null,
]),
];
}
/**
* Get allocation value based on type.
*/
private function getAllocationValueForType(string $type): ?float
{
return match($type) {
Bucket::TYPE_FIXED_LIMIT => $this->faker->numberBetween(100, 10000),
Bucket::TYPE_PERCENTAGE => $this->faker->numberBetween(5, 50),
Bucket::TYPE_UNLIMITED => null,
};
}
}