2024-08-04 04:49:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Database\Factories;
|
|
|
|
|
|
|
|
|
|
use App\Models\Author;
|
2024-08-04 17:32:32 +00:00
|
|
|
use App\Models\Article;
|
2025-03-19 21:52:49 +00:00
|
|
|
use Carbon\Carbon;
|
2024-08-04 04:49:42 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
|
|
|
|
|
*/
|
|
|
|
|
class ArticleFactory extends Factory
|
|
|
|
|
{
|
2024-08-04 17:32:32 +00:00
|
|
|
/**
|
|
|
|
|
* Configure the model factory.
|
|
|
|
|
*/
|
|
|
|
|
public function configure(): static
|
|
|
|
|
{
|
|
|
|
|
return $this->afterMaking(function (Article $article) {
|
|
|
|
|
$article->convertMarkdownToMarkup();
|
|
|
|
|
})->afterCreating(function (Article $article) {
|
|
|
|
|
// ...
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-04 04:49:42 +00:00
|
|
|
/**
|
|
|
|
|
* The current User being used by the factory.
|
|
|
|
|
*/
|
|
|
|
|
protected static Author $author;
|
|
|
|
|
|
2024-08-04 17:32:32 +00:00
|
|
|
/**
|
|
|
|
|
* The current Markdown body being used by the factory.
|
|
|
|
|
*/
|
|
|
|
|
protected static string $markdown_body;
|
|
|
|
|
|
2024-08-04 04:49:42 +00:00
|
|
|
/**
|
|
|
|
|
* Define the model's default state.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, mixed>
|
|
|
|
|
*/
|
|
|
|
|
public function definition(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'title' => fake()->words(3, true),
|
|
|
|
|
'slug' => fake()->unique()->slug(),
|
|
|
|
|
'author_id' => static::$author->id ??= Author::factory()->create()->id,
|
|
|
|
|
'subtitle' => fake()->sentence(),
|
2024-08-04 17:32:32 +00:00
|
|
|
'body' => '', // We'll update it after making using the on-Model method
|
2025-03-19 21:52:49 +00:00
|
|
|
'published_at' => $this->generateRandomPublishDate(),
|
2024-08-04 04:49:42 +00:00
|
|
|
'header_image_url' => fake()->imageUrl(),
|
2024-08-04 17:32:32 +00:00
|
|
|
'markdown_body' => $this->generateMarkdown(),
|
2024-08-04 04:49:42 +00:00
|
|
|
'processed_at' => fake()->date('Y-m-d H:m:s'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicate the model's associated Author
|
|
|
|
|
*/
|
|
|
|
|
public function author(int|Author $author): static
|
|
|
|
|
{
|
|
|
|
|
if(is_int($author)) {
|
|
|
|
|
$author = Author::findOrFail($author);
|
|
|
|
|
}
|
|
|
|
|
static::$author = $author;
|
|
|
|
|
|
|
|
|
|
return $this->state(fn (array $attributes) => [
|
|
|
|
|
'author_id' => $author->id,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2024-08-04 17:32:32 +00:00
|
|
|
|
|
|
|
|
private function generateMarkdown()
|
|
|
|
|
{
|
|
|
|
|
$markdown_result = '';
|
|
|
|
|
|
|
|
|
|
foreach(range(1,rand(15,45)) as $index) {
|
|
|
|
|
$dice_roll = rand(0,100);
|
|
|
|
|
|
|
|
|
|
if($dice_roll <= 50) { // Make a SubTitle
|
2024-08-04 19:07:03 +00:00
|
|
|
$markdown_result .= '## ' . fake()->words(rand(1,5), true) . "\n";
|
2024-08-04 17:32:32 +00:00
|
|
|
} elseif ($dice_roll <= 100) { // Make a paragraph
|
2024-08-04 17:38:03 +00:00
|
|
|
$markdown_result .= fake()->sentences(rand(2,5), true) . "\n";
|
2024-08-04 17:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Always append a new line
|
2024-08-04 17:38:03 +00:00
|
|
|
$markdown_result .= "\n";
|
2024-08-04 17:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $markdown_result;
|
|
|
|
|
}
|
2025-03-19 21:52:49 +00:00
|
|
|
|
|
|
|
|
private function generateRandomPublishDate(): Carbon
|
|
|
|
|
{
|
|
|
|
|
$isOlder = rand(0,1);
|
|
|
|
|
if($isOlder) {
|
|
|
|
|
return now()->subDays(rand(1,800));
|
|
|
|
|
} else {
|
|
|
|
|
return now()->addDays(rand(0,50));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-04 04:49:42 +00:00
|
|
|
}
|