158 lines
5.4 KiB
PHP
158 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Author;
|
|
use App\Models\Article;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
/**
|
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
|
|
*/
|
|
class ArticleFactory extends Factory
|
|
{
|
|
/**
|
|
* Configure the model factory.
|
|
*/
|
|
public function configure(): static
|
|
{
|
|
return $this->afterMaking(function (Article $article) {
|
|
$article->convertMarkdownToMarkup();
|
|
})->afterCreating(function (Article $article) {
|
|
// ...
|
|
});
|
|
}
|
|
|
|
/**
|
|
* The current User being used by the factory.
|
|
*/
|
|
protected static Author $author;
|
|
|
|
/**
|
|
* The current Markdown body being used by the factory.
|
|
*/
|
|
protected static string $markdown_body;
|
|
|
|
/**
|
|
* Define the model's default state.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function definition(): array
|
|
{
|
|
return [
|
|
'title' => $this->getRandomTitle(),
|
|
'slug' => fake()->unique()->slug(),
|
|
'author_id' => static::$author->id ??= Author::factory()->create()->id,
|
|
'subtitle' => $this->getRandomSentence(),
|
|
'body' => '', // We'll update it after making using the on-Model method
|
|
'published_at' => $this->generateRandomPublishDate(),
|
|
'header_image_url' => null,
|
|
'markdown_body' => $this->generateMarkdown(),
|
|
'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,
|
|
]);
|
|
}
|
|
|
|
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
|
|
$markdown_result .= '## ' . fake()->words(rand(1,5), true) . "\n";
|
|
} elseif ($dice_roll <= 100) { // Make a paragraph
|
|
$markdown_result .= fake()->sentences(rand(2,5), true) . "\n";
|
|
}
|
|
|
|
// Always append a new line
|
|
$markdown_result .= "\n";
|
|
}
|
|
|
|
return $markdown_result;
|
|
}
|
|
|
|
private function generateRandomPublishDate(): Carbon
|
|
{
|
|
$isOlder = rand(0,1);
|
|
if($isOlder) {
|
|
return now()->subDays(rand(1,800));
|
|
} else {
|
|
return now()->addDays(rand(0,50));
|
|
}
|
|
}
|
|
|
|
private function getRandomTitle(): string
|
|
{
|
|
$possibilities = [
|
|
'What\'s in a Title? Hopefully More Than This',
|
|
'This One Will Leave You Speechless',
|
|
'Finally, The Pizza of Your Dreams',
|
|
'We Came, We Saw, and Then...',
|
|
'The Incomprehensibility of Aluminum',
|
|
'You Merely Adopted this Format, I Was Born In It, Molded by It',
|
|
'Vector Math is for Cowards',
|
|
'When Two Packets Love Each Other Very Much',
|
|
'I\'m Slipping, I\'m Sliding (I\'m on a Slip \'n Slide)',
|
|
'Tips On Giving Up (Maximum Efficiency)',
|
|
'Top 10 Reasons to Give Up Completely',
|
|
'Even the Monsters Are Disappointed',
|
|
];
|
|
|
|
return $possibilities[random_int(0, count($possibilities) - 1)];
|
|
}
|
|
|
|
private function getRandomSentence(): string
|
|
{
|
|
$possibilities = [
|
|
'That page would not bring the reward that was promised.',
|
|
'However, his tendency toward love was endearing, I miss that most of all.',
|
|
'The jellyfish doesn\'t care what you think, that\'s hubris.',
|
|
'And yet.',
|
|
'I find it distasteful, the smell lingering in the air for weeks and months while officials dragged their feet.',
|
|
'I think it was down this hill, maybe.',
|
|
'Cold eyes, I never liked them.',
|
|
'You can call me fit, or the love of your life, if you want.',
|
|
'The kei truck proves its superiority yet again in the quarter-mile.',
|
|
'He knocked, but heard nothing.',
|
|
'First base!',
|
|
'But these histories are still lost to us, despite our vast technical advances.',
|
|
'In the sincerest way possible, I am begging you to start the fire.',
|
|
'They drifted up high over head and then sank reluctantly.',
|
|
'I\'m so proud of you, no matter how belated!',
|
|
'She gripped the handle, but didn\'t have the heart to turn it.',
|
|
'If this, then that, so to speak.',
|
|
'Mapping the source column onto the target column should be strictly validated to ensure no missing fields.',
|
|
'Would there be something in there, though?',
|
|
'The wood splintering echoed down the hall.',
|
|
'I simply do not understand.',
|
|
'The radio tower nearly scraped the belly of the bomber as it strained against the immense pressure.',
|
|
'What\'s actually left after that?',
|
|
'Yes, but it needs more spice.',
|
|
'Backtracking is good, actually.',
|
|
'Scan has always been the problem.',
|
|
'Smell first, then you can think about licking.',
|
|
];
|
|
|
|
return $possibilities[random_int(0, count($possibilities) - 1)];
|
|
|
|
}
|
|
}
|