*/ class PageFactory extends Factory { protected $model = Page::class; /** * @return array */ public function definition(): array { $title = $this->faker->unique()->sentence(4); $status = $this->faker->randomElement(PageStatus::cases()); return [ 'title' => $this->eachLocale(fn (string $locale) => rtrim($title, '.')." ({$locale})"), 'slug' => $this->eachLocale(fn (string $locale) => Str::slug($title."-{$locale}")), 'excerpt' => $this->eachLocale(fn () => $this->faker->sentence(12)), 'body' => $this->eachLocale(fn () => '

'.implode('

', $this->faker->paragraphs(3)).'

'), 'meta_title' => $this->eachLocale(fn () => Str::limit($title, 55, '')), 'meta_description' => $this->eachLocale(fn () => $this->faker->sentence(14)), 'category_id' => Category::factory(), 'author_id' => User::factory(), 'status' => $status, 'is_featured' => $this->faker->boolean(20), 'published_at' => $status === PageStatus::Published ? $this->faker->dateTimeBetween('-6 months') : null, ]; } public function published(): static { return $this->state(fn () => [ 'status' => PageStatus::Published, 'published_at' => now()->subDays(fake()->numberBetween(0, 30)), ]); } public function draft(): static { return $this->state(fn () => [ 'status' => PageStatus::Draft, 'published_at' => null, ]); } /** * A page translated only into the default locale — the state the * "missing translation" filters and the dashboard stat exist to surface. */ public function untranslated(): static { return $this->state(function (array $attributes): array { $default = (string) config('locales.default'); return [ 'title' => [$default => $attributes['title'][$default]], 'slug' => [$default => $attributes['slug'][$default]], 'excerpt' => [$default => $attributes['excerpt'][$default]], 'body' => [$default => $attributes['body'][$default]], ]; }); } /** * @param callable(string): string $value * @return array */ protected function eachLocale(callable $value): array { return collect(array_keys(config('locales.supported'))) ->mapWithKeys(fn (string $locale) => [$locale => $value($locale)]) ->all(); } }