|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Eclipse\Catalogue\Factories; |
| 4 | + |
| 5 | +use Eclipse\Catalogue\Models\Category; |
| 6 | +use Illuminate\Database\Eloquent\Factories\Factory; |
| 7 | +use Illuminate\Support\Carbon; |
| 8 | +use Illuminate\Support\Str; |
| 9 | + |
| 10 | +class CategoryFactory extends Factory |
| 11 | +{ |
| 12 | + protected $model = Category::class; |
| 13 | + |
| 14 | + public function definition(): array |
| 15 | + { |
| 16 | + $englishName = mb_ucfirst(fake()->words(2, true)); |
| 17 | + $slovenianName = 'SI: '.$englishName; |
| 18 | + |
| 19 | + $englishShortDesc = fake()->sentence(); |
| 20 | + $slovenianShortDesc = 'SI: '.$englishShortDesc; |
| 21 | + |
| 22 | + $englishDesc = fake()->paragraphs(3, true); |
| 23 | + $slovenianDesc = 'SI: '.$englishDesc; |
| 24 | + |
| 25 | + return [ |
| 26 | + 'name' => [ |
| 27 | + 'en' => $englishName, |
| 28 | + 'sl' => $slovenianName, |
| 29 | + ], |
| 30 | + 'parent_id' => null, |
| 31 | + 'image' => self::generateCategoryImage($englishName), |
| 32 | + 'sort' => fake()->randomNumber(), |
| 33 | + 'is_active' => fake()->boolean(), |
| 34 | + 'code' => fake()->optional()->bothify('CAT-####'), |
| 35 | + 'recursive_browsing' => fake()->boolean(), |
| 36 | + 'sef_key' => [ |
| 37 | + 'en' => Str::slug($englishName), |
| 38 | + 'sl' => Str::slug($slovenianName), |
| 39 | + ], |
| 40 | + 'short_desc' => [ |
| 41 | + 'en' => $englishShortDesc, |
| 42 | + 'sl' => $slovenianShortDesc, |
| 43 | + ], |
| 44 | + 'description' => [ |
| 45 | + 'en' => $englishDesc, |
| 46 | + 'sl' => $slovenianDesc, |
| 47 | + ], |
| 48 | + 'created_at' => Carbon::now(), |
| 49 | + 'updated_at' => Carbon::now(), |
| 50 | + 'site_id' => null, |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + private static function generateCategoryImage(string $name): ?string |
| 55 | + { |
| 56 | + $colors = ['3B82F6', '10B981', 'F59E0B', 'EF4444', '8B5CF6', '06B6D4', 'F97316', 'EC4899']; |
| 57 | + $backgrounds = ['E0F2FE', 'ECFDF5', 'FFFBEB', 'FEF2F2', 'F3E8FF', 'ECFEFF', 'FFF7ED', 'FDF2F8']; |
| 58 | + |
| 59 | + $color = fake()->randomElement($colors); |
| 60 | + $background = fake()->randomElement($backgrounds); |
| 61 | + |
| 62 | + return fake()->optional(0.8)->passthrough( |
| 63 | + 'https://ui-avatars.com/api/?name='.urlencode($name). |
| 64 | + '&size=400&background='.$background. |
| 65 | + '&color='.$color. |
| 66 | + '&bold=true&format=png' |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public function parent(): static |
| 71 | + { |
| 72 | + return $this->state(fn (array $attributes): array => [ |
| 73 | + 'parent_id' => null, |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + public function child(?Category $parent = null): static |
| 78 | + { |
| 79 | + return $this->state(fn (array $attributes): array => [ |
| 80 | + 'parent_id' => $parent?->id ?? Category::factory()->parent()->create()->id, |
| 81 | + ]); |
| 82 | + } |
| 83 | + |
| 84 | + public function active(): static |
| 85 | + { |
| 86 | + return $this->state(fn (array $attributes): array => [ |
| 87 | + 'is_active' => true, |
| 88 | + ]); |
| 89 | + } |
| 90 | + |
| 91 | + public function inactive(): static |
| 92 | + { |
| 93 | + return $this->state(fn (array $attributes): array => [ |
| 94 | + 'is_active' => false, |
| 95 | + ]); |
| 96 | + } |
| 97 | +} |
0 commit comments